Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, January 21, 2020 2:44 PM
In my main window (called MainWindow) class, at the top level (ie outside the constructor) I have instantiated a class called 'vm'. Inside vm I have a property called MyProperty (for the sake of simplicity).
I tried to read that property in a totally separate class like so: MainWindow.vm.MyProperty
The problem is vm is underlined red and I'm told that MainWindow.vm is inaccessible due to it's protection level. The Mainwindow class is public partial, the default and the class trying to access MainWindow.vm.MyProperty is just public. The property itself is also public and it's not static.
The vm class is a public class but the instance is not set to public, I didn't specify any access level, but if I do set it to public then the error changes and I'm told I need an object reference to access the property.
How can I access that property?
Thanks
All replies (3)
Tuesday, January 21, 2020 4:39 PM âś…Answered | 1 vote
In order to be accessible outside the class that defines it a member must be public. It can be internal if the other code is in the same assembly but this should be rare. It can be protected if you want derived types access only. In your case it sounds like you want it public.
public class ViewModel
{
public string MyProperty { get; set; }
}
public class MainWindow
{
public ViewModel vm { get; set; }
}
//Elsewhere
void DoWork ( MainWindow window )
{
var value = window.vm.MyProperty;
}
But the problem now is that `vm` is not initialized anywhere so it'll crash when you use it. Who creates this value and gives it to the window? If it is the window itself then you'd do this in the constructor and your problems go away.
public class MainWindow
{
//Initialized
public ViewModel vm { get; } = new ViewModel();
}
//Or if init is complex then use the constructor
public class MainWindow
{
public MainWindow ()
{
vm = new ViewModel();
}
public ViewModel vm { get; }
}
//Or have it passed as a parameter to the window
public class MainWindow
{
public MainWindow ( ViewModel vm )
{
//This is why fields should be _ based...
this.vm = vm;
}
public ViewModel vm { get; }
}
Since this clearly looks like a MVVM app I would recommend that you look at your MVVM library's documentation on how VMs are initialized and follow that approach to initialization.
Michael Taylor http://www.michaeltaylorp3.net
Tuesday, January 21, 2020 2:55 PM | 1 vote
Hello,
How is vm declared, a small code sample will help. The base issue is vm most likely needs to be initialized .
Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
NuGet BaseConnectionLibrary for database connections.
Tuesday, January 21, 2020 5:53 PM
Thanks! Passing a mainwindow reference was all I needed to do.