国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

c# asp.net 多數(shù)組索引的解決方法

2023-05-09 18:58:31
字體:
供稿:網(wǎng)友

本人今天做了一個功能 需要在一個類里用多個數(shù)組,

數(shù)組需要索引器來調(diào)用  一個數(shù)組

我查了msdn 一個類里面只能有一個this 索引器

那這么多數(shù)組如何構(gòu)造索引呢

我在壇子里找到了解決之道

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]);
        } 

這樣 加個判斷就行了 有時候頭腦還真的迷糊 想了半天還是沒想起來。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 原阳县| 沽源县| 堆龙德庆县| 泰来县| 溧阳市| 乐安县| 伊宁县| 平乡县| 民权县| 凉山| 屯留县| 苗栗县| 大理市| 平塘县| 舞钢市| 辽中县| 合阳县| 招远市| 辽源市| 龙里县| 汽车| 原阳县| 马尔康县| 铜山县| 南丹县| 科技| 普陀区| 重庆市| 吐鲁番市| 定远县| 南安市| 高唐县| 平果县| 略阳县| 铜川市| 颍上县| 雷山县| 遂平县| 肥城市| 天水市| 登封市|