Click here to Skip to main content
15,867,771 members
Please Sign up or sign in to vote.
4.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.

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

This May Help You

C#
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;
 
Share this answer
 
v3
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:
C#
USStates[0] = new USState("MyInventedState", "IS");

and for example to work with it:
C#
USState state = USSTates[0];
string longName = state.LongName; 



Cheers,
Edo
 
Share this answer
 
v3
Comments
Sumit Lotankar 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
Joezer BH 28-Jan-13 5:06am    
Simple, I've added it to the solution, see above
Sumit Lotankar 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
Joezer BH 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)



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