i have in the main window variable
example:
public partial class MainWindow : Window
{
internal int i;
public MainWindow()
{
InitializeComponent();
}
}
and i want to use him in child window and for that i do him internal (the two window in the same namespace) and the child window still doesn’t recognize the variable
what i’m suppose to do?
You should create a public property on the child window of type
int. When you create the child window then set that property based on the value of the field in the parent window.It appears that you want to be able to not just read the variable in the child class, but also modify it and have that change reflected in the parent form, so that will complicate the answer.
We’ll need to start off with a helper class. Because the data we’re interested in is an
int(which is a value type) we’ll need something that is a reference type (i.e. a class).So we’ll start by not having an integer in the parent form, but instead an instance of this class:
Next we’ll need a public property on the child form; rather than an
intwe’ll use this new class:Now when we create the child class we’ll set the property based on the value in the parent class:
Now that we’ve done all this we’ve ensure that both the parent and child class have a reference to the same instance of
Wrapper, so any changes to theValueproperty of theWrapperinstance (from either class) will be “seen” by either reference.