Skip to main content
gamedev.net
🔒 Locked

[.net] C# language problems

Started by Endar Feb 17, 2007 at 11:13 PM 7 replies 1.2k views
Original Post
Endar
Endar
I've got 2 small problems relating to C#. First of all: if I have an accessor and a settor (is that what they're called?) for a public member variable of a class, and there is nothing special done, does that mean that I shouldn't bother with the accessor and settor? What if I would like to return a reference to the member? Second: I'm attempting to cycle through an List<> and I'm accessing the different elements of the list using an 'indexer' (fancy name now, huh?). I need to get a reference from the indexer that points to the original object. Is there a way to do this?
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
Nypyren
Nypyren
First: Those are called 'properties', and it's OK to have them even if they don't do anything special - you can add special functionality later without having to change outside code.

Second: If the items in the list are 'reference types' (class, string) then any time you return them or say "a = b" with them, you're copying the reference to the original object. If the items in the list are 'value types' (struct, float, enum, int, any other numeric type), then you cannot return a reference to it and should probably return either a Pair list,int index> or just the index, or think up some other clever mechanism.
TheTroll
TheTroll
I believe with properties you should put in error checking when ever possible into the "setter" part. This makes sure that the value going into the class is a valid one. I am not a big fan of properties that don't do anthing but access private member variables.

theTroll
Endar
Endar
Quote:
Original post by Nypyren
Second: If the items in the list are 'reference types' (class, string) then any time you return them or say "a = b" with them, you're copying the reference to the original object. If the items in the list are 'value types' (struct, float, enum, int, any other numeric type), then you cannot return a reference to it and should probably return either a Pair list,int index> or just the index, or think up some other clever mechanism.


class A{ /* stuff */ }// in a functionA aobj = new A();A bobj = aobj;


So, the second, 'bobj' is a reference for 'aobj'?
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
ToohrVyk
ToohrVyk
Quote:
Original post by Endar
class A{ /* stuff */ }// in a functionA aobj = new A();A bobj = aobj;


So, the second, 'bobj' is a reference for 'aobj'?


Yes. Had you written struct A, it would be a copy.
Endar
Endar
Quote:
Original post by ToohrVyk
Quote:
Original post by Endar
class A{ /* stuff */ }// in a functionA aobj = new A();A bobj = aobj;


So, the second, 'bobj' is a reference for 'aobj'?


Yes. Had you written struct A, it would be a copy.


Aha!! Things are starting to make sense!!

Thanks, guys.
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
gharen2
gharen2
Quote:
Original post by TheTroll
I believe with properties you should put in error checking when ever possible into the "setter" part. This makes sure that the value going into the class is a valid one. I am not a big fan of properties that don't do anthing but access private member variables.

theTroll


There's a couple good reasons to create properties that do nothing but access private member variables.

1) The accessors are simple enough that the compiler will just inline them anyways.

2) As has been mentioned, you may need add code to the properties later on without changing the interface.
TheTroll
TheTroll
[rant]
The reason I say that, is I believe (this is my own little pet peeve) that properties should ALWAYS check for valid values. This prevents all those little problems that people have with getting invalid data. Does a null break your class? Does the value have to be a postive int? Can the string be too long? These are all things that you should be asking yourself and the properties give you a great place to make sure that your class will ALWAYS have good data.
[/rant]

theTroll
orbano
orbano
Quote:
Original post by TheTroll
[rant]
These are all things that you should be asking yourself and the properties give you a great place to make sure that your class will ALWAYS have good data.
[/rant]

theTroll


sometimes its really needless. provided you variables covers the domain of the type used, you dont have anything to check :)
"Knowledge is no more expensive than ignorance, and at least as satisfying." -Barrin

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.