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;
}
}
}
Related
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);
}
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();
}
I have a problem with my code. Its working fine and there is no error but a logical one i think so. I have used a method PassValue(int id) to get value from another from. I have tested it and the forms are exchanging the values correctly but the problem comes when I use the value which i have received from other form as a "textbox.text" or a "label.text"
Here is my code:
namespace MyProgram
{
public partial class UserProfile : Form
{
public string empidstr;
public UserProfile()
{
InitializeComponent();
}
public void PassValue(int id)
{
string idstring = Convert.ToString(id);
// empidlabel.Text = idstring;
empidstr = idstring;
}
private void button2_Click(object sender, EventArgs e)
{
empidlabel.Text = empidstr;
}
private void UserProfile_Load(object sender, EventArgs e)
{
}
}}
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.
There is another question that is very similar to mine however after reading it i still cannot get it to work.
I have two forms , MainForm and SecondForm and a few other classes, i need an instance of my AVLtree and be able to access it through my other forms.
This is what ive done so far
MainForm
public partial class MainForm : Form
{
AddArtist secondForm = new AddArtist();
public static AVLTree<Artist> treeAVL { get; set; }
public MainForm()
{
InitializeComponent();
}
private void butAdd_Click(object sender, EventArgs e)
{
secondForm.Show();
}
private void MainForm_Load(object sender, EventArgs e)
{
}
}
}
SecondForm
public partial class AddArtist : Form
{
String Name1 = "No Name";
int Members = 0;
public AVLTreetreeAVL = new AVLTree();
public AddArtist()
{
InitializeComponent();
treeAVL = MainForm.treeAVL;
}
private void MainForm_Load(object sender, EventArgs e)
{
}
private void butAdd_Click(object sender, EventArgs e)
{
Name1 = tBName.Text;
Members = (Convert.ToInt32(tBMem.Text));
Artist newArtist = new Artist(Name1,Members);
try
{
treeAVL.InsertItem(newArtist);
}
catch (Exception )
{
MessageBox.Show("No Data Entered", "Error",MessageBoxButtons.OK, MessageBoxIcon.Error);
}
tBName.Text = "";
tBMem.Text = " ";
}
}
}
Any help would be greatly appreciated pointing out where im going wrong or how to solve it.
It now compiles however it gives an error of Object reference not set to an instance of an object. i hope ive gone about coding this is the right way.
What is the access modifier of AVLTree class? Check if it is private or internal, since your code needs it to be public.
Set public on your parametrized type
public class Artist
{
..
}