Using Instances in Multiple Locations

Within a Form I am creating an Instance of
c#
public Form()
{
EdgeDriver Driver = new(options)
{
// Url = login_url
Url = $"http://{NVR.IP}/doc/index.html#/portal/login"
};
}
c#
public Form()
{
EdgeDriver Driver = new(options)
{
// Url = login_url
Url = $"http://{NVR.IP}/doc/index.html#/portal/login"
};
}
Then within the Form there is a Button that on Click i would like to
c#
public void Close_FS_Click(object sender, EventArgs e)
{
Driver.Close();
Driver.Quit();
}
c#
public void Close_FS_Click(object sender, EventArgs e)
{
Driver.Close();
Driver.Quit();
}
How can i do this?
9 Replies
Jimmacle
Jimmacle11mo ago
store the instance of EdgeDriver in a field/property in your form you need to do that anyway if you want that class instance to keep existing after your form constructor completes
Christian Dale
Christian DaleOP11mo ago
So make it a Global Variable.
Jimmacle
Jimmacle11mo ago
no, make it a member of your form class that's not global, it only exists in that instance of your form and can't be accessed outside it there is technically no such thing as a global variable in C#, the closest you can get is public static fields/properties
Christian Dale
Christian DaleOP11mo ago
where in the Code would i need to pozition it for it to be classed as a member of the Form Class.
Jimmacle
Jimmacle11mo ago
where do you position your methods to make them a member of the form class? https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/members
Christian Dale
Christian DaleOP11mo ago
what would the Return type be?
Jimmacle
Jimmacle11mo ago
look at the "fields" section in the page i linked
Christian Dale
Christian DaleOP11mo ago
It is the Output that i would need to use multiple times not the Command. If i run the command multiple times it loads the driver multiple times.
Jimmacle
Jimmacle11mo ago
once you store the driver in a field you can run whatever you want as many times as you want
private EdgeDriver Driver;

public Form()
{
Driver = new(options)
{
// Url = login_url
Url = $"http://{NVR.IP}/doc/index.html#/portal/login"
};
}

public void Close_FS_Click(object sender, EventArgs e)
{
Driver.Close();
Driver.Quit();
}
private EdgeDriver Driver;

public Form()
{
Driver = new(options)
{
// Url = login_url
Url = $"http://{NVR.IP}/doc/index.html#/portal/login"
};
}

public void Close_FS_Click(object sender, EventArgs e)
{
Driver.Close();
Driver.Quit();
}
Want results from more Discord servers?
Add your server