Reference Variable from another CS script in MainPage Xaml.CS

jenova1628jenova1628 Member ✭✭
edited December 2018 in Xamarin.Forms

HI, I'm stuck trying to figure out a way to reference a variable in a class I have made but use it in my MainPage.xaml.cs

Basically, I want to create an if statement on my submit button which is in MainPage.xaml.cs but the boolean I want to set to true in the if statement is located in another class called BasicErrorStyle.cs

for better understanding in my BasicErrorStyle.cs my code is:

`public Label errorLabel { get; set; }
        public void ShowError(View view, string message)
        {
            StackLayout layout = view.Parent as StackLayout;
            int viewIndex = layout.Children.IndexOf(view);



            if (viewIndex + 1 < layout.Children.Count)
            {
                View sibling = layout.Children[viewIndex + 1];
                string siblingStyleId = view.Id.ToString();
                // Reuse the existing label
                if (sibling.StyleId == siblingStyleId)
                {
                    errorLabel = sibling as Label;
                    errorLabel.Text = message;
                    errorLabel.IsVisible = true;

                    return;
                }
            }
            // Add new label if none exists
            layout.Children.Insert(viewIndex + 1, new Label
            {
                Text = message,
                FontSize = 10,
                StyleId = view.Id.ToString(),
                TextColor = Color.Red
            });
        }` 

and in my MainPage.xaml.cs is:

`public partial class MainPage : ContentPage
    {
        BasicErrorStyle errorscript = new BasicErrorStyle();

    private voice SubmitButton_Clicked(object sender, EventArgs e)
    {
        if(errorscript.errorLabel.IsVisible == true)
        {
            DisplayAlert("error","errortext","OK");
        }
        }
     }`

All I am getting back when I run it and press submit button is System.NullReferenceException: Object reference not set to an instance of an object.

Tagged:

Best Answer

Answers

Sign In or Register to comment.