Nodessa
Nodessa

Reputation: 59

How to get an object reference for the non static field

i have an observable collection that i need to use in static method. How do i make an object reference?

public  ObservableCollection<ArticleDetailData> LastThreeArticles { get; } = new ObservableCollection<ArticleDetailData>();

I have tried

_lastThreeAricles = new ObservableCollection<ArticleDetailData>LastThreeArticles;

Upvotes: 0

Views: 102

Answers (1)

timothyclifford
timothyclifford

Reputation: 6969

Going to assume your collection exists in this class:

public class MyArticlesBrowserViewModel {
  public ObservableCollection<ArticleDetailData> LastThreeArticles { get; } = new ObservableCollection<ArticleDetailData>();
}

Then create a new instance of this class which contains the reference to the collection:

public static void MyStaticMethod() {
  var myArticlesBrowserViewModel = new MyArticlesBrowserViewModel();
  // This is your instance
  var myCollection = myArticlesBrowserViewModel.LastThreeArticles;
}

public static void Clean()
{
  var myArticlesBrowserViewModel= new MyArticlesBrowserViewModel(); 
  myArticlesBrowserViewModel.LastThreeArticles.Clear();
}

Upvotes: 2

Related Questions