[英]C# How do I convert a string into an instance of a class?
In my program I have a Class Tracer: 在我的程序中,我有一个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;
}
}
}
In my main form i have created instances of several tracers: 在我的主要形式中,我创建了多个跟踪器的实例:
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");
I have a method in my main form that returns the tracer name from a listbox 我有一个主要形式的方法,该方法从列表框中返回跟踪器名称
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
I want to be able to use the results of GetTracer() to retrieve properties of that tracer. 我希望能够使用GetTracer()的结果来检索该跟踪器的属性。
For Example: 例如:
I could call 我可以打电话
J3.family;
// The result of which is "Raychem BTV"
What I want to do is use 我想做的是使用
GetTracer().family;
and have it return the property associated with the tracer that my method GetTracer returns. 并使其返回与我的方法GetTracer返回的跟踪器关联的属性。
Is this possible? 这可能吗? Thank you in advance for your assistance. 预先感谢您的协助。 I am very new to programming and, while I am trying to make my code more robust by using classes, it is proving to be more difficult than I imagined. 我对编程非常陌生,尽管我试图通过使用类使代码更健壮,但事实证明,它比我想象的要困难得多。
You can't do GetTracer().Name
unless GetTracer
returns a Tracer
object. 除非GetTracer
返回Tracer
对象,否则不能执行GetTracer().Name
。
If you want to find a tracer depending on it's code you should think about some dictionary to store that tracer names in it: 如果要根据代码查找示踪剂,则应考虑一些字典来在其中存储示踪剂名称:
Dictionary<string, Tracer> dict = new Dictionary<string, Tracer>;
and insert reference to every created tracer there: 并在其中插入对每个创建的跟踪器的引用:
Tracer J3 = new Tracer("Raychem BTV", "3BTV1-CT", "Self-Reg", 3, 120, 150, 185, "T6", "FM/CSA/ATEX", "J3");
dict.Add("J3", J3);
after that you can easily do this: 之后,您可以轻松地执行以下操作:
string name = GetTracer();
if(dict.ContainsKey(name))
{
Tracer item = dict[name];
string family = item.family;
}
No need for local variables J3
, J5
, J8
etc. and other data structures. 不需要局部变量J3
, J5
, J8
等和其他数据结构。 Just add a ToString
method to your Tracer class 只需在您的Tracer类中添加ToString
方法
public override string ToString()
{
return code;
}
and add your Tracer objects to listbox as below 并将您的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") );
This way, you will see the code
s in your ListBox. 这样,您将在ListBox中看到code
。
In some of ListBox's event you can get the tracer object as 在ListBox的某些事件中,您可以将tracer对象作为
void listBox_SelectedIndexChanged(object sender, EventArgs e)
{
Tracer tracer = ((ListBox)sender).SelectedItem as Tracer;
MessageBox.Show(tracer.family);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.