本人今天做了一個功能 需要在一個類裡用多個數組,
數組需要索引器來調用 一個數組
我查了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