d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Require Expert's Help > With C#
Add Reply New Topic New Poll
Member
Posts: 2,954
Joined: Apr 9 2006
Gold: 72.00
Jun 14 2014 12:45am
Hi, I can't find my error, so could you help me with your expertise?

I don't understand, why the content of my ArrayList (numberList) is deleted, each time I click on the button.


Code
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;

namespace WindowsFormsApplication8
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public int count = 0;


private void button1_Click(object sender, EventArgs e)
{
count++;
ArrayList numberList = new ArrayList();
Random rnd = new Random();
int num = rnd.Next(1, 6);

if (!numberList.Contains(num))
{
numberList.Add(num);

switch (num)
{
case 1:
this.pictureBox1.Image = WindowsFormsApplication8.Properties.Resources.pic1;
break;

case 2:
this.pictureBox1.Image = WindowsFormsApplication8.Properties.Resources.pic2;
break;

case 3:
this.pictureBox1.Image = WindowsFormsApplication8.Properties.Resources.pic3;
break;

case 4:
this.pictureBox1.Image = WindowsFormsApplication8.Properties.Resources.pic4;
break;

case 5:
this.pictureBox1.Image = WindowsFormsApplication8.Properties.Resources.pic5;
break;

}

if (count == 5)
MessageBox.Show("gz!");

}


}
}
}


Actually I want to store all my randomly generated numbers in that array.

Can anyone tell me what I'm doing wrong?

Thanks in advance!!

This post was edited by DBK-Leader on Jun 14 2014 12:48am
Member
Posts: 1,358
Joined: Dec 30 2012
Gold: 0.10
Jun 14 2014 12:58am
Try declaring

Code
ArrayList numberList = new ArrayList();


Outside of the function, like you've done with count. I believe the list gets destroyed at the end of button1_Click's scope

This post was edited by SelfTaught on Jun 14 2014 01:00am
Member
Posts: 2,954
Joined: Apr 9 2006
Gold: 72.00
Jun 14 2014 01:32am
Thank you very much, SelfTaught!!

I could fix my first problem by setting the function type to public. :)

Now, my last problem is that I cannot call the else-function at the buttom of the code:
Code
else
{
button1_Click();
}


If the generated number is already in the list, I want the program to automatically repeat the button1_Click() -function

Can anyone identify why this doesn't work?

Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;

namespace WindowsFormsApplication8
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

public int count = 0;
ArrayList numberList = new ArrayList();

public void button1_Click(object sender, EventArgs e)
{
count++;

Random rnd = new Random();
int num = rnd.Next(1, 6);

if (!numberList.Contains(num))
{
numberList.Add(num);

switch (num)
{
case 1:
this.pictureBox1.Image = WindowsFormsApplication8.Properties.Resources.pic1;
break;

case 2:
this.pictureBox1.Image = WindowsFormsApplication8.Properties.Resources.pic2;
break;

case 3:
this.pictureBox1.Image = WindowsFormsApplication8.Properties.Resources.pic3;
break;

case 4:
this.pictureBox1.Image = WindowsFormsApplication8.Properties.Resources.pic4;
break;

case 5:
this.pictureBox1.Image = WindowsFormsApplication8.Properties.Resources.pic5;
break;

}

if (count == 5)
MessageBox.Show("Hurra!");
}

else // this is the part that doesn't work
{
button1_Click();
}
}
}
}


Thanks a lot!

This post was edited by DBK-Leader on Jun 14 2014 01:39am
Member
Posts: 2,954
Joined: Apr 9 2006
Gold: 72.00
Jun 14 2014 02:10am
Problem solved!

I put all the comments into another function and recalled that function instead of button1_Click() .


Thanks a lot for your advice!
Member
Posts: 2,954
Joined: Apr 9 2006
Gold: 72.00
Jun 14 2014 03:59am
Sorry guys, I still got another problem.

Oftentimes (not always) when I excecute my programm, I get a "System.StackOverflowException" error.
I believe that it's due to this function
Code
else
{
Func2();

}


Can anybody here tell me how I can solve this issue of "System.StackOverflowException", please?


Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;

namespace WindowsFormsApplication8
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

public int count = 0;
ArrayList numberList = new ArrayList();

public void button1_Click(object sender, EventArgs e)
{
Func2();
}

public void Func2()
{
Random rnd = new Random();
int num = rnd.Next(1, 6);

if (!numberList.Contains(num))
{
numberList.Add(num);
count++;

switch (num)
{
case 1:
this.pictureBox1.Image = WindowsFormsApplication8.Properties.Resources.pic1;
break;

case 2:
this.pictureBox1.Image = WindowsFormsApplication8.Properties.Resources.pic2;
break;

case 3:
this.pictureBox1.Image = WindowsFormsApplication8.Properties.Resources.pic3;
break;

case 4:
this.pictureBox1.Image = WindowsFormsApplication8.Properties.Resources.pic4;
break;

case 5:
this.pictureBox1.Image = WindowsFormsApplication8.Properties.Resources.pic5;
break;

}

if (count == 5)
MessageBox.Show("gz!");

}

else
{
Func2();

}
}
}
}


Thank you!

This post was edited by DBK-Leader on Jun 14 2014 03:59am
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jun 14 2014 06:32am
you really need to spend some more time debugging before you give up so easily. VS has a great debugger. additionally you can try logging.
Member
Posts: 4,372
Joined: Jul 2 2009
Gold: 3,630.00
Jun 14 2014 08:02pm
In your case, the stackoverflow exception is thrown because of the recursion of Func2.
Let's say your stack can handle 5 calls of function Func2, Func2 is called recursively when the number you picked randomly is already in your ArrayList. So if you picked 5 times in a row a number which was already in the list, the stack will overflow.
I guess you should do this with a while block.

Also, there might be a problem with your ArrayList and its method Contains() ? Maybe use a List<int> ?
Member
Posts: 4,943
Joined: Jun 18 2006
Gold: 0.00
Jul 28 2014 05:39am
better make "rnd" as a global variable.

and this is a good example of what u shoudl never do....

dont call ureself in a function if u dont know what u do....
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll