Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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.
 
How can one access the items inside the array,supposing i wanted to assign 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"));
 
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 invoker in this case ========================================
Do this statement invoke get call ListBox1.DisplayMember = "LongName"; ListBox1.ValueMember = "ShortName";
 
==============================================
Collapse | Copy Code
 
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
==============================================================
Collapse | Copy Code
 
// 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 24-Jan-13 0:13am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

This May Help You
 
USState obj = (USState)USStates[0];
string longname= obj.LongName;
string shortname= obj.ShortName;
 
//if u create List instead of ArrayList
 
 
List<usstate> USStates=new List<usstate>();
string longname=USStates[0].LongName;
string shortname=USStates[0].ShortName;
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi Sumit,
 
First of all there's only get in the code above (did I miss the set?)
And regarding the get itself:
 
When you access the property for reading the get method is invoked and the code runs inside until it reached the return keyword.
Same goes for set, if you try to write a value to the property.
 
see get&set in a msdn [^] article.
 
To edit a specific cell of the ArrayList USStates, simply use:
USStates[0] = new USState("MyInventedState", "IS");
and for example to work with it:
USState state = USSTates[0];
string longName = state.LongName; 
 

Cheers,
Edo
  Permalink  
v3
Comments
Sumit Lotankar at 28-Jan-13 4:14am
   
How can i excess the array USStates. ex: First element of array USStates(0) so to assign it to some variable
Edo Tzumer at 28-Jan-13 5:06am
   
Simple, I've added it to the solution, see above
Sumit Lotankar at 28-Jan-13 6:20am
   
Sorry Edo for trouble ,but i want is that the variable s to assigned to one of the element in the array Ex:- varibale s to be assigned the value "Alabama" using USSates[0] reference , // string s = USStates[1].ToString(); dosent work
Edo Tzumer at 28-Jan-13 7:16am
   
USStates[1] is a USState, and thus doing USStates[1].ToString() will not get you the name. That means that if you want to do stuff with a USState you can declare it like this: USState state = USSTates[0]; string longName = state.LongName; Cheers, Edo

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 895
1 OriginalGriff 523
2 thatraja 245
3 Abhinav S 243
4 Emre Ataseven 200
0 Sergey Alexandrovich Kryukov 8,142
1 OriginalGriff 4,665
2 Peter Leow 3,774
3 Maciej Los 3,515
4 Er. Puneet Goel 3,107


Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 28 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid