So you need to store the strings inside the class and handle the checked event. There's no reason to use globals to do this. If the check box is in a different form, use a delegate to pass on the event.
the checkbox is in another "private void" thingy and is in the same form, it can't access another's private void'S variables (ie: the one for the Convert button), so I can't fetch the "short" and "long" variables.
what I want to do is automaticly refresh the text in textboxes when the user clicks on a specific checkbox. The values to show will already be saved in a variable because the user must have clicked on the "Convert" button at least once before making the checkbox update the textboxes without having to click another time on the "Convert" button. Otherwise, the checkbox wouldn't do anything until the user clicks on the "Convert" button.
I assume you mean the check box event is private. It returns void. Your checkbox cannot be void.
it can't access another's private void'S variables
No function can access the variables that exist solely in the scope of another function.
I take it the check box is on a different form to the text box. If not, then the code you posted just needs to go into the 'private void thingy' which is the event for the checkbox check. If it is, then you need to create a delegate.
http://www.akadia.com/services/dotnet_delegates_and_events.html is one good link on delegates, you can google for more. Basically, the form with the check box defines an event and an instance of that event, which is public. When the check box is checked, the event is called. The form which creates this form is responsible for making sure that this event is tied to a method in the form with the text box, so that it gets called at the right time.
Your code is trying to access a variable that's local to another class. I don't see any reason for this code to be made global. The reason C# does not support global variables is because they are not a good idea. You should create classes for global support only when something must be visible to many classes through the system. This method does not appear to be a candidate for that. If it was, you'd need to do these things
1. Pass the text from txtCelsius and txtFahrenheit into the method as parameters
2. Put your static string declarations outside the method, you can't define variables inside a method to have scope outside it.
Thank you David, I didn't know that I had to specify it but I need to have a "global" variable that need to be processed ; the code you gave me is a class and doesn't allow "regular code" in it.
I tried creating a new function in it but it doesn't work, it may be because I don't know how to (I've searched and found nothing!).
I am new to C# (being coding PHP for 2 years though), thank you for your patience :)
How to global a variable in C#?
Austin Ledbetter
dotnetgruven
Nikolay Yordanov
public class MyClass
{
public static int MyInt = 10;
}
Then you can do this:
MessageBox.Show(MyClass.MyInt.ToString());
CSI-IT
public static MyClass
{
public static void HelloWorld()
{
MessageBox.Show("Hello World!");
}
}
GManNC
Hello Christian,
the checkbox is in another "private void" thingy and is in the same form, it can't access another's private void'S variables (ie: the one for the Convert button), so I can't fetch the "short" and "long" variables.
How can I do this
Thank you!
jonsofield
what I want to do is automaticly refresh the text in textboxes when the user clicks on a specific checkbox. The values to show will already be saved in a variable because the user must have clicked on the "Convert" button at least once before making the checkbox update the textboxes without having to click another time on the "Convert" button. Otherwise, the checkbox wouldn't do anything until the user clicks on the "Convert" button.
Thank you.
P.N
I assume you mean the check box event is private. It returns void. Your checkbox cannot be void.
it can't access another's private void'S variables
No function can access the variables that exist solely in the scope of another function.
I take it the check box is on a different form to the text box. If not, then the code you posted just needs to go into the 'private void thingy' which is the event for the checkbox check. If it is, then you need to create a delegate.
http://www.akadia.com/services/dotnet_delegates_and_events.html is one good link on delegates, you can google for more. Basically, the form with the check box defines an event and an instance of that event, which is public. When the check box is checked, the event is called. The form which creates this form is responsible for making sure that this event is tied to a method in the form with the text box, so that it gets called at the right time.
Tonia
Your code is trying to access a variable that's local to another class. I don't see any reason for this code to be made global. The reason C# does not support global variables is because they are not a good idea. You should create classes for global support only when something must be visible to many classes through the system. This method does not appear to be a candidate for that. If it was, you'd need to do these things
1. Pass the text from txtCelsius and txtFahrenheit into the method as parameters
2. Put your static string declarations outside the method, you can't define variables inside a method to have scope outside it.
tamtaly
Would you mind giving me a snipplet with this code inside the method
Also, is a method the equivalent of a function found in many other languages
Thank you!
if (txtCelsius.Text != "")
{
try
{
double celsius = double.Parse(txtCelsius.Text);
double fahrenheitResult = celsius * 1.8 + 32;
public static string fahrenheitShort = fahrenheitResult.ToString("F1");
public static string fahrenheitLong = fahrenheitResult.ToString();
}
catch
{
MessageBox.Show("Erreur: mesure en degres Celsius invalide!");
}
}
if (txtFahrenheit.Text != "")
{
try
{
double fahrenheit = double.Parse(txtFahrenheit.Text);
double celsiusResult = (fahrenheit - 32) * 0.555555555555555;
public static string celsiusShort = celsiusResult.ToString("F1");
public static string celsiusLong = celsiusResult.ToString();
}
catch
{
MessageBox.Show("Erreur: mesure en degres Fahrenheit invalide!");
}
}
Stephan Meier
I tried creating a new function in it but it doesn't work, it may be because I don't know how to (I've searched and found nothing!).
I am new to C# (being coding PHP for 2 years though), thank you for your patience :)