Variable Scoping in c#.net - c#

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
}

Related

add pagebar in my form for to change page

I made a simple GUI, in which I would like to add an option where I can switch pages, it is the same page when I open it, I would like a bar where I click and switch pages
namespace POS
{
public partial class Form1 : Form
{
private object gunaLabel_date;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Guna.UI.Lib.ScrollBar.PanelScrollHelper flowphelper ;
gunaLabel_date = DateTime.Now.ToLongDateString();
}
private void gunaVScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
}
private void userControl17_Load(object sender, EventArgs e)
{
}
}
}

Calling a private void function from a button click method

I am trying to call a private void function from a button click method . The value of selectedChoice is fetched from combo box . I debugged and found out that the value of selectedChoice is being fetched properly and it is even going inside If condition inside the button click method. Its just that the functions are not being call .
Every method is inside the class Form1.
namespace Test
{
public partial class Form1 : Form
{
private void button1_Click(object sender, EventArgs e)
{
if(selectedChoice == "ABC")
{
Function1();
Function2();
}
The combo box code -
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if(comboBox1.SelectedIndex!=0)
{
selectedChoice = comboBox1.SelectedItem.ToString();
}
}
The functions I am trying to call are simply defined as
private void Function1()
{
//do something
}
You are trying to perform a compare opertion
selectedChoice == "ABC"
the
==
Operator shall not be used for string. This operator will check if the id of the element and the comparer are equals. Please use
selectedChoice.Equals("ABC")
instead.
Nevertheless the Methods are called using this code:
private string selectedChoice = "ABC";
private void button1_Click(object sender, EventArgs e)
{
if(selectedChoice.Equals("ABC"))
{
Function1();
Function2();
}
}
private void Function2()
{
}
private void Function1()
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if(comboBox1.SelectedIndex!=0)
{
selectedChoice = comboBox1.SelectedItem.ToString();
}
}

How can I pass a string from an onclick event to another class

I am new to encapsulation. I would like to pass a string from the OnClick event of one of my buttons to a private method in another class. When I try to set the string from inside the method now it says that variable isn't recognized How can I safely pass it in?
My OnClick Event:
public partial class ClassOne
protected void lnkbtnKeySearch_Click(object sender, EventArgs e)
{
string myNewString = "Change the value of the string";
}
Class I want to pass it into:
public partial class ClassTwo
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadData();
}
}
private void LoadData()
{
{
string filterText = myNewString;
}
}
You can try to hold the value of the string inside a session variable:
protected void lnkbtnKeySearch_Click(object sender, EventArgs e)
{
Session["myNewString"] = "Change the value of the string";
}
private void LoadData()
{
string filterText = Session["myNewString"].ToString();
}
One way is to make MyNewString static.
public partial class ClassOne
{
public static string MyNewString{set;get;}
protected void lnkbtnKeySearch_Click(object sender, EventArgs e)
{
MyNewString= "Change the value of the string";
}
}
Then
public partial class ClassTwo
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadData();
}
}
private void LoadData()
{
{
string filterText = ClassOne.MyNewString;
}
}
}

Creating a universal boolean in C#

I write:
bool example = true under the code for say, a button event: private void button1_Click(object sender, EventArgs e)
I am trying to change the value of this bool when the user pushes a button.
The problem is, my bool is only recognized in this context. When I try to use it under private void checkBox1_CheckedChanged(object sender, EventArgs e) Visual Studio notifies me of an error saying the name doesn't exist in the context.
Where should I be putting the code for a bool?
Thanks.
Move it to class-level: that is, a member of the Form class:
public class YourForm : Form {
private bool _example = true;
// ... your event handlers here
private void checkBox1_CheckedChanged(object sender, EventArgs e) {
_example = false; // etc
}
}
Where should I be putting the code for a bool?
Outside of your methods, in the class level
public class MyForm : Form
{
// here is class level
public void SomeMethod()
{
// here is method level
}
}
As a member of the class;
public class MyClass
{
private bool example = false;
private void button1_Click(object sender, EventArgs e)
{
example = true;
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
//example available
}
}
Use static variable or class level.

Retrieve data from one form and use it in another form

Alrighty. Here is my problem. I have just about everything done. I just need to take input from the form, and then use it in an algorithm in the second form. I have everything else written up, I just need to know how to connect the 2 so I can write up the last of the code. I've done some research, but none of it seems to go with what I'm trying to do.
Here is the main form.
namespace Airplanes
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
}
private void Arrival_Click(object sender, EventArgs e)
{
ArrivalForm newForm;
newForm = new ArrivalForm();
newForm.ShowDialog();
}
private void Fuel_Click(object sender, EventArgs e)
{
Fuelform newForm2;
newForm2 = new Fuelform();
newForm2.ShowDialog();
}
private void Status_Click(object sender, EventArgs e)
{
}
private void Items_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void NameBox_TextChanged(object sender, EventArgs e)
{
}
private void FuelBox_TextChanged(object sender, EventArgs e)
{
}
private void GateBox_TextChanged(object sender, EventArgs e)
{
}
private void Singlebutton_CheckedChanged(object sender, EventArgs e)
{
}
private void PrivateButton_CheckedChanged(object sender, EventArgs e)
{
}
private void CommercialButton_CheckedChanged(object sender, EventArgs e)
{
}
}
}
And here is the form I'm trying to connect to the main form.
namespace Airplanes
{
public partial class Fuelform : Form
{
public Fuelform()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Fuelform_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
Thanks in advance for any answers.
There are a couple of ways...the easiest would probably be to pass the data in through the constructor of your new form.
FuelForm newForm2 = new FuelForm(myData);
And then change the constructor for your FuelForm:
public FuelForm(int myData) // or whatever data type you need
{
// Deal with myData
}
In Source form
destinationForm df = new destinationForm ();
df .myValue= "My Value";
df .ShowDialog();
in Destination Form
private string destVariable;
public string myValue
{
get { return destVariable; }
set { destVariable= value; }
}
then you can use destVariable everywhere in destination form

Categories