By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,548 Members | 1,735 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,548 IT Pros & Developers. It's quick & easy.

call function from class

P: n/a
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?
Nov 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Can you post a sample of the code you're trying to run?

- 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" <Da**@discussions.microsoft.com> wrote in message
news:84**********************************@microsof t.com...
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?

Nov 17 '05 #2

P: n/a
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

--------------------
Thread-Topic: call function from class
thread-index: AcU0muD2nLfUFAO1QZifwaqXRl8b3w==
X-WBNR-Posting-Host: 68.59.221.147
From: "=?Utf-8?B?RGF2ZQ==?=" <Da**@discussions.microsoft.com>
Subject: call function from class
Date: Tue, 29 Mar 2005 12:07:05 -0800
Lines: 4
Message-ID: <84**********************************@microsoft.co m>
MIME-Version: 1.0
Content-Type: text/plain;
charset="Utf-8"
Content-Transfer-Encoding: 7bit
X-Newsreader: Microsoft CDO for Windows 2000
Content-Class: urn:content-classes:message
Importance: normal
Priority: normal
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
Newsgroups: microsoft.public.dotnet.languages.csharp
Path: TK2MSFTNGXA03.phx.gbl
Xref: TK2MSFTNGXA03.phx.gbl microsoft.public.dotnet.languages.csharp:316503
NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.1.29
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

I have a program that I've written a class for. I need to call the functionin the program from the class. When I try to call the function I receive theerror, the name xxx does not exist in the class or namespace. Where xxx is
the function being called. How do I call the function?


Nov 17 '05 #3

P: n/a
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.
}
}
}
"Francis Ingels [MSFT]" wrote:
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.

Nov 17 '05 #4

P: n/a
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" <Da**@discussions.microsoft.com> wrote in message
news:5E**********************************@microsof t.com...
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.
}
}
}
"Francis Ingels [MSFT]" wrote:
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.

Nov 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.