Page 1 of 1

C# Noob Question

Posted: Sat Feb 14, 2009 1:10 pm
by TroyMac1ure
I'm no noob at programming, but Windows is new to me. I'm trying to fix some bugs in a program & I have the following lines:

Code: Select all

MetaSplitter.SplitIdent id = (MetaSplitter.SplitIdent) i;
id.offset += reflex.translation + (reflex.chunksize * x);
Now "id" is set to "i", but when "id.offset" gets changed, so does "i.offset"
How can I seperate the two so that "i" isn't affected by changes to "id"?

Thanks,
Troy

Re: C# Noob Question

Posted: Sat Feb 14, 2009 1:30 pm
by JacksonCougAr
Create a new object for it, your just referencing the same object here.

Re: C# Noob Question

Posted: Sat Feb 14, 2009 1:41 pm
by OwnZ joO
Jackson is correct.
Make a new MetaSplitter.SplitIdent object.
I don't know all the properties of split ident, but you should do it something like this.

Code: Select all

MetaSplitter.SplitIdent temp = (MetaSplitter.SplitIdent)i;
MetaSplitter.SplitIdent id = new MetaSplitter.SplitIdent();
id.offset = temp.offset + (reflex.chunksize * x);
.
.   // fill in other properties of SplitIdent
.

Re: C# Noob Question

Posted: Sun Feb 15, 2009 4:42 pm
by TroyMac1ure
Saying it has 10 variables contained in it, I would therefore have to go through and set each variable manually, correct?

i thought this may be the cae, so I created a new constructor that would take a metaSplitter object and the constructor would transfer each variable to the newly created metsplitter object.

Is this considered the proper way of doing it? I know how hard it is to break bad habits, so I want to learn the correct mathod from the start.

thanks

Re: C# Noob Question

Posted: Sun Feb 15, 2009 10:49 pm
by OwnZ joO
A SplitIdent constructor that takes a split ident? I would say that's a good enough way to do it. Also, any of the properties that are reference types(classes) you would need to do the same thing for, or else you will have the new one and old one pointing to the same stuff.

The only other way I could see doing it would be to have a private constructor that takes a SplitIdent, and have a public method Duplicate that calls the private constructor, but that's not in any way necessary, especially in entity.