Reputation: 32778
I have the following:
namespace Test {
public class Location {
public string city { get; set; }
}
public class BaseViewModel {
public BaseViewModel() {
Location = new Location { city = "Paris"; };
}
public Location Location { get; set; }
}
public class EditViewModel : BaseViewModel {
public Book Book { get; set; }
Location = "France";
}
}
This seem like a strange requirement but how can I set the value of Location from within the EditViewModel? The line below gives the following error:
Location = "France";
Error 2 Invalid token '=' in class, struct, or interface member declaration
Upvotes: 0
Views: 1416
Reputation: 3252
you just need to change EditViewModel class..
Like this:
public class EditViewModel : BaseViewModel {
public Book Book { get; set; }
public EditViewModel(){
Location = "France";
}
}
*You were trying to set value to the base class property in class's scope.. which is not allowed..
You can do this in constructor on any other method within a class.. As I mentioned above..
Hope this help !
Upvotes: 0
Reputation: 3191
You need to istantiate the object Location, not to set it as a string:
Location = new Location { city = "France" };
and it's better if you rename the property with a different name, do not use the class name.
public class BaseViewModel {
public BaseViewModel() {
CurrentLocation = new Location { city = "Paris"; };
}
public Location CurrentLocation { get; set; }
}
public class EditViewModel : BaseViewModel {
public Book Book { get; set; }
public EditViewModel() : base()
{
CurrentLocation = new Location { city = "France"; };
}
}
Upvotes: 0
Reputation: 6863
Add a constructor.
public EditViewModel () {
Location = "France";
}
or better add a specialized constructor and a default constructor:
public EditViewModel (string LocationText) {
Location = LocationText;
}
public EditViewModel() : this("France") { }
just to be precise, don't use string, use your Object, I've used string for quickening.
Upvotes: 1