parsing a variable from class to class - c#

I would like to parse a variable example number from one class to another as show in the code below:
public class Gettemp // is the "public" will affect the passing of variable operation?
{
public void temp(string temp)
{
temp = "123"; // things that i wanted to pass from this class
}
}
public partial class Form1 : Form
{
private void STARTbtn_Click(object sender, EventArgs e)
{
MessageBox.Show() // i wan to show the variables here
}
}

What about making that method return the temp value :
public string temp()
{
string temp = "123";
return temp; // things that i wanted to pass from this class
}
}
private void STARTbtn_Click(object sender, EventArgs e)
{
Gettemp _GetTemp = new Gettemp();
string temp = _GetTemp.temp();
MessageBox.Show(temp);
}

Related

How can I make a value exist in context

I'm trying to make a small program to get started with C#, I've made a request to coinbase to get the current value of Btc. I now get a simple error but not sure how I should fix it.
error is: The name 'Btc' does not exist in the current context
error happens on Value.Text Btc;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public static void SendRequest(object sender, EventArgs e)
{
while (true)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://api.coinbase.com/v2/prices/USD/spot?");
using (var response = req.GetResponse())
{
var html = new StreamReader(response.GetResponseStream()).ReadToEnd();
string Btc = Regex.Match(html, "\"BTC\",\"currency\":\"USD\",\"amount\":\"([^ \"]*)").ToString();
}
Thread.Sleep(300);
}
}
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
public void Value_Click(object sender, EventArgs e)
{
Value.Text = Btc;
}
}
}
You're declaring a variable inside of a method:
string Btc = Regex.Match(html, "\"BTC\",\"currency\":\"USD\",\"amount\":\"([^ \"]*)").ToString();
Which means it only exists within the scope of that method (or that code block). If it needs to be available to other methods in the class, you could make it a class-level variable:
public partial class Form1 : Form
{
private static string Btc;
//...
}
Then assign to that class-level variable:
Btc = Regex.Match(html, "\"BTC\",\"currency\":\"USD\",\"amount\":\"([^ \"]*)").ToString();
Note of course that, because the method is static, so is the class-level variable. I can't really speak to the applicability of this for the rest of your design, but at the very least this would be how you'd move the scope of the variable from the method to the whole class.
You should make Btc a property of the class.
I`ve corrected your code so it should work now
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
private static string Btc = "";
public static void SendRequest(object sender, EventArgs e)
{
while (true)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://api.coinbase.com/v2/prices/USD/spot?");
using (var response = req.GetResponse())
{
var html = new StreamReader(response.GetResponseStream()).ReadToEnd();
Btc = Regex.Match(html, "\"BTC\",\"currency\":\"USD\",\"amount\":\"([^ \"]*)").ToString();
}
Thread.Sleep(300);
}
}
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
public void Value_Click(object sender, EventArgs e)
{
Value.Text = Btc;
}
}
}

Creating a reference to another object

I want to make an object that stores a reference to another object. I have a code like this:
public partial class Form1 : Form
{
int test = 1;
store st;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
st = new store(test);
}
private void button1_Click(object sender, EventArgs e)
{
test = 7;
}
private void button2_Click(object sender, EventArgs e)
{
label1.Text = Convert.ToString((int)st.o);
}
}
public class store
{
public object o;
public store(object obj)
{
o = obj;
}
}
If I click button2 - I can see "1" in my label. But if I click button2 and then button1 - I still see "1". How should I alter my code so I'll see "7" in that case?
When you create the store object you're evaluating the value of the test variable, and storing that value rather than the test variable. If you want to have a way of evaluating the variable to its value later, you can use a lambda to close over the variable, since closures in C# close over variables, not values.
public partial class Form1 : Form
{
int test = 1;
Store<string> store;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
store = new Store<string>(() => test.ToString());
}
private void button1_Click(object sender, EventArgs e)
{
test = 7;
}
private void button2_Click(object sender, EventArgs e)
{
label1.Text = store.GetValue();
}
}
public class Store<T>
{
private Func<T> function;
public Store(Func<T> function)
{
this.function = function;
}
public T GetValue()
{
return function();
}
}
Note that I made a few changes to the names of items to be in line with standard C# conventions, rather than having Store expose the generator function's field publicly, I provide a function that lets you get the value, and I've also made it generic, rather than using object, as this both avoids boxing, and prevents the need to cast the object returned from the store.
With minimal changes, here you go.
Properties and constructor of Form1:
public Form1()
{
st = new store(test);
}
private int testPrivate { get; set; }
public int test
{
get { return testPrivate; }
set
{
testPrivate = value;
st.o = value;
}
}
public store st { get; set; }
The store class:
public class store
{
public object o { get; set; }
public store(object obj)
{
o = obj;
}
}

How can I take a parameter or call a function which is declared in other braces?

I'm beginner in C# and having great difficulty to figure our some issues. So I hope my terminology does not matter. Here is my question. Let's say I have the following code:
namespace WindowsFormsApplication8
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//code starts
//...
//if(...) {
//...
//string parameter = abc.ToString();
//}
//code ends
}//Form1 ends
private void button1_Click(object sender, EventArgs e)
{
//code here
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = parameter;
button1.Perform();
}
}
}
I have difficulties here.
How can I use the string declared in Form1 called parameter inside button2_Click?
textBox1.Text = parameter; doesn't work.
Use a member variable.
public partial class Form1 : Form
{
private string parameter = null;
public Form1()
{
InitializeComponent();
// ...
parameter = abc.ToString();
}

using a listbox to display items from a class

My program compiles and to me it makes sense.
I want to know how to get 'name' to list in my listbox.
I'm trying to use an array of classes so I can add salesmen. A new class will be created every time a person is to be added.
This way the name is a way of calling all the data in that class.
When I execute the program everything looks like its doing what it's suppose to do but it just lists 'form1' in the list box when i press the list names button
This is what i mean:
Where am I going wrong?
SalesmanClass
namespace WindowsFormsApplication1
{
class SalesmanClass
{
private string name;
public string cNum;
public string Email;
public string address;
public string gArea;
public int tSales;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
Form 1
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Form2 w2;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (w2 == null)
{
w2 = new Form2();
w2.Show();
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
Object names;
names = Name;
listBox1.Items.Add(Name);
}
}
}
Form 2
//form2
namespace WindowsFormsApplication1
public partial class Form2 : Form
{
SalesmanClass[] salesman = new SalesmanClass[] { };
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text.Trim().Length != 0)
{
for (int i = 0; i > salesman.Length; i++)
{
if (salesman[i] == null)
{
salesman[i].Name = textBox1.Text;
break;
}
}
this.Close();
}
else
{
MessageBox.Show("Please Input a Name");
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
In this method:
private void button2_Click(object sender, EventArgs e)
{
Object names;
names = Name; // <--- Using this.Name, i.e. Form.Name, NOT SalesmanClass.Name
listBox1.Items.Add(Name);
}
You have accidentally used the Name property of the Form itself (which naturally is "form1").
You need to have a SalesmanClass object at this point, and use the Name property of that instead.
You don't currently have a list of salesmen in your Form1, so you will need to add one and use that.
Also, if you have a list or array of SalesmanClass objects, you should create a List<string> from them and use that to initialise the listbox, something like:
SalesmanClass[] salesmen = new SalesmanClass[] {};
// ...
List<string> names = new List<string>();
foreach (var salesman in salesmen)
names.Add(salesman.Name);
listBox1.Items.AddRange(names);
You can do this using Linq too, but I don't want to confuse you by introducing that into the mix!
In your button2_Click, you have :
names = Name;
What does this Name belong to ? I suspect it belongs to Form1, that's why it's been displaying "form1". If that's the case, you just need to get your SalesmanClass object and get the Name from it.

C# : modify form control from a class

I have Form and Class like that :
namespace ALTER_Control
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ALTER A = new ALTER();
A.ALTER();
}
}
public class ALTER
{
public Form1 F;
public void ALTER()
{
F.TextBox1.Text="I Altered That";
}
}
}
So i try to call ALTER() to change the textbox1 value in Form1 but i get that error :
object reference not set to an instance of an object
That happens only if i am accessing or modifying the Form1 Controls.
And by the way i set textbox1 modifier to public
So , finally i`d like to change the control value without getting that error.
You need to assign the reference to the form. Like this:
private void button1_Click(object sender, EventArgs e)
{
ALTER A = new ALTER();
A.F = this;
A.ALTER();
}
Why does your ALTER class (which isn't a great class name either) have to know about your form?
private void button1_Click(object sender, EventArgs e)
{
ALTER A = new ALTER();
this.TextBox1.Text = A.ALTER();
}
}
[...]
public class ALTER
{
public String ALTER()
{
// Do your thing
return "I Altered That";
}
}
Use these lines of code:
ALTER A = new ALTER();
A.F = this ;
A.ALTER();

Categories

Resources