Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I am a beginner.This is the code a part of code from msdn,there is a class USState which i have add at the end.

Please Explain this part of code as it instance a new object of class USState and
passing the parameters,How does the get and set accesors get invoked in this sequence
.
C#
USStates.Add(new USState("Alabama", "AL"));

USState constructor gets invoked and this parameters are passed.
==============================================
C#
private string myShortName;;//relationship between this.myShortName 
        private string myLongName;

        public USState(string strLongName, string strShortName)
        {

            this.myShortName = strShortName;//relationship between this.myShortName 
            this.myLongName = strLongName;
        }

what is the // relationship between this.myShortName comments in bold with get and set accesors
==============================================================
C#
   // Populate the list box using an array as DataSource.
            ArrayList USStates = new ArrayList();
            USStates.Add(new USState("Alabama", "AL"));
            USStates.Add(new USState("Washington", "WA"));
            USStates.Add(new USState("West Virginia", "WV"));
            USStates.Add(new USState("Wisconsin", "WI"));
            USStates.Add(new USState("Wyoming", "WY"));
            ListBox1.DataSource = USStates;

            // Set the long name as the property to be displayed and the short
            // name as the value to be returned when a row is selected.  Here
            // these are properties; if we were binding to a database table or
            // query these could be column names.
            ListBox1.DisplayMember = "LongName";
            ListBox1.ValueMember = "ShortName";

public class USState
    {
        private string myShortName;
        private string myLongName;

        public USState(string strLongName, string strShortName)
        {

            this.myShortName = strShortName;
            this.myLongName = strLongName;
        }

        public string ShortName
        {
            get
            {
                return myShortName;
            }
        }

        public string LongName
        {

            get
            {
                return myLongName;
            }
        }

    }
}
Posted
Updated 22-Jan-13 19:28pm
v2
Comments
austinbox 23-Jan-13 1:00am    
If the Properties LongName and ShortName are intended to be read-only just use

public string MyProperty { get; private set; }

That way the property can only be changed inside the class
And don't forget to change your constructor
Sergey Alexandrovich Kryukov 23-Jan-13 1:04am    
That is a good point, my 5.
—SA
Sergey Alexandrovich Kryukov 23-Jan-13 1:09am    
I added explanation about auto-backed properties and of course credited your comment.
Than you very much.
—SA
Sergey Alexandrovich Kryukov 23-Jan-13 1:03am    
All correct. How can it cause any doubt. I tried to explain it, but you can simply read about it.
Don't forget that you should use "internal" instead of "public" if you don't need accessibility from outside or your assembly.
—SA
Sumit Lotankar 23-Jan-13 23:23pm    
How can one access the items inside the array,supposing i wanted to assing
the the first item in the array list to some variable or textbox ,
USStates[0] dosent work
// Populate the list box using an array as DataSource.
ArrayList USStates = new ArrayList();
USStates.Add(new USState("Alabama", "AL"));

Getter are called when you read the value of the property:
C#
USState state = new USState("Washington", "WA");

//...

string whereAmI = state.LongName; // calculation of the value calls getter

And setter is called when you assign a value to some property. You did not define any setters.

The whole idea of properties is the possibility to introduce some side effect behind the operations which as syntactically looks 100% identically to operations of reading or assignment of a field value.

[EDIT]

Please see also the comment by austinbox.

Your backing fields are redundant. In newer syntax, auto-implemented backing fields were introduced. This is all you need:

C#
public class USState
    {

        public USState(string longName, string shortName)
        {
            this.ShortName = shortName;
            this.LongName = longName;
        }

        public string ShortName { get; } // backing field is implied 
        public string LongName { get; } // backing field is implied

    }
}


—SA
 
Share this answer
 
v2
Comments
Abhinav S 23-Jan-13 1:03am    
Agree a 5.
Sergey Alexandrovich Kryukov 23-Jan-13 1:10am    
Thank you, Abhinav.
—SA
There are two parts in this code.

1) There are no set accessors in either of the two properties. The two variables are assigned values in the constructor.

2) These properties are then accessed from outside the class and these values are returned to the calling class during binding
C#
ListBox1.DisplayMember = "LongName";
ListBox1.ValueMember = "ShortName";
 
Share this answer
 
v2
Comments
Sumit Lotankar 23-Jan-13 23:30pm    
When is the get method geting invoked in this case,
as in other cases when
USState state = new USState("Washington", "WA");

//...

string whereAmI = state.LongName; // calculation of the value calls getter

state.LongName is in invoker in this case
========================================
Do this statement invoke get call
ListBox1.DisplayMember = "LongName";
ListBox1.ValueMember = "ShortName";

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900