index out of bounds of the array ?

G

Geoff Cox

Hello,

Ha

I get an "index out of bounds of the array" error message re the line

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

and cannot see why ...

The aim of the code is to display a series of images, each with a
slider. The user positions the pointer and then clicks on the button
to move to the next question and the slider value is put into an
array. After the last question clicking the button displays an "end"
message, the button is made invisible and all the slider values are
saved to a file.....

I'm new to C# and not sure how to see values for debugging. Have tried
Consile.WriteLine(value); but no value appears anywhere?!

Suggestions please!

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
{

private string[] LHSquestions = {"question 1","question 2"};
private string[] RHSquestions = {"question 1", "question 2" };
private int qnumber = 0;
private string[] results = { "fred" };
private int count = 0;
public Form1()
{
InitializeComponent();

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)
{

*** this.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(results[qnumber]);
}
tw.Close();

}

private void Form1_Load(object sender, EventArgs e)
{

}
}
}
 
T

Tim Haughton

Hello,

Ha

I get an "index out of bounds of the array" error message re the line

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

and cannot see why ...

You're creating a string array called results, but you're initialising it
with only 1 element. If you want somewhere to store strings, but aren't sure
how many elements you will need, use a StringCollection.

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton
 
M

Mrinal Kamboj

Hi ,

Error is simple ,

Results array that u create is by default created for containing a
single element and somewhere the value of qnumber becomes > 0 as arrays
have 0 based index .

You need to put a debug point in button click event and then debug to
check exact logical issue .

I am not able to replicate as u have not pasted the complete code ,
atleast paste InitializeComponent method for me to emulate the issue in
my system .

thanks ,

Mrinal

Tim said:
Hello,

Ha

I get an "index out of bounds of the array" error message re the line

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

and cannot see why ...


You're creating a string array called results, but you're initialising it
with only 1 element. If you want somewhere to store strings, but aren't sure
how many elements you will need, use a StringCollection.
 
G

Geoff Cox

You're creating a string array called results, but you're initialising it
with only 1 element. If you want somewhere to store strings, but aren't sure
how many elements you will need, use a StringCollection.

Thanks Tim - will follow up on the above.

I had tried code below using the new string line but that not allowed
so ended up using

private string[] results = new string[2];

but of course do not want to use an absolute figure ...

Where does Console.WriteLine(value) send to? Couldn't see anything
anywhere..

Cheers

Geoff

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

Tim Haughton

On Thu, 11 Aug 2005 06:43:52 GMT, "Tim Haughton"
Where does Console.WriteLine(value) send to? Couldn't see anything
anywhere..

Are you using Visual Studio? Degub.WriteLine and Trace.WriteLine should be
picked up in the output window, I *think* Console.WriteLine is also. If you
want a quick and dirty Windows way of dropping a debug message, you could
use MessageBox.Show( "My Message" ).

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton
 
G

Geoff Cox

Are you using Visual Studio? Degub.WriteLine and Trace.WriteLine should be
picked up in the output window, I *think* Console.WriteLine is also. If you
want a quick and dirty Windows way of dropping a debug message, you could
use MessageBox.Show( "My Message" ).

Tim,

I'm using C# Visual Express Beta 2 - will try what you suggest ... am
having problem with the StringCollection idea!

I tried

private StringCollection results = new StringCollection();

having also added

using System.StringCollections.Specialized;

but did not work - any obvious error?

Cheers

Geoff
 
G

Geoff Cox

Hi ,

Error is simple ,

Results array that u create is by default created for containing a
single element and somewhere the value of qnumber becomes > 0 as arrays
have 0 based index .

Thanks for your reply.

The problem is with

private string[] results = new string[2];

This works but I would prefer not to have the absolute number. I tried

private string[] LHSquestions = ("one","two"};
private string[] results = new string[LHSquestions.Length];

but this would not work. Tim suggests using StringCollection but I'm
not sure how to do that?!

Cheers

Geoff
 
G

Geoff Cox

You're creating a string array called results, but you're initialising it
with only 1 element. If you want somewhere to store strings, but aren't sure
how many elements you will need, use a StringCollection.

Tim,

Thinking about this again ... In fact I do know the number of elements
- it is the same as the number in the LHSquestions array, as I am
using the results array to store the trackbar values and there is 1
value for each question.

private string[] LHSquestions = {"one","two"};
private string[] results = new string[LHSquestions.Length];

ought to work (!) but doesn't.

private string[] results = new string[2]; does work but bad to have
absolute value..

Is there a simple way round this?


Cheers

Geoff
 
T

Tim Haughton

private string[] LHSquestions = {"one","two"};
private string[] results = new string[LHSquestions.Length];

ought to work (!) but doesn't.

Works for me. You know that arrays and collections are zero based, right?

using System;

namespace ConsoleApplication1
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
string[] array1 = {"bob","bill","harry"};
string[] array2 = new string[array1.Length];
for ( int i = 0; i < array2.Length; i++ )
{
array2 = array1;
Console.WriteLine( "array2[" + i.ToString() + "] = " +
array2 );
}
}
}
}

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton
 
G

Geoff Cox

private string[] LHSquestions = {"one","two"};
private string[] results = new string[LHSquestions.Length];

ought to work (!) but doesn't.

Works for me. You know that arrays and collections are zero based, right?

Tim,

Sorted but not sure why ... Perhaps you can explain?

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

Once I changed the first 2 lines from private to static the error
message for " private string[] results = new
string[LHSquestions.Length];" went away. Why was that? I have added
the full partial class code below ...

Thanks

Geoff



-----------full 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 int qnumber = 0;
//private string[] results = new string[2];
private string[] results = new string[LHSquestions.Length];
private int count = 0;

public Form1()
{
InitializeComponent();

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)
{

this.results[qnumber] = trackBar1.Value.ToString();
// MessageBox.Show(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)
{

}
}
}








using System;

namespace ConsoleApplication1
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
string[] array1 = {"bob","bill","harry"};
string[] array2 = new string[array1.Length];
for ( int i = 0; i < array2.Length; i++ )
{
array2 = array1;
Console.WriteLine( "array2[" + i.ToString() + "] = " +
array2 );
}
}
}
}
 
T

Tim Haughton

Sorted but not sure why ... Perhaps you can explain?

Aaarr, I see. Your original implementation was dependent on the order in
which the variables are initialised, which is undefined. When you changed it
to static, the static members would have been initialised before the
instance members.

If you change the first 2 members back to private instance members rather
than static, and switch their initialisation to the constructor instead, it
should work. i.e.

class MyClass
{
string[] array1;
string[] array2;

MyClass()
{
array1 = {"a","b"}
array2 = new string[array1.Length]
}
}

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton
 
G

Geoff Cox

If you change the first 2 members back to private instance members rather
than static, and switch their initialisation to the constructor instead, it
should work. i.e.

Tim,

Not to sure what has to be changed - if you have the time perhaps you
could show me? Could you move the lines as necessary and then I can
get a better idea of what should happen.

I need the first question (ie the LHS and the RHS are the same at the
moment for each question but actually the LHS will be something like
Very Good and the RHS Very Bad), to be there when the app starts..


The code which works is below

Cheers

Geoff

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 int qnumber = 0;
private string[] results = new string[LHSquestions.Length];
private int count = 0;

public Form1()
{
InitializeComponent();

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)
{

this.results[qnumber] = trackBar1.Value.ToString();
// MessageBox.Show(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)
{

}
}
}
 
T

Tim Haughton

Not to sure what has to be changed - if you have the time perhaps you
could show me? Could you move the lines as necessary and then I can
get a better idea of what should happen.

Sure, change this...

public partial class Form1 : Form
{
static string[] LHSquestions = {"question 1","question 2"};
static string[] RHSquestions = {"question 1", "question 2" };
private int qnumber = 0;
private string[] results = new string[LHSquestions.Length];
private int count = 0;

public Form1()
{
InitializeComponent();

this.label1.Text = LHSquestions[qnumber];
this.label2.Text = RHSquestions[qnumber];
}
// Rest of class


To this...


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];

this.label1.Text = LHSquestions[qnumber];
this.label2.Text = RHSquestions[qnumber];
}
// Rest of class


You see the difference??

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton
 
G

Geoff Cox

To this...


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];

this.label1.Text = LHSquestions[qnumber];
this.label2.Text = RHSquestions[qnumber];
}
// Rest of class

Tim,

I tried above but unless I add "string[]" in front of following 3
lines, I get lots of errors when making the build...

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

Having added the string[] the build is OK but when I run the app,
after the first click of the button I get error message "Object
reference not set to an instance of an object" re following line

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

any ideas?!

Cheers

Geoff

---code below with // in front of lines which do work OK -------


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)
{

}
}
}
 

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