当前位置:编程学习 > asp >>

c# asp.net 多数组索引的解决方法

本人今天做了一个功能 需要在一个类里用多个数组,

数组需要索引器来调用  一个数组

我查了msdn 一个类里面只能有一个this 索引器

那这么多数组如何构造索引呢

我在坛子里找到了解决之道

view plaincopy to clipboardprint?
using System;  
 
namespace TestUse  
{  
    /// <summary>  
    /// Summary description for Muliti.  
    /// </summary>  
    public class Muliti  
    {  
        public Muliti()  
        {  
            //  
            // TODO: Add constructor logic here  
            //  
        }  
 
        private string[] test1;  
        private object[] test2;  
        private int[]    test3;  
 
        public object this[string arrname,int index]{  
            get{  
                switch(arrname){  
                    case "test1":return test1[index];  
                    case "test2":return test2[index];  
                    case "test3":return test3[index];  
                    default:return null;  
                }  
            }  
            set{  
                switch(arrname)  
                {  
                    case "test1":test1[index]=value.ToString();break;  
                    case "test2":test2[index]=value;break;  
                    case "test3":test3[index]=(int)value;break;  
                    default:break;  
                }  
            }  
        }  
 
        public void setUpArray(){  
            test1 = new string[3];  
            test2 = new object[2];  
            test3 = new int[4];  
        }  
    }  

using System;

namespace TestUse
{
    /// <summary>
    /// Summary description for Muliti.
    /// </summary>
    public class Muliti
    {
        public Muliti()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private string[] test1;
        private object[] test2;
        private int[]    test3;

        public object this[string arrname,int index]{
            get{
                switch(arrname){
                    case "test1":return test1[index];
                    case "test2":return test2[index];
                    case "test3":return test3[index];
                    default:return null;
                }
            }
            set{
                switch(arrname)
                {
                    case "test1":test1[index]=value.ToString();break;
                    case "test2":test2[index]=value;break;
                    case "test3":test3[index]=(int)value;break;
                    default:break;
                }
            }
        }

        public void setUpArray(){
            test1 = new str

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,