在我的程序中,我有一个Class Tracer:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Tracepak_Heat_Loss_Program
{
class Tracer
{
public string family;
public string model;
public string type;
public int output;
public int voltage;
public int maxMaintain;
public int maxIntermittent;
public string tRating;
public string approvals;
public string code;
public Tracer(string f, string m, string t, int o, int v, int mM, int mI,string tR, string a, string c)
{
family = f;
model = m;
type = t;
output = o;
voltage = v;
maxMaintain = mM;
maxIntermittent = mI;
tRating = tR;
approvals = a;
code = c;
}
}
}
在我的主要表单中,我创建了几个跟踪器的实例:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Tracepak_Heat_Loss_Program
{
public partial class mainForm : Form
{
public mainForm()
{
InitializeComponent();
}
private void mainForm_Load(object sender, EventArgs e)
{
}
Tracer J3 = new Tracer("Raychem BTV", "3BTV1-CT", "Self-Reg", 3, 120, 150, 185, "T6", "FM/CSA/ATEX", "J3");
Tracer J5 = new Tracer("Raychem BTV", "5BTV1-CT", "Self-Reg", 5, 120, 150, 185, "T6", "FM/CSA/ATEX", "J5");
Tracer J8 = new Tracer("Raychem BTV", "8BTV1-CT", "Self-Reg", 8, 120, 150, 185, "T6", "FM/CSA/ATEX", "J8");
Tracer J10 = new Tracer("Raychem BTV", "10BTV1-CT", "Self-Reg", 10, 120, 150, 185, "T6", "FM/CSA/ATEX", "J10");
我的主窗体中有一个方法,它从列表框中返回跟踪器名称
public string GetTracer()
{
String s = tracerListBox.Text;
int index = s.IndexOf(" ");
return index >= 0 ? s.Substring(0, index): s;
}
// This could yield "J3" for example
我希望能够使用GetTracer()的结果来检索该跟踪器的属性。
例如:
我可以打电话
J3.family;
// The result of which is "Raychem BTV"
我想要做的是使用
GetTracer().family;
并让它返回与我的方法GetTracer返回的跟踪器关联的属性。
这可能吗?提前感谢您的协助。我对编程很陌生,虽然我试图通过使用类来使代码更加健壮,但事实证明它比我想象的要困难。
答案 0 :(得分:5)
除非GetTracer().Name
返回GetTracer
个对象,否则您无法Tracer
。
如果你想根据它的代码找到一个跟踪器,你应该考虑一些字典来存储它的跟踪器名称:
Dictionary<string, Tracer> dict = new Dictionary<string, Tracer>;
并在那里插入对每个创建的跟踪器的引用:
Tracer J3 = new Tracer("Raychem BTV", "3BTV1-CT", "Self-Reg", 3, 120, 150, 185, "T6", "FM/CSA/ATEX", "J3");
dict.Add("J3", J3);
之后你可以轻松地做到这一点:
string name = GetTracer();
if(dict.ContainsKey(name))
{
Tracer item = dict[name];
string family = item.family;
}
答案 1 :(得分:1)
无需局部变量J3
,J5
,J8
等以及其他数据结构。
只需在Tracer类中添加ToString
方法
public override string ToString()
{
return code;
}
并将您的Tracer对象添加到列表框中,如下所示
listBox.Items.Add( new Tracer("Raychem BTV", "3BTV1-CT", "Self-Reg", 3, 120, 150, 185, "T6", "FM/CSA/ATEX", "J3") );
listBox.Items.Add( new Tracer("Raychem BTV", "5BTV1-CT", "Self-Reg", 5, 120, 150, 185, "T6", "FM/CSA/ATEX", "J5") );
listBox.Items.Add( new Tracer("Raychem BTV", "8BTV1-CT", "Self-Reg", 8, 120, 150, 185, "T6", "FM/CSA/ATEX", "J8") );
listBox.Items.Add( new Tracer("Raychem BTV", "10BTV1-CT", "Self-Reg", 10, 120, 150, 185, "T6", "FM/CSA/ATEX", "J10") );
这样,您将在ListBox中看到code
。
在ListBox的某些事件中,您可以将跟踪器对象作为
void listBox_SelectedIndexChanged(object sender, EventArgs e)
{
Tracer tracer = ((ListBox)sender).SelectedItem as Tracer;
MessageBox.Show(tracer.family);
}