Skip to main content
Code Review

Return to Answer

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link
  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 Jeff Vanzella's answer 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.

  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.

  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.

added point #3
Source Link
BenVlodgi
  • 4.3k
  • 2
  • 21
  • 47
  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 privateclass 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.

  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

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

Source Link
BenVlodgi
  • 4.3k
  • 2
  • 21
  • 47
  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

lang-cs

AltStyle によって変換されたページ (->オリジナル) /