在C#中无法访问方法属性 [英] Method property not access in C#
问题描述
您好以下代码请帮帮我
公共类K1
{
公共K1( )
{
I1Value = new I1 [] {};
}
private I1 [] I1Value;
public I1 [] K1_Method
{
get
{
返回this.I1Value;
}
设定
{
this.I1Value = value;
}
}
}
公共类I1
{
public I1()
{
Cd = new InstCode();
}
私有InstCode cdField;
私有字符串instField;
公共Instruction3Code Cd
{
get
{
返回this.cdField;
}
set
{
this.cdField = value;
}
}
public string InstrInf
{
get
{
返回this.instField;
}
套装
{
this.instField = value;
}
}
}
公共枚举InstCode
{
A,
B,
C,
D,
}
当我试图访问这样的方法无法访问时
protected void Page_Load(object sender, EventArgs e)
{
试试
{
K1 objK1 =新K1();
//objK1.K1_Method[0].Cd =((InstCode)Enum.Parse(typeof(InstCode),D));
objK1.K1_Method。
}
catch(例外情况)
{
}
}
我尝试了什么:
我试过了访问
objK1.K1_Method。
但它没有访问。
Hello in the below code please help me
public class K1
{
public K1()
{
I1Value = new I1[] { };
}
private I1[] I1Value;
public I1[] K1_Method
{
get
{
return this.I1Value;
}
set
{
this.I1Value = value;
}
}
}
public class I1
{
public I1()
{
Cd = new InstCode();
}
private InstCode cdField;
private string instField;
public Instruction3Code Cd
{
get
{
return this.cdField;
}
set
{
this.cdField = value;
}
}
public string InstrInf
{
get
{
return this.instField;
}
set
{
this.instField = value;
}
}
}
public enum InstCode
{
A,
B,
C,
D,
}
When i am trying to access method like this not able to access
protected void Page_Load(object sender, EventArgs e)
{
try
{
K1 objK1 = new K1();
//objK1.K1_Method[0].Cd = ((InstCode)Enum.Parse(typeof(InstCode), "D"));
objK1.K1_Method.
}
catch (Exception ex)
{
}
}
What I have tried:
I have tryied to access
objK1.K1_Method.
but its not accessing .
推荐答案
首先,K1_Method之后的点没有帮助 - 之后需要一个标识符。
但是......如果删除该行并取消注释上面的行将编译(假设Instruction3Code
在某处声明 - 你可能意味着InstCode为匹配你返回的字段类型。
但是......代码赢了;单独工作,因为你的中没有元素I1Value
数组:
First off, the "dot" after K1_Method is not helping - you need an identifier after that.
But...if you remove that line and uncomment the line above it will compile (assumingInstruction3Code
is declared somewhere - you probably meant InstCode to match the field type you return).
But...that code won;t work in isolation, because there are no elements in yourI1Value
array:
I1Value = new I1[] { };
除非你添加一些,索引0处没有元素,因此当您尝试运行代码时,将获得 IndexOutOfRangeException
。它有点像一个厨房餐具抽屉:你伸手去拿叉子而且它是空的,所以你不能吃你的食物!首先向抽屉添加更多的叉子,然后您可以删除一个。
Unless you add some,. there is no element at index 0, so you will get an IndexOutOfRangeException
when you try to run the code. It's a bit like a kitchen cutlery drawer: you reach in to get a fork and it's empty, so you can't eat your food! Add more forks to the drawer first, and you can remove one.
这篇关于在C#中无法访问方法属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!