0

I have created a list within this class called myVar...

class FailedIgnoredRetrieved
    {

        public static async Task FailedIgnoredRetrievedAndShow(ListView commentLabel)
        {

            void AllEventsComment(string comment)
            {
                if (commentLabel != null)
                {
                    commentLabel.ItemsSource += comment; //+ "\n";
                }
            }

            AllEventsComment("...");
            var api = new ApiClient();
            var retrieved = ApiToken.Load();
            if (retrieved == null)
            {
                AllEventsComment("Failed, not signed in");
                App.SwitchIcon(NotifyIcons.GreyIcon);
            }
            else
            {
                var result = await api.GetAllEventsAsync(retrieved.Token);
                if (result.IsSuccessful)
                {

                    List<string> myVar = new List<string>();

                    void AddToList(string v)
                    {
                        myVar.Add(v);
                    }

                    foreach (var eventsText in result.Events)
                        if (eventsText.EventStatus == 1)
                        {
                            AddToList($"Red {eventsText.CheckId}");
                        }

                        else if (eventsText.EventStatus == 0)
                        {
                            AddToList($"Orange {eventsText.CheckId}");
                        }
                }
            }
        }
    }     
}

I now want to use myVar List in FailedIgnoredWindow.xaml.cs to bind to for a ListView in FailedIgnoredWindow.xaml I'm struggling to understand how to set it as the ListView.ItemSource = i.e. how do I access the list in the other class?

 public partial class FailedIgnoredWindow : Window
    {


        public FailedIgnoredWindow()
        { 
            InitializeComponent();

            FailedIgnoredDialogue.ItemsSource = 

        }

      

        private async void AllEvents_Click(object sender, RoutedEventArgs e)
        {
            AllEventsWindow win2 = new AllEventsWindow();
            this.Visibility = Visibility.Hidden;
            win2.WindowStartupLocation = 0;
            //win2.Left = 0;
            //win2.Top = 0;
            win2.Show();
            await AllEventsRetrieved.AllEventsRetrievedAndShowCount(win2.AllEventsDialogue);
        }
    }

I've tried to create a seperate class with the properties but I'm struggling to fully understand what I need to do.

James Aston
  • 77
  • 1
  • 4

1 Answers1

1

myVar is declared inside the méthod so it will only be accessible inside this method. If you want to access it from outside, you should make it a public property of the class FailedIgnoredRetrieved (note that in this case if you want to modify the property from the method FailedIgnoredRetrievedAndShow you will have to make it not static)

Another way would be to have the method return the list so you get it as result (the declaration would become Task<List<string>>FailedIgnoredRetrievedAndShow(... ).

There seems to be quite a bit of confusion in your code so I would recommend reading a tutorial about the scope of variable you can start here You should also check this answer about the access modifiers here

I would also recommend staying away from local functions (functions inside functions) at the beginning as I think it adds to the confusion.

Let me know if it makes more sense after reading the links in my answer

Ostas
  • 839
  • 7
  • 11
  • Ostas, thank you for taking the time, that's very helpful...Am I right in thinking functions and methods are the same thing? Now I just need to get my data binding right as currently it's printing one character per line! – James Aston Mar 05 '21 at 19:44
  • Yes a method is a function that belong to a class basically. So for c# the correct term would be method but I often mix the two terms. – Ostas Mar 05 '21 at 19:55