C# 访问列表中的元素<&燃气轮机;结构类型

C# 访问列表中的元素<&燃气轮机;结构类型,c#,list,binding,combobox,struct,C#,List,Binding,Combobox,Struct,有一点背景,这始于问题: 我已经设法创建了结构状态,并创建了列表。最初这是一个多维数组,其目的是将0-index作为组合框中的一个项,然后将idex 1-3显示在3个文本框中。我现在的问题是,如何使用struct/List组合复制此功能。结构包含与原始数组相同的4个字段,但我在理解如何指定哪个字段指向哪个框时遇到了问题。从我在别处读到的内容来看,这应该只是使用struct.field格式的问题,但它似乎不起作用。像往常一样,这可能是我错过的一些简单的东西,但我似乎不能把我的头围绕在它周围 另一方

有一点背景,这始于问题:

我已经设法创建了
结构状态
,并创建了
列表
。最初这是一个多维数组,其目的是将0-index作为组合框中的一个项,然后将idex 1-3显示在3个文本框中。我现在的问题是,如何使用struct/List组合复制此功能。结构包含与原始数组相同的4个字段,但我在理解如何指定哪个字段指向哪个框时遇到了问题。从我在别处读到的内容来看,这应该只是使用
struct.field
格式的问题,但它似乎不起作用。像往常一样,这可能是我错过的一些简单的东西,但我似乎不能把我的头围绕在它周围

另一方面,这个列表是完全不可变的,这些信息都不会改变(如果改变了,它将处于代码级别,我将自己重做,但用户输入永远不会影响它)

任何帮助都将不胜感激

结构:

struct State
{
    private String StateID;
    private String TimeZone;
    private String CureType;
    private int CureTimeframe;

    public State(string stateID, string timeZone, string cureType, int cureTimeframe)
    {
        StateID = stateID;
        TimeZone = timeZone;
        CureType = cureType;
        CureTimeframe = cureTimeframe;
    }

    public override string ToString()
    {
        string data = String.Format("{0} {1} {2} {3}", StateID, TimeZone, CureType, CureTimeframe);
        return data;
    }
}
class StatesAndTerritories
{
    public List<State> stateData = new List<State>();
    public StatesAndTerritories ()
    {
        stateData.Add(new State("AL", "CST", "None", 0));
        stateData.Add(new State("AK", "AST", "COD", 10));
        stateData.Add(new State("AR", "CST", "COD", 10));
        stateData.Add(new State("AZ", "MST", "COD", 10));
        stateData.Add(new State("CA", "PST", "COD/Mandatory", 10));
        stateData.Add(new State("CO", "MST", "Mandatory", 20));
        stateData.Add(new State("CT", "EST", "Mandatory", 10));
        stateData.Add(new State("DC", "EST", "Mandatory", 10));
        stateData.Add(new State("DE", "EST", "None", 0));
        stateData.Add(new State("FL", "EST", "COD", 10));
        stateData.Add(new State("GA", "EST", "COD", 10));
        stateData.Add(new State("HI", "HST", "None", 0));
        stateData.Add(new State("IA", "CST", "Mandatory", 20));
        stateData.Add(new State("ID", "MST", "None", 0));
        stateData.Add(new State("IL", "CST", "COD", 10));
        stateData.Add(new State("IN", "EST", "None", 0)); 
        stateData.Add(new State("KS", "CST", "Mandatory", 20));
        stateData.Add(new State("KY", "EST", "None", 0));
        stateData.Add(new State("LA", "CST", "Replevin", 0));
        stateData.Add(new State("MA", "EST", "Mandatory", 21));
        stateData.Add(new State("MD", "EST", "Mandatory", 10));
        stateData.Add(new State("ME", "EST", "Mandatory", 19));
        stateData.Add(new State("MI", "EST", "COD/Mandatory", 30));
        stateData.Add(new State("MN", "CST", "COD", 10));
        stateData.Add(new State("MO", "CST", "Mandatory", 20));
        stateData.Add(new State("MS", "CST", "None", 0)); 
        stateData.Add(new State("MT", "MST", "COD", 10)); 
        stateData.Add(new State("NC", "EST", "None", 0));
        stateData.Add(new State("ND", "CST", "None", 0));
        stateData.Add(new State("NE", "CST", "Mandatory", 20));
        stateData.Add(new State("NH", "EST", "None", 0));
        stateData.Add(new State("NJ", "EST", "COD", 10)); 
        stateData.Add(new State("NM", "MST", "COD", 10));
        stateData.Add(new State("NV", "PST", "COD", 10));
        stateData.Add(new State("NY", "EST", "None", 0));
        stateData.Add(new State("OH", "EST", "COD", 10)); 
        stateData.Add(new State("OK", "CST", "COD", 10));
        stateData.Add(new State("OR", "PST", "COD", 10));
        stateData.Add(new State("PA", "EST", "None", 0));
        stateData.Add(new State("RI", "EST", "Mandatory", 24));
        stateData.Add(new State("SC", "EST", "Mandatory", 20));
        stateData.Add(new State("SD", "CST", "COD", 10));
        stateData.Add(new State("TN", "CST", "COD", 10));
        stateData.Add(new State("TX", "CST", "Mandatory",10));
        stateData.Add(new State("UT", "MST", "None", 0));
        stateData.Add(new State("VA", "EST", "None", 0));
        stateData.Add(new State("VT", "EST", "None", 0));
        stateData.Add(new State("WA", "PST", "COD", 10)); 
        stateData.Add(new State("WI", "CST", "Mandatory", 15));
        stateData.Add(new State("WV", "EST", "Mandatory", 10));
        stateData.Add(new State("WY", "MST", "None", 0));
    }

    public IEnumerable<State> GetStates (){
        return stateData;
    }
    public IEnumerable<State> GetStatesInTimeZone(string timezone)
    {
        return stateData;
    }
    public IEnumerable<State> GetStatesCureType(string curetype){
        return stateData;
    }
    public IEnumerable<State> GetStatesCureTimeframe(int curetimeframe){
        return stateData;
    }        
}
    public void PopulateStateInfo()
    {
        StatesAndTerritories states = new StatesAndTerritories();

        foreach (State state in states.stateData)
        {
            //string s = stateData[i, 0];
            stateInfoBox.Items.Add(states.stateData);
        }
    }
    private void stateInfoBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        StatesAndTerritories states = new StatesAndTerritories();
        try
        {
            stateTimeZone.Text = states.stateData.ToString();
            stateCureType.Text = states.stateData.ToString();
            stateCureTimeframe.Text = states.stateData.ToString();
            if (stateCureTimeframe.Text != 0.ToString())
            {
                stateCureAtDPD.Text = ((55 - int.Parse(stateCureTimeframe.Text)).ToString());
            }
        }
        catch (IndexOutOfRangeException)
        {
            stateTimeZone.Text = "";
            stateCureType.Text = "";
            stateCureTimeframe.Text = "";
            stateCureAtDPD.Text = "";
        }
    }
列表初始值设定项:

struct State
{
    private String StateID;
    private String TimeZone;
    private String CureType;
    private int CureTimeframe;

    public State(string stateID, string timeZone, string cureType, int cureTimeframe)
    {
        StateID = stateID;
        TimeZone = timeZone;
        CureType = cureType;
        CureTimeframe = cureTimeframe;
    }

    public override string ToString()
    {
        string data = String.Format("{0} {1} {2} {3}", StateID, TimeZone, CureType, CureTimeframe);
        return data;
    }
}
class StatesAndTerritories
{
    public List<State> stateData = new List<State>();
    public StatesAndTerritories ()
    {
        stateData.Add(new State("AL", "CST", "None", 0));
        stateData.Add(new State("AK", "AST", "COD", 10));
        stateData.Add(new State("AR", "CST", "COD", 10));
        stateData.Add(new State("AZ", "MST", "COD", 10));
        stateData.Add(new State("CA", "PST", "COD/Mandatory", 10));
        stateData.Add(new State("CO", "MST", "Mandatory", 20));
        stateData.Add(new State("CT", "EST", "Mandatory", 10));
        stateData.Add(new State("DC", "EST", "Mandatory", 10));
        stateData.Add(new State("DE", "EST", "None", 0));
        stateData.Add(new State("FL", "EST", "COD", 10));
        stateData.Add(new State("GA", "EST", "COD", 10));
        stateData.Add(new State("HI", "HST", "None", 0));
        stateData.Add(new State("IA", "CST", "Mandatory", 20));
        stateData.Add(new State("ID", "MST", "None", 0));
        stateData.Add(new State("IL", "CST", "COD", 10));
        stateData.Add(new State("IN", "EST", "None", 0)); 
        stateData.Add(new State("KS", "CST", "Mandatory", 20));
        stateData.Add(new State("KY", "EST", "None", 0));
        stateData.Add(new State("LA", "CST", "Replevin", 0));
        stateData.Add(new State("MA", "EST", "Mandatory", 21));
        stateData.Add(new State("MD", "EST", "Mandatory", 10));
        stateData.Add(new State("ME", "EST", "Mandatory", 19));
        stateData.Add(new State("MI", "EST", "COD/Mandatory", 30));
        stateData.Add(new State("MN", "CST", "COD", 10));
        stateData.Add(new State("MO", "CST", "Mandatory", 20));
        stateData.Add(new State("MS", "CST", "None", 0)); 
        stateData.Add(new State("MT", "MST", "COD", 10)); 
        stateData.Add(new State("NC", "EST", "None", 0));
        stateData.Add(new State("ND", "CST", "None", 0));
        stateData.Add(new State("NE", "CST", "Mandatory", 20));
        stateData.Add(new State("NH", "EST", "None", 0));
        stateData.Add(new State("NJ", "EST", "COD", 10)); 
        stateData.Add(new State("NM", "MST", "COD", 10));
        stateData.Add(new State("NV", "PST", "COD", 10));
        stateData.Add(new State("NY", "EST", "None", 0));
        stateData.Add(new State("OH", "EST", "COD", 10)); 
        stateData.Add(new State("OK", "CST", "COD", 10));
        stateData.Add(new State("OR", "PST", "COD", 10));
        stateData.Add(new State("PA", "EST", "None", 0));
        stateData.Add(new State("RI", "EST", "Mandatory", 24));
        stateData.Add(new State("SC", "EST", "Mandatory", 20));
        stateData.Add(new State("SD", "CST", "COD", 10));
        stateData.Add(new State("TN", "CST", "COD", 10));
        stateData.Add(new State("TX", "CST", "Mandatory",10));
        stateData.Add(new State("UT", "MST", "None", 0));
        stateData.Add(new State("VA", "EST", "None", 0));
        stateData.Add(new State("VT", "EST", "None", 0));
        stateData.Add(new State("WA", "PST", "COD", 10)); 
        stateData.Add(new State("WI", "CST", "Mandatory", 15));
        stateData.Add(new State("WV", "EST", "Mandatory", 10));
        stateData.Add(new State("WY", "MST", "None", 0));
    }

    public IEnumerable<State> GetStates (){
        return stateData;
    }
    public IEnumerable<State> GetStatesInTimeZone(string timezone)
    {
        return stateData;
    }
    public IEnumerable<State> GetStatesCureType(string curetype){
        return stateData;
    }
    public IEnumerable<State> GetStatesCureTimeframe(int curetimeframe){
        return stateData;
    }        
}
    public void PopulateStateInfo()
    {
        StatesAndTerritories states = new StatesAndTerritories();

        foreach (State state in states.stateData)
        {
            //string s = stateData[i, 0];
            stateInfoBox.Items.Add(states.stateData);
        }
    }
    private void stateInfoBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        StatesAndTerritories states = new StatesAndTerritories();
        try
        {
            stateTimeZone.Text = states.stateData.ToString();
            stateCureType.Text = states.stateData.ToString();
            stateCureTimeframe.Text = states.stateData.ToString();
            if (stateCureTimeframe.Text != 0.ToString())
            {
                stateCureAtDPD.Text = ((55 - int.Parse(stateCureTimeframe.Text)).ToString());
            }
        }
        catch (IndexOutOfRangeException)
        {
            stateTimeZone.Text = "";
            stateCureType.Text = "";
            stateCureTimeframe.Text = "";
            stateCureAtDPD.Text = "";
        }
    }
组合框选择:

struct State
{
    private String StateID;
    private String TimeZone;
    private String CureType;
    private int CureTimeframe;

    public State(string stateID, string timeZone, string cureType, int cureTimeframe)
    {
        StateID = stateID;
        TimeZone = timeZone;
        CureType = cureType;
        CureTimeframe = cureTimeframe;
    }

    public override string ToString()
    {
        string data = String.Format("{0} {1} {2} {3}", StateID, TimeZone, CureType, CureTimeframe);
        return data;
    }
}
class StatesAndTerritories
{
    public List<State> stateData = new List<State>();
    public StatesAndTerritories ()
    {
        stateData.Add(new State("AL", "CST", "None", 0));
        stateData.Add(new State("AK", "AST", "COD", 10));
        stateData.Add(new State("AR", "CST", "COD", 10));
        stateData.Add(new State("AZ", "MST", "COD", 10));
        stateData.Add(new State("CA", "PST", "COD/Mandatory", 10));
        stateData.Add(new State("CO", "MST", "Mandatory", 20));
        stateData.Add(new State("CT", "EST", "Mandatory", 10));
        stateData.Add(new State("DC", "EST", "Mandatory", 10));
        stateData.Add(new State("DE", "EST", "None", 0));
        stateData.Add(new State("FL", "EST", "COD", 10));
        stateData.Add(new State("GA", "EST", "COD", 10));
        stateData.Add(new State("HI", "HST", "None", 0));
        stateData.Add(new State("IA", "CST", "Mandatory", 20));
        stateData.Add(new State("ID", "MST", "None", 0));
        stateData.Add(new State("IL", "CST", "COD", 10));
        stateData.Add(new State("IN", "EST", "None", 0)); 
        stateData.Add(new State("KS", "CST", "Mandatory", 20));
        stateData.Add(new State("KY", "EST", "None", 0));
        stateData.Add(new State("LA", "CST", "Replevin", 0));
        stateData.Add(new State("MA", "EST", "Mandatory", 21));
        stateData.Add(new State("MD", "EST", "Mandatory", 10));
        stateData.Add(new State("ME", "EST", "Mandatory", 19));
        stateData.Add(new State("MI", "EST", "COD/Mandatory", 30));
        stateData.Add(new State("MN", "CST", "COD", 10));
        stateData.Add(new State("MO", "CST", "Mandatory", 20));
        stateData.Add(new State("MS", "CST", "None", 0)); 
        stateData.Add(new State("MT", "MST", "COD", 10)); 
        stateData.Add(new State("NC", "EST", "None", 0));
        stateData.Add(new State("ND", "CST", "None", 0));
        stateData.Add(new State("NE", "CST", "Mandatory", 20));
        stateData.Add(new State("NH", "EST", "None", 0));
        stateData.Add(new State("NJ", "EST", "COD", 10)); 
        stateData.Add(new State("NM", "MST", "COD", 10));
        stateData.Add(new State("NV", "PST", "COD", 10));
        stateData.Add(new State("NY", "EST", "None", 0));
        stateData.Add(new State("OH", "EST", "COD", 10)); 
        stateData.Add(new State("OK", "CST", "COD", 10));
        stateData.Add(new State("OR", "PST", "COD", 10));
        stateData.Add(new State("PA", "EST", "None", 0));
        stateData.Add(new State("RI", "EST", "Mandatory", 24));
        stateData.Add(new State("SC", "EST", "Mandatory", 20));
        stateData.Add(new State("SD", "CST", "COD", 10));
        stateData.Add(new State("TN", "CST", "COD", 10));
        stateData.Add(new State("TX", "CST", "Mandatory",10));
        stateData.Add(new State("UT", "MST", "None", 0));
        stateData.Add(new State("VA", "EST", "None", 0));
        stateData.Add(new State("VT", "EST", "None", 0));
        stateData.Add(new State("WA", "PST", "COD", 10)); 
        stateData.Add(new State("WI", "CST", "Mandatory", 15));
        stateData.Add(new State("WV", "EST", "Mandatory", 10));
        stateData.Add(new State("WY", "MST", "None", 0));
    }

    public IEnumerable<State> GetStates (){
        return stateData;
    }
    public IEnumerable<State> GetStatesInTimeZone(string timezone)
    {
        return stateData;
    }
    public IEnumerable<State> GetStatesCureType(string curetype){
        return stateData;
    }
    public IEnumerable<State> GetStatesCureTimeframe(int curetimeframe){
        return stateData;
    }        
}
    public void PopulateStateInfo()
    {
        StatesAndTerritories states = new StatesAndTerritories();

        foreach (State state in states.stateData)
        {
            //string s = stateData[i, 0];
            stateInfoBox.Items.Add(states.stateData);
        }
    }
    private void stateInfoBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        StatesAndTerritories states = new StatesAndTerritories();
        try
        {
            stateTimeZone.Text = states.stateData.ToString();
            stateCureType.Text = states.stateData.ToString();
            stateCureTimeframe.Text = states.stateData.ToString();
            if (stateCureTimeframe.Text != 0.ToString())
            {
                stateCureAtDPD.Text = ((55 - int.Parse(stateCureTimeframe.Text)).ToString());
            }
        }
        catch (IndexOutOfRangeException)
        {
            stateTimeZone.Text = "";
            stateCureType.Text = "";
            stateCureTimeframe.Text = "";
            stateCureAtDPD.Text = "";
        }
    }

在表单上放置一个新的。然后可以将
列表
绑定到绑定源。使用
DataSource
成员将数据绑定到控件。这一切都可以在设计器中完成,并替换循环以填充下拉控件。文本框自动显示单个项(绑定源的当前项),而组合框可以绑定到整个列表。

您已将结构字段定义为私有字段,因此自然不能在其他类中引用它们。您只需定义一个公共只读属性来公开每个字段。

首先,您无法访问结构的内容

private String cvStateID;
private String cvTimeZone;
private String cvCureType;
private int cvCureTimeframe;
这些是私有的,因此不能从结构外部访问。 您需要为它们定义公共只读属性:

public readonly string StateID { get { return cvStateID; } }
public readonly string TimeZone { get { return cvTimeZone; } }
public readonly string CureType { get { return cvCureType; } }
public readonly int CureTimeframe { get { return cvCureTimeframe; } }
接下来,我想您只想在组合框中显示StateID。如果是这样,则需要相应地编写ToString():

public override string ToString()
{
    return StateID.ToString();
}
PopulateStateInfo
方法中,您没有添加列表中的每一项,而是添加整个集合,这是不寻常的!适当的实施方式如下:

public void PopulateStateInfo()
{
    StatesAndTerritories states = new StatesAndTerritories();

    foreach (State state in states.GetStates())
    {
        stateInfoBox.Items.Add(state);
    }
}
private void stateInfoBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    State selectedState = (State)stateInfoBox.SelectedItem;
    stateTimeZone.Text = selectedState.TimeZone;
    StateCureType.Text = selectedState.CureType;
    stateCureTimeframe.Text = selectedState.CureTimeframe.ToString();
}
最后,处理SelectedIndexChanged事件的正确方法如下:

public void PopulateStateInfo()
{
    StatesAndTerritories states = new StatesAndTerritories();

    foreach (State state in states.GetStates())
    {
        stateInfoBox.Items.Add(state);
    }
}
private void stateInfoBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    State selectedState = (State)stateInfoBox.SelectedItem;
    stateTimeZone.Text = selectedState.TimeZone;
    StateCureType.Text = selectedState.CureType;
    stateCureTimeframe.Text = selectedState.CureTimeframe.ToString();
}

我想说明的最后一点是,请不要将statesandTerritions类的stateData元素公开。如果只能从类内部进行修改,您将从中受益。

绑定需要属性,字段无法工作。这很有效!非常感谢你们,正如我在另一篇文章中所说的,这是我第一次真正进入structs,这无疑帮助我学会了它。我遇到的一个问题是,它抛出了一个错误,指出
readonly
对属性无效。虽然我删除了关键字,但效果很好,这是因为属性本身是只读的,没有
访问器吗?还有一件事,您使用
cv
作为前缀是任意选择,还是它代表了什么?我非常喜欢有意义的变量名,现在我不知道cv代表什么,所以知道它会有助于我的理解。再次感谢!我很乐意帮忙。关于readonly关键字,您是完全正确的,通过不提供setter,您可以轻松实现readonly。cv作为前缀代表“class variable”,但我在常用用法中看不到它。