This should be quite simple really – not sure what the problem is.
I have a C# Class (Public.cs) and a windows form (Form1.cs). Through a function in Public.cs, I want to get the value of a control on Form1 (without having to use object parameters).
// This code appears in Public.cs
public string MyFunction(int num_val)
{
if (chk_num.checked == true)
{
// Something here...
}
}
The issue is that my class cannot find the control on my form. Is there some way that I must reference it in C#?
Thank you.
I would strongly suggest exposing the
Checkedproperty via a specific property on Form1 (perhaps with a more meaningful name). This will help to hide the implementation details (i.e. control structure) of the Form1 from it’s caller and instead expose only the logic that is required for other consumers to do their jobFor example:
Or alternatively, if you still really want to access the control directly, from the designer you can select the control and change it’s
Modifierproperty to public (or something else) enabling you to access the control object using the code you originally wrote above.EDIT: (Response based on comment)
Public.cs will still need a reference to Form1 and then will call the
IsNumberRequestedproperty of that object.Alternatively, you could pass the form as a parameter to the
MyFunctiontoo rather than using it as an instance variable.