Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Property question

Status
Not open for further replies.

cjburkha

Programmer
Jul 30, 2004
76
0
0
US
Hi, I'm just getting started with C#, so excuse this question

It's my understanding that

Code:
private string mnfParNum;

public string MnfParNum
{
	get{return mnfParNum;}
	set{mnfParNum = value;}
}
works just like
Code:
public string MnfParNum;

I know that the first way is preferred, and I half understand why.

My question is, do I really have to write 4 lines of code everytime I want to declare a public variable? There must be an easier way to do this. Something cute like
Code:
private property string mnfParNum
obviously that doesn't work, but something to save my carpul tunnel syndrom?

I heard some programmers get paid by the line, they must love this new construct huh?

I'm joking a little, but this is a serious question. I did search around for a while on google groups, and I found a lot of people talking about how the property is the preferd way to go with C#, but not a simpeler way to code it.

Thanks for your time,

CJB
 
There are two reasons why using properties is preferred over just using a public member variable:

1) If you need to perform some validation of a value that might get sent to your class, you can do it in a property, but not if the variable was declared as public.

2) You can declare it to be read-only or write-only by omitting the other part of the property declaraion.

I don't know of anyone being paid by the line. I like the idea, though...
:)

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Thanks for the help guys.

Here is my concern then. I have a class that represents an inventory item. I connect to a DB and get a lot of properties for this inventory item, maybe 30 properties. If I make them all properties that is going to be a huge class for not much info. I am tempted to just make one property, and that property would be the data set. Not very cool, but what do you think?
 
Hey there, if I may contribute something here (fresh off the C# exam and still remembering way too many Microsoft definitions) ...

I think your first example is demonstrating a property where, as Chip says, can consist of code to perform some validation, and the second is called a 'field' of a class. Fields simply exposed access to a value.

As far as your thought of having one property that returns a dataset - that's potentially a valid design, but depending on the overall scope of the project, you might break some rules of encapsulation going this route. If you choose to return a dataset, you're assuming that everyone that will use your class will know how to manipulate a dataset (which is fine if they do, just something to consider) - but if you go the more code intensive route within the class and expose a bunch of properties or fields, you can have a relatively inexperienced programmer create an instance of your class, retrieve values, then trash it.


______________________________________________
When told, "goto hell", a programmer finds the method, not the destination as harmful.
 
Your right, of course, returning the dataset is not a very smart way to go.

It's just that if I have 30 properties to expose, thats 120 lines of very repetitive code. I totally understand why properties are the way to go, thank you all for that, but I wish I could find a faster way to code it.

Maybe I can check and if I add a propertie to my class, VS will add the code form me.

Well, I guess if there are no shortcuts I'll just bite the bullet and do it.

Thanks again everyone

CJB
 
only 120 lines of code? You can do that in no time. ;-)

Best of luck to you.

______________________________________________
When told, "goto hell", a programmer finds the method, not the destination as harmful.
 
If I make them all properties that is going to be a huge class for not much info
What I've done when I don't want to type all that stuff in is use or write a utility to take a dataset and generate a class that has all my properties in it.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top