0

I have class FrontModel contains some fields from XAML:

public class FrontModel()
{
  public static string LoginName { get; set;}
  public static string userPass;
  public static string Domain { get; set;}
}

Into ViewModel I'm try to connect to FrontModel

public class MainViewModel
{
  FrontModel fm = new FrontModel();

  public MainViewModel()
  {
   ...
   fm.LoginName = Environment.UserName.ToString();//error
  }  
}

But I don't have access to my field. What I'm doing here wrong?

enter image description here

I know that LoginName {get; set;} can do directly into MainViewModel and then it's working, but I'm trying to move it to separate class.

4est
  • 3,010
  • 6
  • 41
  • 63
  • Possible duplicate of [Member '' cannot be accessed with an instance reference](https://stackoverflow.com/questions/1100009/member-method-cannot-be-accessed-with-an-instance-reference) – Sinatr Jan 04 '19 at 10:01
  • As a hint, try to google next time for error message, removing private parts. There are typically several questions on SO for each error message already. – Sinatr Jan 04 '19 at 10:03

1 Answers1

2

This is because you are referencing a static property on a non static instance of FrontModel. Try:

FrontModel.LoginName = Environment.UserName.ToString();

Or if the property does not need to be static, remove static.

Alfie
  • 1,903
  • 4
  • 21
  • 32
  • ok, it's no possible to use alias to my class? (like into my example fm) – 4est Jan 04 '19 at 09:54
  • no you can't use `fm.LoginName` because `LoginName` is static, reading this post should clear things up for you https://stackoverflow.com/questions/10795502/what-is-the-use-of-static-variable-in-c-when-to-use-it-why-cant-i-declare-th – Alfie Jan 04 '19 at 09:55