Reputation: 47
Alright, here we go. I am kinda new with programming and C#, but I'm learning it with John Sharp's Visual C#: Step By Step book. (I wonder if that dude's last name is really Sharp)
Anyway, I'm trying to create an application that will help me transpose note. It's a windows forms app and I'm using two classes: form1.cs where all the code happens and vars.cs where all the variables are stored.
My problem is that I want to use string vars.noteChosen and define with comboBoxNote's currently selected item (e.g. C). Whenever I do this, however, when I execute vars.noteChosen = comboNote.SelectedText;
I get a "Object reference not set to an instance of an object." error. Any ideas? (comboNote is a combobox)
This is vars.cs
namespace Transposer
{
class vars
{
public static bool rbTransposeUp = true;
public static string noteChosen = ""; //
public static string toTranspose = ""; //
public static int note = 0;
public static string boxResultText = note.ToString();
}
}
and this is a part of form1.cs
namespace Transposer
{
public partial class Transposer : Form
{
public Transposer()
{
vars.noteChosen = comboNote.SelectedText;
vars.toTranspose = comboNote.Text;
InitializeComponent();
}
...
What am I doing wrong?
Upvotes: 2
Views: 151
Reputation: 1001
Move the lines after the call to InitializeComponent();
It's this call that is creating the comboNote object.
That is
public Transposer()
{
InitializeComponent(); // this constructs the comboNote
vars.noteChosen = comboNote.SelectedText;
vars.toTranspose = comboNote.Text;
}
Upvotes: 6