5
\$\begingroup\$

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.

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Mar 13, 2014 at 18:43
\$\endgroup\$
4
  • 2
    \$\begingroup\$ This is a very effective way to make an immutable Truck class. What is it that you have a problem with? \$\endgroup\$ Commented Mar 13, 2014 at 18:49
  • \$\begingroup\$ @Magus When building classes I seem to repeat this method over and over again. Seems like there should be a shortcut when passing variables identical to the object's properties. \$\endgroup\$ Commented Mar 13, 2014 at 18:51
  • \$\begingroup\$ So, are you saying that you have both Truck and Car and want to be able to assign them both in a loop, or that you want to be able to make the call Truck dumpTruck = (6, "yellow", false)? In the second case, you're out of luck, but you can at least reduce it to var dumpTruck = new Truck(6, "yellow", false). The main reason for this is that you could also have a class UltraTruck : Truck { } and use it instead. The compiler can't tell the difference without a name. \$\endgroup\$ Commented Mar 13, 2014 at 19:12
  • \$\begingroup\$ C# version 6.0 may have Primary Constructors (inspired by F#) while will make such class initialization more idiomatic and easy to code. damieng.com/blog/2013/12/09/probable-c-6-0-features-illustrated \$\endgroup\$ Commented Mar 13, 2014 at 20:37

3 Answers 3

7
\$\begingroup\$
  1. Depending on your implementation and needs of this program you may want to look into the Color struct it represents colors and there is a Colors 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 a string is just fine.

  2. 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

  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 reason private class scoped variables are often named with an underscore prefix. In some conventions, that underscore prefix isn't used, so the class scoped private members are named something like wheels, and then in the constructor you do have to say this.wheels = wheels;, the former referring to the class level wheels, and the latter referring to the constructor level wheels. This is confusing when people do that, which is why I prefer to do it the way you did it, and have all my private class level members prefixed with an underscore. Note that I did specify private, if a variable is public 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.
answered Mar 13, 2014 at 19:20
\$\endgroup\$
2
  • \$\begingroup\$ @SamLeach It will show up with IntelliSense, however if the set accessor is private, it will result in a compile error. \$\endgroup\$ Commented 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\$ Commented Dec 18, 2016 at 5:36
10
\$\begingroup\$

A few observations.

  1. Properties should be PascalCase.

    public class Truck
    {
     // ...
     public int Wheels { get {return _wheels; } }
     // ...
    }
    
  2. 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.

answered Mar 13, 2014 at 18:53
\$\endgroup\$
4
  • 2
    \$\begingroup\$ ThisIsPascalCase and thisIsCamelCase. \$\endgroup\$ Commented Mar 13, 2014 at 19:08
  • \$\begingroup\$ Thanks @Magus I get the names confused regularly :) Edit made \$\endgroup\$ Commented 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\$ Commented Mar 13, 2014 at 20:49
  • \$\begingroup\$ What are you trying to do @iambriansreed? \$\endgroup\$ Commented Mar 13, 2014 at 21:15
2
\$\begingroup\$

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.

answered Mar 13, 2014 at 21:18
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.