我在一个类中设置了几个属性
如:public string A1
public int A2
...... 我想给这些属性加上几个参数,像VB6中的属性设置这样dim m_ItemID() as integer
redim m_ItemID(3,5) as integerpublic property Let ItemID(byval a as integer,byval b as integer ,byval NewValue as integer)
m_itemdid(a,b)=NewValue
end propertypublic Property Get ItemID(byval a as integer,byval b as integer) as integer
itemid=m_itemid(a,b)
end property写属性:对象.Itemid(1,3)=5
读属性:msgbox 对象.ItemID(1,3) & ""
如:public string A1
public int A2
...... 我想给这些属性加上几个参数,像VB6中的属性设置这样dim m_ItemID() as integer
redim m_ItemID(3,5) as integerpublic property Let ItemID(byval a as integer,byval b as integer ,byval NewValue as integer)
m_itemdid(a,b)=NewValue
end propertypublic Property Get ItemID(byval a as integer,byval b as integer) as integer
itemid=m_itemid(a,b)
end property写属性:对象.Itemid(1,3)=5
读属性:msgbox 对象.ItemID(1,3) & ""
class IndexerClass
{
private int [] myArray = new int[100];
public int this [int index] // Indexer declaration
{
get
{
// Check the index limits.
if (index < 0 || index >= 100)
return 0;
else
return myArray[index];
}
set
{
if (!(index < 0 || index >= 100))
myArray[index] = value;
}
}
}
对于属性应该使用get(),set()方法
读属性:msgbox 对象.ItemID(1,3)
这样好像不可以吧!
最多就是对象本身可以用索引器来引用对象的成员
class a
{
int [] num = new int[5] ;
public a ()
{
//初始化
}
public int this[int index]
{
get{
return num[index] ;
}
set{
num[index] = value ;
}
}
}这样你就可以这么使用 class a的对象了class a = new a();
a[2] = 2;
Console.WriteLine(a[2]) ;//结果显示 2
public|protected|private 数据类型 this[数据类型 索引参数名]
{
get
{
}
set
{
}
}
我想在一个类中使用多个带参数的属性,我要用类.属性名(参数1,参数2,...参数n)=值的方式使用,难道c#中没有可以带多个参数的属性吗?顺便问一下,如果使用索引器,那在一个类中我要使用几个返回int的索引器怎么办?
class a
{
int [,,,,] num = new int[5,...] ;
public a ()
{
//初始化
}
public int this[int index1,int index2,...] //也可以传其他类型的参数如:string str
{
get{
return num[index,index1,index2,...] ;
}
set{
num[index,index1,index2,...] = value ;
}
}
}
如下:
class a
{
int [] num = new int[5] ;
int [] num2 = new int[5] ;
public a ()
{
//初始化
}
public int this[string numstr ,int index]
{
get{
switch (numstr){
"num": return num[index] ;
"num2": return num2[index] ;
}
}
set{
switch (numstr){
"num": num[index] = value ; break ;
"num2": num2[index] = value ; break ;
} }
}
}