Forms & variables & suchlike
-
Hiya. I'm using Visual C# - does anyone know how to declare a variable in say FrmMain - int myVar = 6; and then in a child of FrmTempTools set myVar to a value so i can read it again in my main form? I have tried and can only get meesages such as undeclared variable in FrmTempTools. Thanks anyone, surgeproof.
-
Hiya. I'm using Visual C# - does anyone know how to declare a variable in say FrmMain - int myVar = 6; and then in a child of FrmTempTools set myVar to a value so i can read it again in my main form? I have tried and can only get meesages such as undeclared variable in FrmTempTools. Thanks anyone, surgeproof.
Make the variable as public, or create a public getter property for the variable. Then just pass the Form instance to your FrmTempTools child.
public class FrmMain : Form
{
public int myVar = 6;public FrmMain()
{
ChangeTheValue();
}private void ChangeTheValue()
{
ChildOfFrmTempTools t = new ChildOfFrmTempTools(this); // pass this classConsole.WriteLine(myVar); // outputs "12"
}
}public class ChildOfFrmTempTools
{
public ChildOfFrmTempTools(FrmMain mainInstance)
{
mainInstance.myVar = 12;
}
}Also note you can use the ref or out keywords to pass the variable by reference, rather than the default of passing by value. For instance
public void Test()
{
int i = 6;
Modify(ref i);
Console.WriteLine(i); // outputs 12
}public void Modify(ref int i)
{
i = 12;
}--------------------------- He who knows that enough is enough will always have enough. -Lao Tsu
-
Make the variable as public, or create a public getter property for the variable. Then just pass the Form instance to your FrmTempTools child.
public class FrmMain : Form
{
public int myVar = 6;public FrmMain()
{
ChangeTheValue();
}private void ChangeTheValue()
{
ChildOfFrmTempTools t = new ChildOfFrmTempTools(this); // pass this classConsole.WriteLine(myVar); // outputs "12"
}
}public class ChildOfFrmTempTools
{
public ChildOfFrmTempTools(FrmMain mainInstance)
{
mainInstance.myVar = 12;
}
}Also note you can use the ref or out keywords to pass the variable by reference, rather than the default of passing by value. For instance
public void Test()
{
int i = 6;
Modify(ref i);
Console.WriteLine(i); // outputs 12
}public void Modify(ref int i)
{
i = 12;
}--------------------------- He who knows that enough is enough will always have enough. -Lao Tsu
thanks. i think i get it now! ;) surgeproof.
-
Make the variable as public, or create a public getter property for the variable. Then just pass the Form instance to your FrmTempTools child.
public class FrmMain : Form
{
public int myVar = 6;public FrmMain()
{
ChangeTheValue();
}private void ChangeTheValue()
{
ChildOfFrmTempTools t = new ChildOfFrmTempTools(this); // pass this classConsole.WriteLine(myVar); // outputs "12"
}
}public class ChildOfFrmTempTools
{
public ChildOfFrmTempTools(FrmMain mainInstance)
{
mainInstance.myVar = 12;
}
}Also note you can use the ref or out keywords to pass the variable by reference, rather than the default of passing by value. For instance
public void Test()
{
int i = 6;
Modify(ref i);
Console.WriteLine(i); // outputs 12
}public void Modify(ref int i)
{
i = 12;
}--------------------------- He who knows that enough is enough will always have enough. -Lao Tsu
On trying to follow through and adapt your code, i only get this error: No overload for method 'bladeblahFormName' takes '1' arguments. Any ideas? thanks.