Is there a better way to pass variables into a class?
public class Truck
{
private int _wheels;
private string _color;
private bool _loaded;
public int wheels { get { return _wheels; } }
public string color { get { return _color; } }
public bool loaded { get { return _loaded; } }
public Truck(int wheels, string color, bool loaded)
{
this._wheels = wheels;
this._color = color;
this._loaded = loaded;
}
}
Truck dumpTruck = new Truck(6, "yellow", false);
I have learned C# by error code and there has got to be a better way to do this. I am stuck on Framework 2.0.
3 Answers 3
Depending on your implementation and needs of this program you may want to look into the
Color
struct
it represents colors and there is aColors
class
which has many predefined colors created already. This would be usedful if your program is a visual program and you have to render the specified color on screen. Otherwise astring
is just fine.To elaborate more on Jeff Vanzella's answer when he said
As for your question of having a better way: the only other way to set them is to make the property's set method public and set them through that.
This is of-course changing your program in a way that you really may not want, but it is good to know about.
public class Truck { public int Wheels { get; set; } public string Color { get; set; } public bool Loaded { get; set; } }
...Somewhere else, when declaring Truck
var truck = new Truck() { Wheels = 4, Color = "Blue", Loaded = true };
or
var truck = new Truck(); truck.Wheels = 4; truck.Color = "Blue"; truck.Loaded = true;
the above two method do the exact same thing, the former is just shorthand for the latter
Edit: Added point #3
- You don't need to say
this._wheels = wheels
, in your case there are no variables in your constructor that conflict with_wheels
so you can just say_wheels = wheels;
In-fact that is partially the reasonprivate
class
scoped variables are often named with an underscore prefix. In some conventions, that underscore prefix isn't used, so theclass
scopedprivate
members are named something likewheels
, and then in the constructor you do have to saythis.wheels = wheels;
, the former referring to theclass
levelwheels
, and the latter referring to the constructor levelwheels
. This is confusing when people do that, which is why I prefer to do it the way you did it, and have all my privateclass
level members prefixed with an underscore. Note that I did specifyprivate
, if a variable ispublic
is should be Capitalized, with no underscores in the beginning(preferably none in the rest of the name), and a property, meaning it has the{get; set;}
or the more lengthy getters and setters you used in the OP.
-
\$\begingroup\$ @SamLeach It will show up with IntelliSense, however if the
set
accessor is private, it will result in a compile error. \$\endgroup\$BenVlodgi– BenVlodgi2014年03月13日 20:14:50 +00:00Commented Mar 13, 2014 at 20:14 -
\$\begingroup\$ Using underscores in variable names is actually against the recommended guidelines set forth by Microsoft for variable naming in .NET. MSDN. \$\endgroup\$Bradford Dillon– Bradford Dillon2016年12月18日 05:36:36 +00:00Commented Dec 18, 2016 at 5:36
A few observations.
Properties should be PascalCase.
public class Truck { // ... public int Wheels { get {return _wheels; } } // ... }
The
this
keyword is not needed here. by having the '_' in front of your class variables, you are differentiating them from the local variables.
Now, to answer your question, you could use Automatic Properties, which would change your class to be something like:
public class Truck
{
public int Wheels { get; private set; }
public string Color { get; private set; }
public bool Loaded { get; private set; }
public Truck(int wheels, string color, bool loaded)
{
Wheels = wheels;
Color = color;
Loaded = loaded;
}
}
As for your question of having a better way: the only other way to set them is to make the property's set
method public and set them through that. Personally, I like passing them through the constructor better, it makes it easier to make the the class immutable.
-
2\$\begingroup\$ ThisIsPascalCase and thisIsCamelCase. \$\endgroup\$Magus– Magus2014年03月13日 19:08:25 +00:00Commented Mar 13, 2014 at 19:08
-
\$\begingroup\$ Thanks @Magus I get the names confused regularly :) Edit made \$\endgroup\$Jeff Vanzella– Jeff Vanzella2014年03月13日 19:11:19 +00:00Commented Mar 13, 2014 at 19:11
-
\$\begingroup\$
public int Wheels { get; private set; }
throws...Truck.Wheels.get' must declare a body because it is not marked abstract or extern
\$\endgroup\$iambriansreed– iambriansreed2014年03月13日 20:49:54 +00:00Commented Mar 13, 2014 at 20:49 -
\$\begingroup\$ What are you trying to do @iambriansreed? \$\endgroup\$Jeff Vanzella– Jeff Vanzella2014年03月13日 21:15:03 +00:00Commented Mar 13, 2014 at 21:15
Maybe the word "better" needs to be elaborated.
As it was suggested - public setter will help make it so that you can reuse the object. For a stand alone class - there is not much you can add - if it had dependencies then it would have been another matter and also how you retrieve the parameters.
That's when having less parameters receiving by the constructor will be important as for example wheel can be its own class with its own dimension (like rim size and number).
But Jeff's solution of at least putting the public setter available should be sufficient for your needs for now.
Truck
class. What is it that you have a problem with? \$\endgroup\$Truck
andCar
and want to be able to assign them both in a loop, or that you want to be able to make the callTruck dumpTruck = (6, "yellow", false)
? In the second case, you're out of luck, but you can at least reduce it tovar dumpTruck = new Truck(6, "yellow", false)
. The main reason for this is that you could also have aclass UltraTruck : Truck { }
and use it instead. The compiler can't tell the difference without a name. \$\endgroup\$