简体   繁体   中英

C# How do I convert a string into an instance of a class?

In my program I have a 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.

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.

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.

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. Just add a ToString method to your Tracer class

public override string ToString()
{
    return code;
}

and add your Tracer objects to listbox as below

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.

In some of ListBox's event you can get the tracer object as

void listBox_SelectedIndexChanged(object sender, EventArgs e)
{
    Tracer tracer =  ((ListBox)sender).SelectedItem as Tracer;
    MessageBox.Show(tracer.family);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM