Access to a private method in C# - c#

Hi People I'm newbie in the C# world and I'm having a problem. I have done an array in the Form_Load method of my program, but I need to access the array in a picture_box method like this:
private void Form2_Load(object sender, EventArgs e)
{
//In this method we get a random array to set the images
int[] imgArray = new int[20];
Random aleatorio = new Random();
int num, contador = 0;
num = aleatorio.Next(1, 21);
imgArray[contador] = num;
contador++;
while (contador < 20)
{
num = aleatorio.Next(1, 21);
for (int i = 0; i <= contador; i++)
{
if (num == imgArray[i])
{
i = contador;
}
else
{
if (i + 1 == contador)
{
imgArray[contador] = num;
contador++;
i = contador;
}
}
}
}
}
private void pictureBox1_Click(object sender, EventArgs e)
{
pictureBox1.Image = Image.FromFile(#"C:\Users\UserName\Desktop\MyMemoryGame\" + imgArray[0] + ".jpg");
}
But I only get the error: Error 1 The name 'imgArray' does not exist in the current context

You need to define int[] imgArray at the class level (outside of Form2_Load) rather than inside it. Otherwise the "scope" of that variable is limited to that function. You will need to knock off the first "int[]" part in Form2_Load to prevent you from just declaring a new variable.
For example:
public class MyClass
{
private int[] myInt;
public void Form2_Load(...) {
myInt = ...;
}
}

The error means exactly what it says.
You've declared the array in the scope of the Form2_Load function. Outside of it, it will not exist.
To do what you're trying to achieve, add a private array to the form itself.
private int[] _imgArray = new int[20];
private void Form2_Load(object sender, EventArgs e)
{
//Setup the imgArray
}
private void pictureBox1_Click(object sender, EventArgs e)
{
//_imgArray is now available as its scope is to the class, not just the Form2_Load method
}
Hopefully that helps.

Related

Index Out Of Range exception on " examScores[countOfExams] = double.Parse(examScoresTextBox.Text);" [duplicate]

This question already has answers here:
What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?
(5 answers)
Closed 6 years ago.
When I press the click event to add array values, it gives me an out of index exception error. I'd like to know why it's happening and how to fix it, thanks.
using System;
using System.Windows.Forms;
namespace ArrayTestCalculator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
double total = 0;
double average = 0;
double scoreInput = 0;
int countOfExams = 0;
double[] examScores;
double count = 0;
//field array declare
private void enterBtn_Click(object sender, EventArgs e)
{
countOfExams = int.Parse(numOfExamsTextBox.Text);
examScores = new double[countOfExams];
examLabel.Text = "Exam 1: ";
label1.Visible = false;
numOfExamsTextBox.Visible = false;
enterBtn.Visible = false;
examLabel.Visible = true;
examScoresTextBox.Visible = true;
addBtn.Visible = true;
}
private void addBtn_Click(object sender, EventArgs e)
{
examLabel.Text = "Exam " + (countOfExams +1) + ":";
examScores[countOfExams] = double.Parse(examScoresTextBox.Text);
countOfExams++;
examScoresTextBox.ResetText();
examScoresTextBox.Focus();
if(countOfExams >= examScores.Length)
{
MessageBox.Show("hoise vaya");
}
}
private void clearBtn_Click(object sender, EventArgs e)
{
Array.Clear(examScores, 0, examScores.Length);
ClearAll();
}
private void exitBtn_Click(object sender, EventArgs e)
{
this.Close();
}
public void ClearAll()
{
examLabel.Text = "Exam:";
examScoresTextBox.ResetText();
numOfExamsTextBox.ResetText();
}
private void calcBtn_Click(object sender, EventArgs e)
{
}
}
}
In the first line of your enterBtn_Click,
countOfExams = int.Parse(numOfExamsTextBox.Text);
I suppose you are using countOfExams as the total length of the array.
Later in the second line of your addBtn_Click,
examScores[countOfExams] = double.Parse(examScoresTextBox.Text);
countOfExams++;
I suppose you are using countOfExams to track the actual number of exams. Because countOfExams is already set to the length, it results in OutOfRangeException.
Thus, I suggest you to use another variable (e.g. a local variable) for the total length of the array,
var size = int.Parse(numOfExamsTextBox.Text);
examScores = new double[size];
double[] examScores; has been defined as array, which was never initialized if you are not calling enterBtn_Click first
Ideally,
You should first initialize/ check if it is initialized it by
double[] examScores = new double[<length>];
also, the counter starts from 0 and not 1
If you are not sure about the length or capacity, use List

Copy one array to another using Array.Copy(a, 0, b, 0, 20);

If I click on btn1 the contains of array 1 (generate) will change. What I want to do is: after clicking on btn2 (Reset btn) want to copy the elements of array 2 (n) to array1 (generate) but I'm getting this error:
Destination array was not long enough. Check destIndex and length, and the array's lower bounds.
public partial class Form1 : Form
{
string[] generate = new string[20] {"Name1","Name2","Name3","Name4","Name5","Name6","Name7","Name8","Name9","Name10","Name11","Name12","Name13","Name14","Name15","Name16","Name17","Name18","Name19","Name20" };
string[] n = new string[] { "Name1", "Name2", "Name3", "Name4", "Name5", "Name6", "Name7", "Name8", "Name9", "Name10", "Name11", "Name12", "Name13", "Name14", "Name15", "Name16", "Name17", "Name18", "Name19", "Name20" };
string name;
int Num;
public Form1()
{
InitializeComponent();
}
private void button3_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e) // START btn
{
Num = int.Parse(textBox1.Text) - 1;
if (Num < 20)
{
if (generate.Length > Num)
{
for (int i = Num; i >= 0; i--)
{
name = generate.ElementAt(i);
listBox1.Items.Add(name); // Print it to list1
generate = generate.Where(s => s != name).ToArray(); // Delete name after using
}
}
else MessageBox.Show("Sorry, remaining names =" + generate.Length);
listBox2.Items.Clear();
listBox2.Items.AddRange(generate);
}
else MessageBox.Show("Max name is 20!");
}
private void button2_Click(object sender, EventArgs e) // Reset btn
{
listBox1.Items.Clear();
listBox2.Items.Clear();
textBox1.Clear();
Array.Copy(n, 0, generate, 0, 20);
}
}
}
You can use LINQ to duplicate array.
var array2 = array1.ToList().ToArray()
If you use .Copy method make sure your range is correct
When you click button 1, the following line is creating a new array for the generate variable which is of size less than 20:
generate = generate.Where(s => s != name).ToArray(); // Delete name
So, when you call button 2, you are trying to copy from array n of size 20 to the array generate of size less than 20.

Find the highest number in array , using while or for?

Please a i have a Questions , I need find the higghest value in array. To the array will people write name (textbox1) and money (texbox2). I have 2 buttons first button is save to the array and second write the name with the biggest money.
Code for save:
string[] name = new string[50];
int items = 0;
int[] money = new int[50];
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
Convert.ToInt32(textBox2.Text);
}
catch
{
name[items] = textBox1.Text;
money[items] = Int32.Parse(textBox2.Text);
items++;
}
}
And to the button2 need search the biggest value and write name! Please help me
private void button2_Click(object sender, EventArgs e)
{
int maxIndex = 0;
for(int i = 0; i < 50; i++)
{
if (money[i] > money[maxIndex])
maxIndex = i;
}
MessageBox.Show(name[maxIndex] + " has biggest value " + money[maxIndex]);
}
To get the Max int from your array you can use IEnumerable.Max:
money.Max();
But there could be more than one name with the same high money value, perhaps you need to handle this also, I think Dictionary would be your best option
private Dictionary<string, int> Names = new Dictionary<string, int>();
private void button1_Click(object sender, EventArgs e)
{
int value = 0;
if (int.TryParse(textBox2.Text, out value))
{
if (!Names.ContainsKey(textBox1.Text))
{
Names.Add(textBox1.Text, value);
}
}
}
private void button2_Click(object sender, EventArgs e)
{
if (Names.Any())
{
int maxMoney = Names.Max(v => v.Value);
var names = Names.Where(k => k.Value.Equals(maxMoney));
foreach (var name in names)
{
// Names with the highest money value
}
}
}

Timer and List<string> in C#

I need help with timer and List.
List consist of collection of string say 5 or 6 at a time. Now, I want to display string one on label1 and it should wait for 5s and then display string 2 on label1. I have timer control and I am specifying my code in timer_tick event.
private void timer1_Tick(object sender, EventArgs e)
{
string[] myStatus = myStatusCollection.ToArray();
int length = myStatus.Length;
for (int i = 0; i < length; i++)
{
string _myStatus = myStatus[i];
//label1.ResetText();
MessageBox.Show("Twitter Status =" + _myStatus);
//label1.Text = _myStatus;
//label1.Visible = true;
}
}
I have specify, Elapse = true and interval = 5000 but still I am not able to display one string at a time. In fact, I am getting last string only. I want to rotate the strings all time.
Can anyone help me.
That's because you're looping through all the strings each time the timer event fires.
Store your index in a private variable and use that instead.
private int _index = 0;
private void timer1_Tick(object sender, EventArgs e)
{
string[] myStatus = myStatusCollection.ToArray();
string _myStatus = myStatus[_index];
//label1.ResetText();
MessageBox.Show("Twitter Status =" + _myStatus);
//label1.Text = _myStatus;
//label1.Visible = true;
if(_index == (myStatus.Length - 1))
_index = 0;
else
_index++;
}
Well it is doing just what you told it to. However, what you told it to do is not what you meant for it to do. Try this.
public class Form1 : Form {
private string[] statuses = { "A", "B", "C", "D", "E" }; // Init with proper values somewhere
private int index = 0;
private void OnTimerTick(object sender, EventArgs e) {
string status = statuses[index];
index++;
if (index == statuses.Length) { // If index = Array.Length means we're
// outside bounds of array
index = 0;
}
}
}
I'd create an int outside of the Tick to hold your position. Make sure you reset it back to 0 when you restart the process.
int MyPosition = 0;
private void timer1_Tick(object sender, EventArgs e)
{
string[] myStatus = myStatusCollection.ToArray();
int length = myStatus.Length;
if((MyPosition + 1) > length)
{
//Index Out of Range
}
else
{
string _myStatus = myStatus[MyPosition];
label1.Text = _myStatus
}
MyPosition++;
}

c# - getting the same random number repeatedly [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Random number generator not working the way I had planned (C#)
Hi,
Below is the complete code for a small app I am writing - it returns an ArrayList of alphanumeric codes. The problem I have is that when 'stepping' through the code the e.Result is returned correctly with each item being different. However, if I let the app run without the breakpoints I just get multiple instances of the same variable back:
public partial class AlphaNum : Form
{
public AlphaNum()
{
InitializeComponent();
// Initialise BackGroundWorker Reporting
backgroundWorker1.WorkerReportsProgress = true;
// Initialise BackGroundWorker Cancel
backgroundWorker1.WorkerSupportsCancellation = true;
}
private void button1_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
button1.Enabled = false;
}
private int RandomNumber(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}
public string RandomString(Random r, int len)
{
string str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
StringBuilder sb = new StringBuilder();
while ((len--) > 0)
sb.Append(str[(int)(r.NextDouble() * str.Length)]);
return sb.ToString();
}
private string generateCode()
{
Random rnd = new Random();
int length = int.Parse(stringLength.Text);
string code = "";
code = RandomString(rnd, length);
return code;
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
int quantity = int.Parse(quantityRequired.Text);
ArrayList codes = new ArrayList();
string myText = "";
for (int a = 0; a < quantity; a++)
{
myText = generateCode();
codes.Add(myText);
backgroundWorker1.ReportProgress(((100 / quantity) * a));
}
e.Result = codes;
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage; //update progress bar
//Console.WriteLine(time);
//in this example, we log that optional additional info to textbox
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
button1.Enabled = true;
ArrayList codes = new ArrayList();
codes = (ArrayList)e.Result;
for (int i = 0; i < codes.Count; i++ )
{
outputText.Text += codes[i].ToString();
outputText.AppendText(Environment.NewLine);
}
}
}
}
Can anyone explain why the result is okay when I effectively slow the process down by stepping my way through but incorrect when it runs without being stopped/slowed down?
Many Thanks
The loop in backgroundWorker1_DoWork is so fast that the Random object generated in generateCode is always seeded with the same value, thus producing the same values. Don't recreate the Random object but assign one to an instance variable in the constructor of your class and only use that one.
You shouldn't create a new Random object every time you need a random number.
Random rnd = new Random();
uses the current time to initialize the LFSR, when the program runs at full speed, the same "current time" gets used many times in a row.

Categories

Resources