Calling function in Class from button1_click ? 
Author Message
 Calling function in Class from button1_click ?
First off, I'm new to C#...  I've done extensive coding in other languages
but I'm just breaking into .net.

I have a project that starts up Form2.  I have a button on that form that I
wish to call a function that is currently in Class1.  The compiler complains
that 'PingHost' does not exist in the class or namespace 'Ping.Form2', so I
obviously haven't done an adequate job of importing the namespaces required
for it to find the code in the Class1.  Please help!  Thanks in advance.
Code snippets are below:

From Form2:

  private void button1_Click(object sender, System.EventArgs e)
  {
   const string host = www.yahoo.com;
   PingHost(host) ;
  }

From Class1:

public static void PingHost(string host)
{
...some code here to ping host and messagebox.show the result...

Quote:
}




Mon, 23 May 2005 00:52:55 GMT  
 Calling function in Class from button1_click ?

Quote:
> From Form2:

>   private void button1_Click(object sender, System.EventArgs e)
>   {
>    const string host = www.yahoo.com;
>    PingHost(host) ;
>   }

> From Class1:

> public static void PingHost(string host)
> {
> ...some code here to ping host and messagebox.show the result...
> }

You're almost there! Try:

 Class1.PingHost(host);

The static method PingHost is a method of Class1, therefore when you call
that method you need to indicate this by prepending the class name.

HTH

Tobin



Mon, 23 May 2005 01:00:36 GMT  
 Calling function in Class from button1_click ?
Craig,

    When you create the instance of Form2, pass in through the constructor
the instance of form 1 that has the PingHost method.  Then, in the Form2
instance call the PingHost method on the reference to Form1 that was passed
in through the constructor.

    Hope this helps.

--
               - Nicholas Paldino [.NET/C# MVP]


Quote:
> First off, I'm new to C#...  I've done extensive coding in other languages
> but I'm just breaking into .net.

> I have a project that starts up Form2.  I have a button on that form that
I
> wish to call a function that is currently in Class1.  The compiler
complains
> that 'PingHost' does not exist in the class or namespace 'Ping.Form2', so
I
> obviously haven't done an adequate job of importing the namespaces
required
> for it to find the code in the Class1.  Please help!  Thanks in advance.
> Code snippets are below:

> From Form2:

>   private void button1_Click(object sender, System.EventArgs e)
>   {
>    const string host = www.yahoo.com;
>    PingHost(host) ;
>   }

> From Class1:

> public static void PingHost(string host)
> {
> ...some code here to ping host and messagebox.show the result...
> }



Mon, 23 May 2005 00:53:17 GMT  
 Calling function in Class from button1_click ?
Tobin provided me with a very basic solution, however you've given me some
more advanced research to do.  I'm researching a couple of the items you
mentioned to keep learning!

Thanks for the prompt reply



Quote:
> Craig,

>     When you create the instance of Form2, pass in through the constructor
> the instance of form 1 that has the PingHost method.  Then, in the Form2
> instance call the PingHost method on the reference to Form1 that was
passed
> in through the constructor.

>     Hope this helps.

> --
>                - Nicholas Paldino [.NET/C# MVP]



> > First off, I'm new to C#...  I've done extensive coding in other
languages
> > but I'm just breaking into .net.

> > I have a project that starts up Form2.  I have a button on that form
that
> I
> > wish to call a function that is currently in Class1.  The compiler
> complains
> > that 'PingHost' does not exist in the class or namespace 'Ping.Form2',
so
> I
> > obviously haven't done an adequate job of importing the namespaces
> required
> > for it to find the code in the Class1.  Please help!  Thanks in advance.
> > Code snippets are below:

> > From Form2:

> >   private void button1_Click(object sender, System.EventArgs e)
> >   {
> >    const string host = www.yahoo.com;
> >    PingHost(host) ;
> >   }

> > From Class1:

> > public static void PingHost(string host)
> > {
> > ...some code here to ping host and messagebox.show the result...
> > }



Mon, 23 May 2005 02:08:48 GMT  
 Calling function in Class from button1_click ?
Thanks so much, I *was* so close!  That made complete sense, and to confirm
I read a couple great FAQ on classes, namespaces, etc.  and its all clear
now.

Thanks again for your prompt reply.


Quote:


> > From Form2:

> >   private void button1_Click(object sender, System.EventArgs e)
> >   {
> >    const string host = www.yahoo.com;
> >    PingHost(host) ;
> >   }

> > From Class1:

> > public static void PingHost(string host)
> > {
> > ...some code here to ping host and messagebox.show the result...
> > }

> You're almost there! Try:

>  Class1.PingHost(host);

> The static method PingHost is a method of Class1, therefore when you call
> that method you need to indicate this by prepending the class name.

> HTH

> Tobin



Mon, 23 May 2005 02:07:51 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. destructor is called before copy-constructor on temporary class (allocated on stack during function call)

2. newbie One class calling functions in another class

3. Inherited class function calls from a pointer assigned to the base class

4. Condition breakpoints and function calls and function calls in Immediate window

5. Problems to call a function of another class

6. How To: Managed C++ class calling/wrapping C functions and structs

7. Compiler error on call to base class function

8. Calling a base class virtual function

9. ?calling function through pointer in a class

10. calling form class functions (?)

11. How to include a function call by multiple Classes

12. CreateThread calls class member function

 

 
Powered by phpBB® Forum Software