7
Answers

Problems with an Indexer using a class as the type.

Bill Beggs

Bill Beggs

16y
2.7k
1

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

Answers (7)