Need help pulling an int from an array in c# - c#

namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
int[] codes = { 39835, 72835, 49162, 29585, 12653, 87350, 74783};
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{}
private void label4_Click(object sender, EventArgs e)
{}
private void btnRandom_Click(object sender, EventArgs e)
{
Random mRandom = new Random();
int randcode = mRandom.Next(0, codes.Length - 1);
}
}
}
I want to pull a random code from the array by clicking "btnRandom" and compare it to a string but "int randcode" always comes up as an error when i try it.
private void button1_Click(object sender, EventArgs e)
{
if (txtCode.Text == randcode) ;
{
MessageBox.Show("working");
}
}
Trying to get it to work like this.

You have a few problems:
Your codes array contains integers. Your textbox contains a string.
You are not actually accessing the array when you do the compare - you're comparing an integer index to a string.
You have a semicolon after your if statement that shouldn't be there.
randcode is not defined at the class level, so you can't access it from a different function than the one it was declared in.
In short, you want something like this:
int randcode;
private void btnRandom_Click(object sender, EventArgs e)
{
Random mRandom = new Random();
randcode = mRandom.Next(0, codes.Length - 1);
}
private void button1_Click(object sender, EventArgs e)
{
if (txtCode.Text == codes[randcode].ToString())
{
MessageBox.Show("working");
}
}

You have either to parse txtCode.Text to integer or convert randCode to string:
if (int.Parse(txtCode.Text) == randcode) ...
or
if (txtCode.Text == randcode.ToString()) ....
But there are some things you can/must fix:
In Random.Next(min, max) method, max is exclusive, so the call must be like this:
int randcode = mRandom.Next(0, codes.Length);
You are declaring ranCode inside btnRandom_Click(), and must be in Form1 class:
public partial class Form1 : Form
{
int randCode;
int[] codes = { 39835, 72835, 49162, 29585, 12653, 87350, 74783};
....
randcode = mRandom.Next(0, codes.Length);
And you have a ; after the if statement, so MessageBox.Show("working"); will be executed no matter the result.
Finally the code should be like this:
public partial class Form1 : Form
{
int randCode;
int[] codes = { 39835, 72835, 49162, 29585, 12653, 87350, 74783};
...
private void btnRandom_Click(object sender, EventArgs e)
{
Random mRandom = new Random();
randcode = mRandom.Next(0, codes.Length);
}
...
private void button1_Click(object sender, EventArgs e)
{
if (int.Parse(txtCode.Text) == randcode)
{
MessageBox.Show("working");
}
}
...
}

thats because txtCode.Text is a string and randcode is an integer. YOu cant compare these two. They must both be strings or ints. Try this
if(txtCode.Text == randcode.ToString())
I also note that int randcode is not in scope in this method. You meed to mkae randcode a member variable by declaring it at class scope

Related

Call method inside a Form

Hi I am learning C# and struggling with a Form in which a btnCalc_click event should call a method calcArea and produce the output in a textBox1.text
the error is in row: textBox1.Text = calcArea.ToString();
who can help with with the correct syntax ?
namespace Meetkunde
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void txbLengte_TextChanged(object sender, EventArgs e)
{
double length = 0;
length = double.Parse(txbLength.Text);
}
private void txbBreedte_TextChanged(object sender, EventArgs e)
{
double width = 0;
width = double.Parse(txbWidth.Text);
}
public double calcArea(double length, double width)
{
double area = 0;
area = (length * width);
return area;
}
private void label3_Click(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void btnCalc_Click(object sender, EventArgs e)
{
textBox1.Text = calcArea.ToString();
}
}
}
As I mentioned above - you need to pass the method the parameters as you defined them in the method. You don't need Text Changed events for this either - plus they aren't doing anything.
Try changing your code to something like (not real sure of what exactly you named textboxes):
private void btnCalc_Click(object sender, EventArgs e)
{
double width = Convert.ToDouble(txbBreedte.Text);//txbWidth.Text?
double length= Convert.ToDouble(txbLengte.Text);//txbLength.Text?
textBox1.Text = calcArea(length, width).ToString();
}
calcArea.ToString();
when you are calling this, you aren't calling the function, you are referencing the function, not the return
do
calcArea(parameters).ToString();
replace parameters with what you want to calculate.

Call a LIST<DOUBLE> inside button1 click event into button2 click event in c#

I have two buttons in my C# windows form application. Button1 and Button2.
i want to use a variable and a list calculated in Button1's event as an input variable in Button2's event. how can I do that? Example:
private void button1_Click(object sender, EventArgs e)
{
int a;
// some steps
// after these steps, assume a gets the value of 5 so a = 5 at this point.
// also there is a list which gets its values after these steps
List<double> parameterValues = new List<double> {
i.GetDouble(), S.GetDouble(), L.GetDouble(),B.GetDouble()
};
}
Here is the code for button2 event, in this I want to be able to use the value of a calculated in button1's code.
private void button2_Click(object sender, EventArgs e)
{
int b = a + 5;
// some code to call the list as well
}
You have to make int a global in order to use it in both buttons.
public int a;
private void button1_Click(object sender, EventArgs e)
{
a = 5;
// some steps
// after these steps, assume a gets the value of 5 so a = 5 at this point.
}
private void button2_Click(object sender, EventArgs e)
{
int b = a + 5;
}
You have a scope issue currently. The value you want to use inside button click 2 must be at least modular to the forms class in order to use in both methods. In this example, "outerValue" is modular and can be accessed by both. Have a read through this article to get a better overview of variable scope.
https://msdn.microsoft.com/en-us/library/ms973875.aspx
public partial class Form1 : Form
{
private int outerValue = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
int a = 5;
outerValue = a + 5;
}
private void button2_Click(object sender, EventArgs e)
{
int b = outerValue + 5;
}
}

Set an 'int' in one event and use it in another event

I'm making a little something in Windows Form (C# Visual Studio)
But I need to know how I can set an int and then to use it in another event. For example, :
private void BtnYes_Click(object sender, EventArgs e)
{
int yes = 1;
int no = 0;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (yes == 1) {
//EVENT
}
}
When I do this I get some errors. Can anyone help me with this? Or just tell me how to do something like this using a different technique?
U need to use a field for this:
private int _yes;
private void BtnYes_Click(object sender, EventArgs e)
{
_yes = 1;
int no = 0;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (_yes == 1) {
//EVENT
}
}
The variable "yes" you are declaring is only visible in the scope of the method. By making it a field of class, this would make it visible to all methods in the class (when private).
class YourClass
{
private int yes = 1;
private int no = 0;
private void BtnYes_Click(object sender, EventArgs e)
{
//Remove the type declaration here to use the class field instead. If you leave the type declaration, the variable here will be used instead of the class field.
yes = 1;
no = 0;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (yes == 1) {
//EVENT
}
}
}

Variable Scoping in c#.net

Hi it might sound a simple task but i am a bit confused here
I have an event :
private void ManReg_Click(object sender, EventArgs e)
{
int store = data[0];
}
and then another event function like :
private void measurementsRead_Click(object sender, EventArgs e)
{
}
I would like to use the variable "store" in the second function. How can I ?
The form is declared as follows :
namespace myRfid
{
public partial class Form1 : Form
{
}
}
You should put variable onto the class level
class ...
{
private int store;
private void ManReg_Click(object sender, EventArgs e)
{
store = data[0];
}
private void measurementsRead_Click(object sender, EventArgs e)
{
// use store
}
}
You can set it as shown in below code in your class but private as it would be used within class only as shown below :-
private int store;
private void ManReg_Click(object sender, EventArgs e)
{
this.store = data[0];
}
private void measurementsRead_Click(object sender, EventArgs e)
{
this.store//// however you want to use
}

Trying to write a simple calculator... beginner with C#, can't get past an exception

Okay, so I am modeling this idea off of one of my school projects I had fairly recently and I am not sure why it isn't working at the moment.
My current plan for the program is to use a DataManager to hold a string array containing the numbers a user would input from the menu, and then convert them into integers, and ultimately delineate through them by operators and such. I haven't figured out the last part, and I don't think that idea is going to work like I made it sound. But right now I am having trouble getting the program to store string values in the array.
When I run the program my current goal is to click '1' and press 'Enter', when that happens I want the output to look like 'Number: 1' but I keep throwing an exception.
"An unhandled exception of type 'System.InvalidCastException' occurred in mscorlib.dll
Additional information: At least one element in the source array could not be cast down to the destination array type."
Here is the code from my DataManager class....
public class DataManager
{
public ArrayList calculation;
public DataManager()
{
calculation = new ArrayList();
}
public void addNumbers(string n)
{
calculation.Add(n);
}
// public NumbersAndOperators[] getNumbers()
//{
// return //(NumbersAndOperators[])calculation.ToArray(typeof(NumbersAndOperators));
//}
public void removeNumbers(NumbersAndOperators n)
{
calculation.Remove(n);
}
public void addOperators(string o)
{
calculation.Add(o);
}
public NumbersAndOperators[] getOperators()
{
return (NumbersAndOperators[])calculation.ToArray(typeof(NumbersAndOperators));
}
public void removeOperators(NumbersAndOperators o)
{
calculation.Remove(o);
}
}
}
Here is the code from my NumbersAndOperators class
public class NumbersAndOperators
{
private string number; private string operate;
public NumbersAndOperators(string n, string o)
{
number = n;
operate = o;
}
public void setNumber(string n)
{
number = n;
}
public void setOperate(string o)
{
operate = o;
}
public string getNumber(string n)
{
return n;
}
public string getOperate(string o)
{
return o;
}
}
}
Here is the code from my form
public partial class Form1 : Form
{
public DataManager data;
public Form1(DataManager d)
{
InitializeComponent();
data = d;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button10_Click(object sender, EventArgs e)
{
string n = "0";
data.addNumbers(n);
}
private void button1_Click(object sender, EventArgs e)
{
string n = "1";
data.addNumbers(n);
}
private void button2_Click(object sender, EventArgs e)
{
string n = "2";
data.addNumbers(n);
}
private void button3_Click(object sender, EventArgs e)
{
string n = "3";
data.addNumbers(n);
}
private void button4_Click(object sender, EventArgs e)
{
string n = "4";
data.addNumbers(n);
}
private void button5_Click(object sender, EventArgs e)
{
string n = "5";
data.addNumbers(n);
}
private void button6_Click(object sender, EventArgs e)
{
string n = "6";
data.addNumbers(n);
}
private void button7_Click(object sender, EventArgs e)
{
string n = "7";
data.addNumbers(n);
}
private void button8_Click(object sender, EventArgs e)
{
string n = "8";
data.addNumbers(n);
}
private void button9_Click(object sender, EventArgs e)
{
string n = "9";
data.addNumbers(n);
}
private void division_Click(object sender, EventArgs e)
{
string o = "/";
data.addOperators(o);
}
private void multiplication_Click(object sender, EventArgs e)
{
string o = "*";
data.addOperators(o);
}
private void subtraction_Click(object sender, EventArgs e)
{
string o = "-";
data.addOperators(o);
}
private void addition_Click(object sender, EventArgs e)
{
string o = "+";
data.addOperators(o);
}
private void dec_Click(object sender, EventArgs e)
{
string n = ".";
data.addNumbers(n);
}
private void equals_Click(object sender, EventArgs e)
{
panel1.Controls.Clear();
NumbersAndOperators[] num = data.getNumbers();
Label tmp;
for (int i = 0; i < num.Length; i++)
{
tmp = new Label();
tmp.Text = "Number:" + data.getNumbers();
tmp.AutoSize = true;
tmp.Location = new Point(0, 85);
panel1.Controls.Add(tmp);
}
}
}
}
I appreciate any and all advice, this is a problem I haven't seen before which makes it even harder to understand from just reading. I've been googling like a fool for awhile now and everything that comes up I simply do not understand.
Also, I Commented out the line that is throwing the exception
When you call:
public void addNumbers(string n)
{
calculation.Add(n);
}
you add a string to your ArrayList.
When you call:
(NumbersAndOperators[])calculation.ToArray(typeof(NumbersAndOperators));
It tries to retrieve data as NumbersAndOperators, which is invalid since you added string to your ArrayList.
Best would be to have your calculation member as:
public List<NumbersAndOperators> calculation;
so you have a type safe list.
On this line:
return (NumbersAndOperators[])calculation.ToArray(typeof(NumbersAndOperators));
You're trying to get an array of NumbersAndOperators, but your ArrayList also contains strings, and strings can't be cast to NumbersAndOperators...
I guess you meant to write:
for (int i = 0; i < num.Length; i++)
{
tmp = new Label();
tmp.Text = "Number:" + num[i]; // <--
tmp.AutoSize = true;
tmp.Location = new Point(0, 85);
panel1.Controls.Add(tmp);
}

Categories