Click here to Skip to main content
12,394,573 members (83,818 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# C#4.0
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
.
USStates.Add(new USState("Alabama", "AL"));
USState constructor gets invoked and this parameters are passed.
==============================================
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
==============================================================
   // 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 22-Jan-13 18:52pm
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
   
That is a good point, my 5.
—SA
   
I added explanation about auto-backed properties and of course credited your comment.
Than you very much.
—SA
   
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"));
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Getter are called when you read the value of the property:
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:

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
  Permalink  
v2
Comments
Abhinav S 23-Jan-13 1:03am
   
Agree a 5.
   
Thank you, Abhinav.
—SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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
ListBox1.DisplayMember = "LongName";
ListBox1.ValueMember = "ShortName";
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160721.1 | Last Updated 23 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100