CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2012
    Posts
    2

    C# Properties Question

    Hello, I am an intermediate Java programmer and I have some questions about Properties that I cannot find anywhere. From what I gather, they are similar to Java's getter and setter methods, but seem like they may be more powerful.

    Code:
    private string color; 
    
    public string Color
    {
        get 
        {
            return color.ToUpper(); 
        }
        set 
        { 
            if(value == "Red")
                color = value; 
            else
                Console.WriteLine("This car can only be red!");
        }
    }
    This allows you to set the private string color to any string. I am just curious about how you would call such a method? How you differentiate between setting and getting when you make the call. Where is the variable "value" coming from?

    Just some things that I would like to clarify before I move on. Thank you for any help.

  2. #2
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,452

    Re: C# Properties Question

    The properties are only available to instances of objects. So let's extend the code a bit.

    Code:
    public class Car
    {
    private string color; 
    
    public string Color
    {
        get 
        {
            return color.ToUpper(); 
        }
        set 
        { 
            if(value == "Red")
                color = value; 
            else
                Console.WriteLine("This car can only be red!");
        }
    }
    }
    Code:
    Car myNewCar = new Car();
    myNewCar.Color = "Red";    // This sets the value of the color property to red.  
    myNewCar.Color = "Blue";  //This displays the warning message on the console.
    Console.WriteLine(myNewCar.Color);  //  Displays "RED" on the console.
    The easiest way to explain would be, Properties allow you to get or set values for an object. They are for use in Assignment operations. You Set the color of the car, or you Get the color of the car. The Setting or Getting is determined by the type of assignment operation. IE.

    myNewCar.Color = "Red";

    will call the SET portion.

    The statment...

    string CarColor = myNewCar.Color;
    Will call the get portion.

    Methods are more for Actions. Car.Start(); would be a method.

    This is a very simplistic description.

    The variable value is always implied on the "Set" property;

  3. #3
    Join Date
    May 2012
    Posts
    2

    Re: C# Properties Question

    So value is the default variable that is passed to the method? That makes sense, since I don't see it declared anywhere else.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: C# Properties Question

    A couple of things:
    1) check out using auto-properties (no need to have a separate private variable with an auto-property)
    2) check out using enums (storing such a value as a string isn't very efficient, nor are each value typesafe).

    Code:
    public enum CarColor { Red, Green, Blue };
    
    public class Car
    {
    
      // Auto-property (with a private setter)
      public CarColor Color { get; private set; }
    
      // ctor
      public Car( CarColor color )
      {
        Color = color;  // sets the initial color
      }
    }
    Usage:
    Code:
    var car = new Car( CarColor.Red);
    Console.WriteLine( car.Color ); // prints Red
    
    car.Color = CarColor.Blue; // compiler error - can't set the private setter from here

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured