Valhalla Legends Archive

Programming => General Programming => .NET Platform => Topic started by: mentalCo. on February 01, 2005, 05:52 PM

Title: [c#.net] working up the chain [solved]
Post by: mentalCo. on February 01, 2005, 05:52 PM
say i have a class named MyClass on my main form called Form1.  How do I call functions in Form1 from inside MyClass?

edit:

i solved it using events.
Title: Re: [c#.net] working up the chain [solved]
Post by: MyndFyre on February 01, 2005, 08:02 PM
Quote from: mentalCo. on February 01, 2005, 05:52 PM
say i have a class named MyClass on my main form called Form1.  How do I call functions in Form1 from inside MyClass?

edit:

i solved it using events.

Egad!  Why?  Why not just pass your Form instance to MyClass and then give your Form1 class some public instance-level functions?
Title: Re: [c#.net] working up the chain [solved]
Post by: shout on February 01, 2005, 08:15 PM
A code example for MydnFyre's reply:

class MyClass
{
private Form1 whatnot;

public MyClass()
{
whatnot = new Form1();
}

private void blah()
{
whatnot.iamamethod();
}
}
Title: Re: [c#.net] working up the chain [solved]
Post by: MyndFyre on February 01, 2005, 08:29 PM
Quote from: shout on February 01, 2005, 08:15 PM
A code example for MydnFyre's reply:

class MyClass
{
private Form1 whatnot;

public MyClass()
{
whatnot = new Form1();
}

private void blah()
{
whatnot.iamamethod();
}
}


Yeah that's MyndFyre, thanks :P

Anyway, the reason this comes up is because VB6 people are used to just referencing their forms and crap via the name of the form rather than an instance variable.  If you make your MyClass object from within the Form, you can pass the instance of the Form as a parameter via "this".
Title: Re: [c#.net] working up the chain [solved]
Post by: mentalCo. on February 01, 2005, 10:11 PM
i dont know why i didnt think of that.  haha.  thanks.
Title: Re: [c#.net] working up the chain [solved]
Post by: shout on February 02, 2005, 03:57 PM
Sorry MyndFyre, but I am but a human who makes mistakes.