首页 > 编程 > C# > 正文

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

2023-05-09 18:58:31
字体:
来源:转载
供稿:网友

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

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

我查了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 string[3];
            test2 = new object[2];
            test3 = new int[4];
        }
    }
}
 

view plaincopy to clipboardprint?
private void button1_Click(object sender, System.EventArgs e)  
  {  
      Muliti testm = new Muliti();  
      testm.setUpArray();  
      testm["test1",0]="test1-0";  
      testm["test2",0]= "test2-0";  
      testm["test3",0]= 3;  
      MessageBox.Show((string)testm["test1",0]);  
      MessageBox.Show((string)testm["test2",0]);  
      MessageBox.Show("" + testm["test3",0]);  
  } 
      private void button1_Click(object sender, System.EventArgs e)
        {
            Muliti testm = new Muliti();
            testm.setUpArray();
            testm["test1",0]="test1-0";
            testm["test2",0]= "test2-0";
            testm["test3",0]= 3;
            MessageBox.Show((string)testm["test1",0]);
            MessageBox.Show((string)testm["test2",0]);
            MessageBox.Show("" + testm["test3",0]);
        } 

这样 加个判断就行了 有时候头脑还真的迷糊 想了半天还是没想起来。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表