1

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.

2 Answers 2

5

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Rather than finish typing up my (identical) answer I give you +1 for getting there before me!
Thanks guys I will give it a try. The quick responses are much appreciated!
1

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 codes 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);
}

3 Comments

Thank you so much for the response. This was the perfect solution for me and exactly what I was wanting to accomplish and it will greatly improve the quality of this particular program. Cheers
Im not sure if anyone will see this, but just to confirm my understanding: Is the ToString() method in my Tracer Class what allows me to call MessageBox.Show(tracer.family); without an error?
@wcard No,It's use is for only to display a string representing your object in listbox. ListBox's Items collection holds all your objects (listBox.Items.Add( new Tracer(.....)). Just remove ToString method and test your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.