如何将值从函数传递给属性 [英] How to pass the value from function to property
问题描述
public class 字段
{
public string StructureField_Identifier {获得跨度>; set ; }
public string func_in_field( string a)
{
Field n = new Field();
n.StructureField_Identifier = a;
return n;
}
}
//要将函数func_in_field中的字符串a传递给公共字符串StructureField_Identifier,我使用的是n.StructureField_Identifier 。但我无法通过。如何将值从函数传递给属性。
除非我不理解你,否则它应该是:
public class 字段
{
public string StructureField_Identifier { get ; set ; }
public string func_in_field( string a)
{
StructureField_Identifier = a; // 在此实例中为属性分配。
返回 a; // ????
}
}
你好,
Ron Sir的帖子是正确的
首先,
public class 字段
{
public string StructureField_Identifier { get ; set ; }
public string func_in_field( string a)
{
Field n = new Field();
n.StructureField_Identifier = a;
return n;
}
}
您的方法func_in_field的返回类型是一个字符串和您拥有的类型用来返回的是Field
所以这段代码不会编译。
它给出的错误是:
Quote:无法将类型字段转换为字符串
要编译它,请执行以下操作:
public string func_in_field( string a)
{
Field n = new Field();
n.StructureField_Identifier = a;
return n.StructureField_Identifier;
}
现在确认在
$ b $添加断点b
引用:返回n.StructureField_Identifier;
将鼠标悬停在
引用:n.StructureField_Identifier = a;
你会看到价值被分配。
你的问题听起来像是一个运行时异常(这意味着在运行时没有分配值),但是正好与编译时异常相反。
所以你不能继续,你需要解决它。
谢谢,
- Rahul
使用this .StructureField_Identifier = a;
而不是
n.StructureField_Identifier =一个; 击>
public class Field
{
public string StructureField_Identifier { get; set; }
public string func_in_field(string a)
{
Field n = new Field();
n.StructureField_Identifier = a;
return n;
}
}
// To pass the string "a" from function "func_in_field" to public string StructureField_Identifier, I am using n.StructureField_Identifier. But I am not able pass. How to pass the value from function to property.
Unless I'm not understanding you right, it should be:
public class Field { public string StructureField_Identifier { get; set; } public string func_in_field(string a) { StructureField_Identifier = a; //Assigns a to the property in this instance. return a; //???? } }
Hello,
Ron Sir's post is correct
Firstly,
public class Field { public string StructureField_Identifier { get; set; } public string func_in_field(string a) { Field n = new Field(); n.StructureField_Identifier = a; return n; } }
The return type of your method "func_in_field" is a string and the type you have used to return is of "Field"
So this code wont compile.
The error it gives is :
Quote:cant convert type Field to type string
To make it compile do this :
public string func_in_field(string a) { Field n = new Field(); n.StructureField_Identifier = a; return n.StructureField_Identifier; }
Now to confirm add a a breakpoint at
Quote:return n.StructureField_Identifier;
Hover your mouse to
Quote:n.StructureField_Identifier = a;
And you will see that the value is assigned.
Your question is sounding like it is a run time exception(Which means value is not getting assigned at run time), but it is exactly opposite you are getting a compile time exception.
So you cant go ahead, you will need to solve it.
Thanks,
- Rahul
usethis.StructureField_Identifier = a;
instead of
n.StructureField_Identifier = a;
这篇关于如何将值从函数传递给属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!