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.
"Dave" <Dave@discussions.microsoft.com> wrote in message
news:5E8FEA52-F9D4-4707-941B-FAF64482AEBF@microsoft.com...[color=blue]
> Here is a sample program that does nothing but demonstrate what I'm trying[/color]
to[color=blue]
> 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[/color]
function(which[color=blue]
> 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.
> }
> }
> }
>
>
> "Francis Ingels [MSFT]" wrote:
>[color=green]
> > It's hard to tell what the cause of the problem is without code to look[/color][/color]
at,[color=blue][color=green]
> > but you might want to ensure that the access modifiers are correct for[/color][/color]
the[color=blue][color=green]
> > function you are calling.
> >[/color][/color]