1

i start my programm with a form called "mainWindow" in this form i have a button and this run a function that open a new form as object. my problem is i cant access a control on this object or a function on this object and i dont understand why

the first form:

public partial class mainWindow : Form
{
    public mainWindow()
    {
        InitializeComponent();
        this.drawDriverGrid();
    }

    public void drawDriverGrid() 
    {
        Form driverGridForm = new driverGridView();
        driverGridForm.GetSelected();
    }

this is the second form

public partial class driverGridView : Form 
{
    public driverGridView() 
    {
        InitializeComponent();
    }

    public int GetSelected() 
    {
        return 1;
    }
}

i cant run the GetSelected function

7
  • 1
    You declare a Form called driverGridForm, but your class is actually a driverGridView. If you want to access members of a driverGridView than your instance has to be of that type. Commented Jan 16, 2018 at 22:23
  • 1
    For your future Stack Overflow usage: Never say "I can't do X". We know you can't do X; if you could, you wouldn't be asking the question. Say what is happening: "When I tried I got this error BLAH BLAH BLAH on this line, and I tried debugging it like this, and here's what happened..." In this case it is obvious what is wrong, but if it were not obvious, "I can't make it work" gives us nothing to go on to try to diagnose the problem. Commented Jan 16, 2018 at 22:28
  • You need to get an instance to the form class. See my two form project : stackoverflow.com/questions/34975508/… Commented Jan 16, 2018 at 22:29
  • 1
    @jdweng: The use has an instance; read the given code. Commented Jan 16, 2018 at 22:30
  • WHile you're at it, look up standard coding conventions (learn.microsoft.com/en-us/dotnet/standard/design-guidelines/…) - class names should ideally start with a capital letter e.g. MainWindow, DriverGridView Commented Jan 16, 2018 at 22:33

1 Answer 1

4

The compiler must be informed (HA HA) that you intend to use a method of driverGridView. So NOT:

    Form driverGridForm = new driverGridView();
    driverGridForm.GetSelected();

By saying "Form" in the declaration of driverGridForm you are saying "I only wish to use methods of Form with this variable".

Rather, you intended to say "I wish to use methods of driverGridView with this variable", so:

    driverGridView driverGridForm = new driverGridView();
    driverGridForm.GetSelected();

Or, better:

    var driverGridForm = new driverGridView();
    driverGridForm.GetSelected();

which means "let the compiler deduce from the new expression what type I meant here".

Note also that C# naming conventions are that class names and namespace names and method names start with a capital, so it should be

    var driverGridForm = new DriverGridView();
    driverGridForm.GetSelected();

and it should be namespace Test and so on.

Sign up to request clarification or add additional context in comments.

4 Comments

Could you please clarify why "var driverGridForm" is better than "driverGridView driverGridForm"
@DaniDev its redundant declare the type in that context. var exists so that you can write var foo = new DateTime() instead of repeating yourself DateTime foo = new DateTime()
understand that part, It's "prettier". Is there any compiler or runtime benefit?
@DaniDev: The feature is useful when dealing with anonymous types because they don't have names. However, anonymous types are now deprecated in favor of tuples in C# 7. In non-anonymous situations, var is just a "sugar"; it makes the code easier to read because it removes redundancy and de-emphasizes what is usually an unimportant detail. If you find that using var makes the code harder to understand then don't use it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.