private rather than static?

G

Geoff Cox

Hello,

The code below works but could be improved I am told. I should use
private rather than static declarations.

The "correct" lines have // in front of them. But when I use those
lines I get an error message "Object
reference not set to an instance of an object" re following line

results[qnumber] = trackBar1.Value.ToString();

Any ideas please?

This is created using C# Visual Express Beta 2 - I could post the
other part of the code if helpful....

Cheers

Geoff


--------------------code------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace slider3
{
public partial class Form1 : Form
{

static string[] LHSquestions = {"question 1","question 2"};
static string[] RHSquestions = {"question 1", "question 2" };
//private string[] LHSquestions;
//private string[] RHSquestions;
private int qnumber = 0;
private string[] results = new string[LHSquestions.Length];
//private string[] results;
private int count = 0;

public Form1()
{
InitializeComponent();

//string[] LHSquestions = {"question 1","question 2"};
//string[] RHSquestions = {"question 1","question 2"};
//string[] results = new string[LHSquestions.Length];

this.label1.Text = LHSquestions[qnumber];
this.label2.Text = RHSquestions[qnumber];

}

private void pictureBox1_Click(object sender, EventArgs e)
{

}

private void trackBar1_Scroll(object sender, EventArgs e)
{

}

private void button1_MouseClick(object sender, MouseEventArgs
e)
{
results[qnumber] = trackBar1.Value.ToString();

++qnumber;

if (qnumber == LHSquestions.Length)
{
endMessage();
}
else
{

this.label1.Text = LHSquestions[qnumber];
this.label2.Text = RHSquestions[qnumber];

}
}

private void endMessage()
{
this.label1.Text = "Finished!";
this.label2.Text = "Thank you";
this.button1.Visible = false;

TextWriter tw = new StreamWriter("d:\\a-temp1\\data.txt");
for (count = 0; count < LHSquestions.Length; count++)
{
tw.WriteLine("q" + (count+1) + " = " + results[count]);
}
tw.Close();

}

private void Form1_Load(object sender, EventArgs e)
{

}
}
}
 
R

Rick Lones

Geoff said:
Hello,

The code below works but could be improved I am told. I should use
private rather than static declarations.

The "correct" lines have // in front of them. But when I use those
lines I get an error message "Object
reference not set to an instance of an object" re following line

results[qnumber] = trackBar1.Value.ToString();

Any ideas please?
static string[] LHSquestions = {"question 1","question 2"};
static string[] RHSquestions = {"question 1", "question 2" };
//string[] LHSquestions = {"question 1","question 2"};
//string[] RHSquestions = {"question 1","question 2"};
//string[] results = new string[LHSquestions.Length];

Dunno what a compiler maven would say, but my rather simple-minded way of
looking at this is that you have to instantiate something before you can assign
to it. "Static" causes creation of the object, which is why it works for you.
You want to instantiate via "new", as per:

string[] LHSquestions = new string[]{"question 1","question 2"};
string[] RHSquestions = new string[]{"question 1","question 2"};
string[] results = new string[LHSquestions.Length];

I think this will do what you want.

HTH,
-rick-
 
G

Geoff Cox

Dunno what a compiler maven would say, but my rather simple-minded way of
looking at this is that you have to instantiate something before you can assign
to it. "Static" causes creation of the object, which is why it works for you.
You want to instantiate via "new", as per:

string[] LHSquestions = new string[]{"question 1","question 2"};
string[] RHSquestions = new string[]{"question 1","question 2"};
string[] results = new string[LHSquestions.Length];

Rick

I do above abd it builds OK but still get an error message "Object
reference not set to an instance of an object" re following line

results[qnumber] = trackBar1.Value.ToString();

!!??

Geoff
 
G

Geoff Cox

Could you post a short but complete program which demonstrates the
problem?

Jon,

Have found how to get the code to work! Perhaps you can explain why?

public partial class Form1 : Form
{

private string[] LHSquestions;
private string[] RHSquestions;
private int qnumber = 0;
private string[] results;
private int count = 0;

public Form1()
{
InitializeComponent();

LHSquestions = {"question 1","question 2"};
RHSquestions = {"question 1","question 2"};
results = new string[LHSquestions.Length];

Previously I had

string[] LHSquestions = {"question 1","question 2"};
string[] RHSquestions = {"question 1","question 2"};
string[] results = new string[LHSquestions.Length];

I thought that if creating new string I had to have string[] at the
start but apparently not ...

Cheers

Geoff
 
J

Jon Skeet [C# MVP]

<Geoff Cox <>> wrote:

Previously I had

string[] LHSquestions = {"question 1","question 2"};
string[] RHSquestions = {"question 1","question 2"};
string[] results = new string[LHSquestions.Length];

I thought that if creating new string I had to have string[] at the
start but apparently not ...

The problem is you were declaring new local variables within the
constructor - those were completely separate variables from the member
variables you'd declared earlier.

What you really wanted wasn't to declare new variables, just assign
values to the member ones - which is what happens if you get rid of the
"string[]" bit at the start of each line.
 
G

Geoff Cox

<Geoff Cox <>> wrote:

Previously I had

string[] LHSquestions = {"question 1","question 2"};
string[] RHSquestions = {"question 1","question 2"};
string[] results = new string[LHSquestions.Length];

I thought that if creating new string I had to have string[] at the
start but apparently not ...

The problem is you were declaring new local variables within the
constructor - those were completely separate variables from the member
variables you'd declared earlier.

What you really wanted wasn't to declare new variables, just assign
values to the member ones - which is what happens if you get rid of the
"string[]" bit at the start of each line.


Ah! Local. I see what you mean!

Thanks

Geoff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top