程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> c# asp.net 多數組索引的解決方法

c# asp.net 多數組索引的解決方法

編輯:C#入門知識

本人今天做了一個功能 需要在一個類裡用多個數組,

數組需要索引器來調用  一個數組

我查了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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved