Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: 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 19:52pm
Edited 22-Jan-13 20:28pm
v2
Comments
austinbox at 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 at 23-Jan-13 1:04am
   
That is a good point, my 5.
—SA
Sergey Alexandrovich Kryukov at 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 at 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 at 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 at 23-Jan-13 1:03am
   
Agree a 5.
Sergey Alexandrovich Kryukov at 23-Jan-13 1:10am
   
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 at 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
0 OriginalGriff 195
1 ProgramFOX 130
2 Maciej Los 105
3 Sergey Alexandrovich Kryukov 105
4 Afzaal Ahmad Zeeshan 82
0 OriginalGriff 6,564
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 23 Jan 2013
Copyright © CodeProject, 1999-2014
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