Reputation: 29
I am fairly new to C# programming (and programming in general). I want to use a variable in two different methods I thought I needed to declare the variable just inside the class but I keep getting this error message "Error: A field initializer cannot reference the non-static field, method, or property" I am sure it is a relatively simple error on my part, but how do I fix this?
After researching online for awhile I think I am on the right track of understanding classes but my understanding is obviously lacking.`
public partial class MainPage : ContentPage
{
public string path = diceNumber.SelectedItem.ToString();
public MainPage()
{
InitializeComponent();
}
private void DiceRollResult_Clicked(object sender, EventArgs e)
{
if (path == "One")
{
DisplayAlert("One", "You Lost", "Close");
}
else if (path=="Two")
{
DisplayAlert("Two", "You Lost", "Close");
}
else if (path == "Three")
{
DisplayAlert("Three", "You Won", "Close");
}
// The else if statements are just to show you how I am using the code.
Upvotes: 0
Views: 1393
Reputation: 35400
A few things for you to understand here:
diceNumber.SelectedItem
won't even exist at the time when path
is being initialized. That is the cause of your error. You can only use static fields or values to assign to a class-level variable for initialization (because static members do not need an instance).diceNumber.SelectedItem
to your variable upon startup, you probably don't want to do that, because then it would only be executed once upon startup. What you actually want it to do is to check currently selected value at the time of click and then respond accordingly. Therefore you should move your path variable inside the click handler because I don't see you're using it anywhere else.Lastly, if you need to access this value in other functions too, you can create local variables inside those functions just like this:
string path = diceNumber.SelectedItem.ToString();
in all the functions where you need this. No need of a global variable.
diceNumber
(which is probably a UI control) itself is a global class-level variable. So it will do everything that your path
variable is doing for you. Not sure if this is WinForms or WPF or something else, but you can always see the declaration of these UI controls as class-level variables in the code-behind.Upvotes: 4
Reputation: 2446
You can access a variable from any other class or form by making it a static variable.
public static string path = diceNumber.SelectedItem.ToString();
Now in any class or in any method, you can access the variable by var s = MainPage.path;
The static member is always accessed by the class name, not the instance name. Only one copy of a static member exists, regardless of how many instances of the class are created. Static methods and properties cannot access non-static fields and events in their containing type, and they cannot access an instance variable of an object unless it is explicitly passed in a method parameter.
Upvotes: 0
Reputation: 102
This seems to be a Xamarin.Forms application, its not a good practice to use a variable in several methods you would generally use a class and instantiate that class throughout the lifespan of your program.
Upvotes: 0