Tranferring parameters to structs in a class?

O

ORC

How to transfer a parameter to a structure in a class?

In a class (MySubClass) I have defined a private structure (I don't want it
to be public) and a property {get; set} like this

private TestStruct
{
public int member_1;
public int member_2;
}

TestStruct TS = new TestStruct();

public TestStruct members
{
get
{
return TS;
}
set
{
TS = value;
}
}

In another class (MyParentClass) Doing this:

MySubClass tc = new MySubClass();
testVariable = tc.members.member_1;

works very well ! But doing this:

tc.members.member_1 = testVariable;

Doesn't work - the compiler raises an error saying:
"Cannot modify the return value of
'Application_for_tests.TestClass.variables' because it is not a variable"

So how is the correct way to do stuff like that?

Thanks
Ole
 
N

Nicholas Paldino [.NET/C# MVP]

ORC,

You would have to declare your structure as a class. If you do this:

tc.members.member_1 = testVariable;

Then the return value of members is a copy of the TS field that is
stored in the class instance. You will be modifying the field on the copy,
and not on the instance held by the class.

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top