Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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.

Update: I probably should have mentioned I am stuck on Framework 2.0. SHarepoInT Happens.

share|improve this question
1  
This is a very effective way to make an immutable Truck class. What is it that you have a problem with? –  Magus Mar 13 at 18:49
    
@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. –  iambriansreed Mar 13 at 18:51
    
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. –  Magus Mar 13 at 19:12
    
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 –  hocho Mar 13 at 20:37

3 Answers 3

up vote 7 down vote accepted
  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.
share|improve this answer
    
@SamLeach It will show up with IntelliSense, however if the set accessor is private, it will result in a compile error. –  BenVlodgi Mar 13 at 20:14

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.

share|improve this answer
2  
ThisIsPascalCase and thisIsCamelCase. –  Magus Mar 13 at 19:08
    
Thanks @Magus I get the names confused regularly :) Edit made –  Jeff Vanzella Mar 13 at 19:11
    
public int Wheels { get; private set; } throws ...Truck.Wheels.get' must declare a body because it is not marked abstract or extern –  iambriansreed Mar 13 at 20:49
    
What are you trying to do @iambriansreed? –  Jeff Vanzella Mar 13 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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.