I'm new at C#. I am trying to get this indexer coto work, but I have obviously missed something.
Near the bottom, the line: hv[0].TS = "Test"; is giving me an error see code below. "NullReferenceException was unhandled" -- "Object reference not set to an instance of an object".This is using 2008 (3.5), so I used the get/set code defaults. Anyone have any ideas.
namespace
MCTIDMain{
public partial class Mainform : Form { public class V2 { public string TS { get; set; } public int TI { get; set; }}
public class Hv { public V2[] hvv = new V2[5]; public V2 this[int indexrange] { get { return hvv[indexrange]; } set { hvv[indexrange] = value; } }}
public Mainform() {InitializeComponent();
}
private void Mainform_Load(object sender, EventArgs e) {}
public void SetVars_Click(object sender, EventArgs e) { Hv hv = new Hv();hv[0].TS =
"Test"; <-------- Error "NullReferenceException was unhandled"Var1.Text = hv[0].TS;
} } }
Thanks, Bill
Bill BeggsPosted Jan 8, 2008, 2:45 AM
AlanPosted Jan 6, 2008, 7:02 AM
Bill, in the first line of SetVars_Click did you remember to remove the initial 'Hv' ?
Hv hv = new Hv(); // original
hv = new Hv(); // should now be
If you didn't, then a new local variable will still be created which will hide the field and so the latter will still be null when you try to access it in the other method.
Bill BeggsPosted Jan 5, 2008, 8:38 PM
Alan, the private Hv hv = null; didn't work. Should the instances of the new Hv and V2 be defined outside of the two routines that will use them?
When executing the "if" statement you added in ReadVars_Click, it cause the same exception I had before, so hv is not visible in that routine. The get and set both work in the first routine.
AlanPosted Jan 5, 2008, 7:55 PM
To enable your 'hv' variable to be accessible to all methods in the class, you need to make it into a private field rather than a local variable:
private Hv hv = null ; // now private field, initial value null
public void SetVars_Click(object sender, EventArgs e) {
hv = new Hv();
hv[0] = new V2();
hv[0].TS = "Test";
hv[0].TI = 1;
}
public void ReadVars_Click(object sender, EventArgs e) {
if (hv[0] != null) { // make sure not null
Var1.Text = hv[0].TS; // hv and its indexer now accessible from ReadVars_Click
Var2.Text =
Convert.ToString(hv[0].TI);}
}
Bill BeggsPosted Jan 5, 2008, 7:29 PM
Alan, that fixed the problem. That really helped, thanks. I can now set and get values in the single SetVars_Click routine.
How would I now increase the scope so that values set in the SetVars_Click routine can be read in a similar ReadVars_Click routine? The two routines are now as below these loose the contents because of the second instance in the second routine. I have tried to make them static, with no luck.
public void SetVars_Click(object sender, EventArgs e) { Hv hv = new Hv();hv[0] =
new V2();hv[0].TS =
"Test";hv[0].TI = 1;
}
public void ReadVars_Click(object sender, EventArgs e) { Hv hv = new Hv();hv[0] =
new V2();Var1.Text = hv[0].TS;
Var2.Text =
Convert.ToString(hv[0].TI);}
AlanPosted Jan 5, 2008, 6:52 PM
Actually, Dave, I think the problem here is that hv[0] is null because, whilst Bill has initialized the hvv array, he hasn't assigned any V2 objects to its elements and so they are all null.
This should fix it:
Hv hv = new Hv();
hv[0] = new V2();
hv[0].TS = "Test";
DavePosted Jan 5, 2008, 6:44 PM
hv.hvv[0].TS = "Test";