call function from class

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a program that I've written a class for. I need to call the function
in the program from the class. When I try to call the function I receive the
error, the name xxx does not exist in the class or namespace. Where xxx is
the function being called. How do I call the function?
 
It's hard to tell what the cause of the problem is without code to look at,
but you might want to ensure that the access modifiers are correct for the
function you are calling.

Also, unless the funciton is declared as static, you need to create an
instance of the class before you can call any of its public methods.

Hope this helps,
Francis



--------------------
 
Here is a sample program that does nothing but demonstrate what I'm trying to
ask.The commented out lines are where I tried to pass the function to the
class.

pertinent lines of main prog- One button calls the function from the
program(which works), the other call the class that calls the function(which
is what I need to do);

private void button1_Click(object sender, System.EventArgs e)
{
writeText("Write from form.");
}

private void button2_Click(object sender, System.EventArgs e)
{
//object wtest;
//wtest= writeText;
//tsend.WriteBox(wtest, writeText);
tsend.WriteBox();
}

public void writeText(string stext)
{
richTextBox1.Text= stext;
}

class code;

using System;
using System.Text;

namespace TestCall
{
/// <summary>
/// Summary description for TestClass.
/// </summary>
public class TestClass
{
private static object wobject;

public TestClass()
{
// Test function call to main program
}

//public void WriteBox(object sentObject, string ttext)
public void WriteBox()
{
//sentObject(ttext);

writeText("Message sent from function");//line where I'm trying to call
function.
}
}
}
 
In your example, you're trying to call a function WriteText() from inside a
class where the function is not defined in the class of in any of it's
parents. This will not compile. Here're few ways you can call a function in
another class (c2) from inside your class (c1):
1. Create a member variable of type C2 inside class C1
2. Have C2 inherit from C1
3. Define the method inside C2 as static and then call using the Syntax
Class.Method()
4. Define a delegate of the same type as the function call in your class
C1 and on construction initialize the delegate to the original function
defined.

Hope this helps. If you tell me the exact scenario, I can give you some more
specific pointers
- Shuvro

--
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm.
 
Back
Top