I havn't been able to find anything on the web which actually applies to what i'm trying to do yet.

I have form1 which has 2 methods. These methods are the same but two versions, one with 3 variables in its parameter (?), one with 4. It is necessary for my project for it to be a polymorphic override type thing so i'm keeping them in the same form.

On form2 I use the 3 variable parameter.
On form3 I use the 4 variable parameter one (in order to do a very similar piece of working)

BUT I can't work out how to actually do this, how to reference it. I was told once that a form is essentially a class but I can't access the different form's method by saying i.e. firstForm.MyMethod(a, b, c); I also tried using the static reference(?) but that also didn't work and I'm not supposed to use a static method here.

I've tried putting: public firstForm(); at the top where i've observed other sample code on the web, that doesn't seem to work. I can't understand whether it's through delegates as i've also read but I don't really 'get' them tbf.

Is it possible to do what i'm trying to do. If anyone could even suggest a phrase i'm happy to do the googling but really I'm going round in circles.

Cheers

Recommended Answers

All 9 Replies

C# is a class based language, EVERYTHING is in a class. So the ideology here is correct. but you are forgetting that the name "form1" is a reference variable of type "System.Windows.Forms.Form" you can't use a variable if you don't have a reference to it. So what you need to do is pass a reference to one form to the next. you could do that through a property, or constructors, or whatever you feel.

ok cheers for replying, i'll have a stab at that for before I mark it solved. :)

Form2 secondForm = new Form2();
            secondForm.Owner = this;

I put that within a Form1 button click. Is this the wrong approach?
I still havn't been able to access the method. I don't even understand whether the default...

static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }

...which Visual c# 2008 puts in automatically in the program.cs file, creates an instance of the class because it doesn't specify or ask for an object name. It just runs the...

public Form1()
        {
                InitializeComponent();
        }

...is this a constructor? Because I havn't got a clue what to call it to invoke the methods or access the variables.

I don't think I quite understand what you are trying to achieve.

If you want to access the same static methods/functions from a number of places the best way to do this is to create a class like this:

public class MyClass
{
    public static bool MyMethod(float arg)
    {
        return arg > 200f;
    }

    public static bool MyMethod(int arg1, float arg2)
   {
        return (arg1 + arg2) > 200f;
    }

    public static bool MyMethod(params float[] args)
   {
        float total = 0;
        foreach(float arg in args)
        {
             total += arg;
        }

        return total > 200f;
    }
}

Then you can access it easily from anywhere like this:

MyClass.MyMethod(x,y,z);

Somtimes static is not the way to do it though you could create non static methods in a class and reference them using an instance (this is called instantiating) like this:

MyClass instance = new MyClass();

// Now we can access non static methods from that instance
instance.MyMethod(x,y,z);

I don't think I quite understand what you are trying to achieve.

If you want to access the same static methods/functions from a number of places the best way to do this is to create a class like this:

public class MyClass
{
    public static bool MyMethod(float arg)
    {
        return arg > 200f;
    }

    public static bool MyMethod(int arg1, float arg2)
   {
        return (arg1 + arg2) > 200f;
    }

    public static bool MyMethod(params float[] args)
   {
        float total = 0;
        foreach(float arg in args)
        {
             total += arg;
        }

        return total > 200f;
    }
}

Then you can access it easily from anywhere like this:

MyClass.MyMethod(x,y,z);

Somtimes static is not the way to do it though you could create non static methods in a class and reference them using an instance (this is called instantiating) like this:

MyClass instance = new MyClass();

// Now we can access non static methods from that instance
instance.MyMethod(x,y,z);

Your second example is what i'm trying to achieve, the assignment is based in such a way as for us to not use static methods, which is why i'm trying to use the instance methods you've put at the bottom.

However this is not working with my forms. I want to keep all methods and variables in form1. I want to access the same method (myMethod()) from form2 like this - myMethod(a, b, c); and a polymorphic overloaded (?) version myMethod(a,b,c,d); in form3.

But, the problem i've had all along is that I cannot compile because debug complains about various things which come down to the fact that i'm not referencing from one form to another in the correct way, thing is I don't know how to get this working and i've not found any clear explanation anywhere.

I'm still clueless btw!

Oh, do you mean a seperate Class within the namespace(?) ?

If that's what you mean, i'll give it a try, not tried that yet!
cheers

Okay thanks privatevoid that did the job, I created a new class and did it all that way.

Its good that you found a way to get the job done. But its also important that you understand why what you were doing didn't work.

First off. in the program.cs file is where the entry point of the application is placed. This is the Main method. the Application.Run(new Form1()); call creates a message pump with the specified form as the owner of the thread. It accepts an instance of a form as a parameter. There is never a reference variable created that you can see, although the static class Application does hold a reference to it.

from within in object, the keyword "this" refers to that particular instance of the object. So, we can use "this" to refer to our main form. so when you create your new form, give it a varaible of type form1. and expose it as a property, or make it public or assign it using its constructor, whatever. you could do something like

form2 myform2 = new form2();
myform2.form1var = this;

then from inside form two you could

form1var.methodOnForm1();

Its great you found another way to solve your problems, (in fact in most situations putting worker methods in a utility class is the best practice) but accessing methods from different instances is an important part of using C#. try and get it down. you'll be glad you did.

well, thanks for going to the effort to post that Diamonddrake, i'll try and find some time to go over that because that looks like a good explanation, thank you very much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.