OOP Question

J

Joe Cool

I have never had any formal training in Object Oriented Programming, I
have had to learn it by studying other people's code. I think I have a
pretty good understanding of it now, but I still occasionally see
something that has me curious.

There are two basic ways to initialize class properties to working
values.

One way is to:

public class myClass
{
public string aString { get; set; }

public myClass(string astring)
{
this.aString = astring;
}
}

later in some other class:

myClass myclass = new myClass("value to set");


Alternatively, there is:

public class myClass
{
public string aString { get; set; }
}

later:

myClass myclass = new myClass();
myclass.aString = "value to set";

Either way works just fine. What I am wondering, which is the "right"
way according to the definition of OOP? Or is there even a "right"
way?

Or is it just a matter of personal preference?
 
M

Marcel Overweel

Joe Cool said:
I have never had any formal training in Object Oriented Programming, I
have had to learn it by studying other people's code. I think I have a
pretty good understanding of it now, but I still occasionally see
something that has me curious.

There are two basic ways to initialize class properties to working
values.

One way is to:

public class myClass
{
public string aString { get; set; }

public myClass(string astring)
{
this.aString = astring;
}
}

later in some other class:

myClass myclass = new myClass("value to set");


Alternatively, there is:

public class myClass
{
public string aString { get; set; }
}

later:

myClass myclass = new myClass();
myclass.aString = "value to set";

Either way works just fine. What I am wondering, which is the "right"
way according to the definition of OOP? Or is there even a "right"
way?

Or is it just a matter of personal preference?

Hi,

I would use a constructor with a parameter if the class itself
isn't able to function without a default value, or when there is no
clear default value.

If the value is something that will almost always be "something",
you could better set it to "something" in the constructor and
use the second option.

public class myClass
{
public string aString { get; set; }

public myClass() // no parameters in the constructor
{
aString = "something"; // set a default value
}
}

Suppose you have a class called 'Car'.
This class has two properties called 'WheelCount' and 'PaintColor'.
In this case, I would use a constructor like this:

public Car(Color paintColor)
{
this.WheelCount = 4; // set a default, this value will most likely be a 4.
this.PaintColor = paintColor; // who knows, a color is a matter of taste.
}

Now, if 'Car' would be used in your program only, and you know
you will always set the property 'WheelCount' (for example when
you are creating cars defined in a parameter list coming from a
database) there is no use in setting a default value because that
would be a waste of cpu cycles.

So it depends on the situation.

regards,
Marcel
 
J

J.B. Moreno

Marcel Overweel said:
"Joe Cool" <[email protected]> schreef in bericht -snip-
-snip constructor w/o parameter and setting of public property-
Hi,

I would use a constructor with a parameter if the class itself
isn't able to function without a default value, or when there is no
clear default value.

I wouldn't consider default values relevant to the decision.

Two somewhat interrelated criteria should be used: whether the class
can function without it (do any of the methods or properties do
something useful without this value being set), and is the resulting
instance usefully unique.
 

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