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.
No need for local variables
J3,J5,J8etc. and other data structures.Just add a
ToStringmethod to your Tracer classand add your Tracer objects to listbox as below
This way, you will see the
codes in your ListBox.In some of ListBox’s event you can get the tracer object as