将数组值分配给某个变量 [英] Assign array value to some variable
本文介绍了将数组值分配给某个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
数组USStates填充了显示的值,但如果我想要
来访问数组中的这个值,它是如何构成的。
通过USStates(1) )剂量工作。
USStates.Add(新USState(Alabama,AL));
< pre lang =cs> //使用数组作为DataSource填充列表框。
< b> ArrayList USStates = new ArrayList();< / b>
< b> USStates.Add(new USState(& quot; Alabama& quot;,& quot; AL& quot;))< / b> ;;
USStates.Add(new USState(& quot; Washington& quot;,& quot; WA& quot;));
USStates.Add(new USState(& quot; West Virginia& quot;,& quot; WV& quot;));
USStates.Add(new USState(& quot; Wisconsin& quot;,& quot; WI& quot;));
USStates.Add(new USState(& quot; Wyoming& quot;,& quot; WY& quot;));
ListBox1.DataSource = USStates;
//将长名称设置为要显示的属性,将短
//名称设置为选择行时要返回的值。这里
//这些是属性;如果我们绑定到数据库表或
//查询,这些可能是列名。
ListBox1.DisplayMember =& quot; LongName& quot ;;
ListBox1.ValueMember =& quot; ShortName& quot ;;
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;
}
}
公共字符串LongName
{
get
{
return myLongName;
}
}
}
}< / pre>
解决方案
请检索您的数据,如下所示。您必须将特定索引值强制转换为UsState类。
ArrayList arrayList = new ArrayList();
var usStateItem = new UsState( 我们, Usa);
arrayList.Add(usStateItem);
UsState usState =(UsState)arrayList [ 0 ];
希望这有帮助
您可以使用[]
运算符来访问ArrayList
项目,例如
ArrayList al = new ArrayList();
al.Add( foo);
al.Add( boo);
al.Add( goo);
Console.WriteLine( 项目1为{0},al [ 1 跨度>]);
al [ 1 ] = hoo 跨度>;
Console.WriteLine( 现在项目1为{0},al [ 1 跨度>]);
请考虑使用List< string>< / string>
而不是ArrayList
。
USState obj =(USState)USStates [0];
string longname = obj.LongName;
string shortname = obj.ShortName;
//如果你创建列表而不是ArrayList
列表< USState> USStates = new List< USState>();
string longname = USStates [ 0 ]。LongName;
string shortname = USStates [ 0 ]。ShortName;
Array USStates is populated with the values as shown but if i want
to access this values within the array, how is it posssible.
Refrencing through USStates(1) dosent work.
USStates.Add(new USState("Alabama", "AL")); <pre lang="cs">// Populate the list box using an array as DataSource. <b> ArrayList USStates = new ArrayList();</b> <b> USStates.Add(new USState("Alabama", "AL"))</b>; 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; } } } }</pre>
解决方案
Please retrieve your data like below.You must cast the particular index value to UsState class.
ArrayList arrayList=new ArrayList(); var usStateItem=new UsState("Us","Usa"); arrayList.Add(usStateItem); UsState usState = (UsState)arrayList[0];
Hope this helps
You may use the[]
operator to accessArrayList
items, e.g.
ArrayList al=new ArrayList(); al.Add("foo"); al.Add("boo"); al.Add("goo"); Console.WriteLine("Item 1 is {0}", al[1]); al[1] = "hoo"; Console.WriteLine("Now item 1 is {0}", al[1]);
Please consider usingList<string></string>
instead of theArrayList
.
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;
这篇关于将数组值分配给某个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文