object initialization
-
I have a class myclass{public int a;} and a string str="a". I want to set myclassobject.a=1 as myclassobject.str=1.How is it possible? } bidisha
-
I have a class myclass{public int a;} and a string str="a". I want to set myclassobject.a=1 as myclassobject.str=1.How is it possible? } bidisha
Your class is:
class myclass
{
public int a;
}There is no definition of str in your class. You cannot set
myclassobject.str = 1;
What is it you actually want to do? To what purpose do you want to setstr=1
in a class that contains no definition ofstr
? Also, on a matter of coding guides, you should not make fields public like this. You should encapsulate them in a property. ColinMackay.net "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell -
Your class is:
class myclass
{
public int a;
}There is no definition of str in your class. You cannot set
myclassobject.str = 1;
What is it you actually want to do? To what purpose do you want to setstr=1
in a class that contains no definition ofstr
? Also, on a matter of coding guides, you should not make fields public like this. You should encapsulate them in a property. ColinMackay.net "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'DonnellActually I have a xsd file from which I autogenerate a .cs file containing all class definations.I need to change my xsd often,so the members of the class often changes.And I want to initialise the members at run time whose name I can specify through a string str.suppose I set str="call".Now I want to set myobject.call=value.Please help. bidisha
-
Actually I have a xsd file from which I autogenerate a .cs file containing all class definations.I need to change my xsd often,so the members of the class often changes.And I want to initialise the members at run time whose name I can specify through a string str.suppose I set str="call".Now I want to set myobject.call=value.Please help. bidisha
Then you need to have a look at some of the stuff in the System.Reflection namespace. Start with
typeof(MyClass)
which returns aType
object and from there you can enumerate all sorts of things like the properties in a class, you can then invoke them in order to set the value of the property and so on. ColinMackay.net "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell -
Then you need to have a look at some of the stuff in the System.Reflection namespace. Start with
typeof(MyClass)
which returns aType
object and from there you can enumerate all sorts of things like the properties in a class, you can then invoke them in order to set the value of the property and so on. ColinMackay.net "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'DonnellI have used FieldInfo.setValue to set the fields where FieldInfo.Name=my_specified_name. I have no problem with setting the field value with primitive datatype such as string or int.But some field are of user defined datatype.For example, I have a field as Call mycall; where public class Call { string calltime; string callvalue; } I cant reach to mycall.calltime using FieldInfo. bidisha