469,926 Members | 1,867 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,926 developers. It's quick & easy.

Accessing another namespace

Given the following c# code:

namespace Company2
{
public class SomeFunctions
{
public void FunctionA()
{
// Do Something.
}
}
}
namespace Company1.Company2
{
public class SomeFunctions
{
public void FunctionB()
{
Company2.FunctionA();
}
}
}

In the line: Company2.FunctionA(); FunctionA is undefined because the
compiler looks in Company1.Company2 namespace, not in Company1 namespace. I
need to find a way to call FunctionA from FunctionB, does anyone know how I
can do this ?
Nov 15 '05 #1
5 2229
Mike,
If changing one of the Company2 namespace names is out of the question, you
will need to import (the using statement) namespace Company2 with an alias.

Something like (untested):

using x = Company2;
namespace Company2
{
public class SomeFunctions
{
public void FunctionA()
{
// Do Something.
}
}
}
namespace Company1.Company2
{
public class SomeFunctions
{
public void FunctionB()
{
x.SomeFunctions.FunctionA();
}
}
}
Look up 'using alias directives' in MSDN for more details.

Hope this helps
Jay

"Mike Oliszewski" <mi*************@faxback.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl... Given the following c# code:

namespace Company2
{
public class SomeFunctions
{
public void FunctionA()
{
// Do Something.
}
}
}
namespace Company1.Company2
{
public class SomeFunctions
{
public void FunctionB()
{
Company2.FunctionA();
}
}
}

In the line: Company2.FunctionA(); FunctionA is undefined because the
compiler looks in Company1.Company2 namespace, not in Company1 namespace. I need to find a way to call FunctionA from FunctionB, does anyone know how I can do this ?

Nov 15 '05 #2
The problem I see does not have to do with the namespace, but with the
fact that
you are trying to call a function (FunctionA();) that is not static.
To call this function you first have to make an instance of a class
(Company2.SomeFunctions test = new Company2.SomeFunctions(); )
then use that object to call the variable
(test.FunctionA();)
If you want the function to be called without it requiring to be in a
class, make it static using the "static " key word in front of the
function.

The below code should help you out with this

Best of luck

Andy Renk junker_mail(Remove)@yahoo.com -- to email delete the
"(remove)"

Samle COde
********************
using System;
namespace a
{
class SomeFunctions
{

[STAThread]
static void Main(string[] args)
{
Function(); // calls function in
namespace a
b.SomeFunctions.Function(); // calls function in
namespace b
c.SomeFunctions tempClass = new c.SomeFunctions(); // make
class
//from
namespace c
tempClass.Function(); // ececute call from class in
namespace c
}
public static void Function()
{
Console.WriteLine("This is the Function is namespace a");
}
}
}
namespace b
{
class SomeFunctions
{
static public void Function()
{
Console.WriteLine("This is the Function is namespace b");
}
}
}
namespace c
{
class SomeFunctions
{
public void Function()
{
Console.WriteLine("This is the Function is namespace c");
}
}
}
Nov 15 '05 #3

Hi Andy,

The main problem of the user is really to do with the namespace.
In Company1.Company2, the Company2 namespace is shielded, so if you want to
do like this:
using System;

namespace Company2
{
public class SomeFunctions
{
public void FunctionA()
{
// Do Something.
Console.WriteLine("Company2.SomeFunctions.Function A()");
}
}
}

namespace Company1.Company2
{
public class SomeFunctions
{
public void FunctionB()
{
Company2.SomeFunctions c2=new Company2.SomeFunctions();
c2.FunctionA();
}
}
}

The compiler will generate an error denotes
"'Company1.Company2.SomeFunctions' does not contain a definition for
'FunctionA'".

So you should use "using" alias, like this:

using System;
using x=Company2;

namespace Company2
{
public class SomeFunctions
{
public void FunctionA()
{
// Do Something.
Console.WriteLine("Company2.SomeFunctions.Function A()");
}
}
}

namespace Company1.Company2
{
public class SomeFunctions
{
public void FunctionB()
{
x.SomeFunctions c2=new x.SomeFunctions();
c2.FunctionA();
}
}
}

Hope this helps,

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: ju*********@yahoo.com (Andy Renk)
| Newsgroups: microsoft.public.dotnet.languages.csharp
| Subject: Re: Accessing another namespace
| Date: 21 Oct 2003 17:46:53 -0700
| Organization: http://groups.google.com
| Lines: 66
| Message-ID: <82**************************@posting.google.com >
| References: <#e**************@TK2MSFTNGP12.phx.gbl>
<uj**************@TK2MSFTNGP12.phx.gbl>
| NNTP-Posting-Host: 131.107.3.86
| Content-Type: text/plain; charset=ISO-8859-1
| Content-Transfer-Encoding: 8bit
| X-Trace: posting.google.com 1066783614 5617 127.0.0.1 (22 Oct 2003
00:46:54 GMT)
| X-Complaints-To: gr**********@google.com
| NNTP-Posting-Date: Wed, 22 Oct 2003 00:46:54 +0000 (UTC)
| Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed 00.sul.t-online.de!t-onlin
e.de!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!postnew s1.google.com!no
t-for-mail
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:193046
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| The problem I see does not have to do with the namespace, but with the
| fact that
| you are trying to call a function (FunctionA();) that is not static.
| To call this function you first have to make an instance of a class
| (Company2.SomeFunctions test = new Company2.SomeFunctions(); )
| then use that object to call the variable
| (test.FunctionA();)
| If you want the function to be called without it requiring to be in a
| class, make it static using the "static " key word in front of the
| function.
|
| The below code should help you out with this
|
| Best of luck
|
| Andy Renk junker_mail(Remove)@yahoo.com -- to email delete the
| "(remove)"
|
| Samle COde
| ********************
| using System;
| namespace a
| {
| class SomeFunctions
| {
|
| [STAThread]
| static void Main(string[] args)
| {
| Function(); // calls function in
| namespace a
| b.SomeFunctions.Function(); // calls function in
| namespace b
| c.SomeFunctions tempClass = new c.SomeFunctions(); // make
| class
| //from
| namespace c
| tempClass.Function(); // ececute call from class in
| namespace c
| }
| public static void Function()
| {
| Console.WriteLine("This is the Function is namespace a");
| }
| }
| }
| namespace b
| {
| class SomeFunctions
| {
| static public void Function()
| {
| Console.WriteLine("This is the Function is namespace b");
| }
| }
| }
| namespace c
| {
| class SomeFunctions
| {
| public void Function()
| {
| Console.WriteLine("This is the Function is namespace c");
| }
| }
| }
|

Nov 15 '05 #4
Andy,
Doh! You are correct FunctionA is not a static method, my mistake on the
sample call, if you notice the OP had it mangled even more ;-)

However as Jeffery pointed out, the problem is the OP has Company2 namespace
& Company1.Company2 namespace, the OP wants to use Company2.SomeFunctions
from Company1.Company2.SomeFunctions.

The problem is the OP needs to use a "using alias".

As far as I can tell whether the method is instance or static is immaterial
to the problem.

Just a thought
Jay
"Andy Renk" <ju*********@yahoo.com> wrote in message
news:82**************************@posting.google.c om...
The problem I see does not have to do with the namespace, but with the
fact that
you are trying to call a function (FunctionA();) that is not static.
To call this function you first have to make an instance of a class
(Company2.SomeFunctions test = new Company2.SomeFunctions(); )
then use that object to call the variable
(test.FunctionA();)
If you want the function to be called without it requiring to be in a
class, make it static using the "static " key word in front of the
function.

The below code should help you out with this

Best of luck

Andy Renk junker_mail(Remove)@yahoo.com -- to email delete the
"(remove)"

Samle COde
********************
using System;
namespace a
{
class SomeFunctions
{

[STAThread]
static void Main(string[] args)
{
Function(); // calls function in
namespace a
b.SomeFunctions.Function(); // calls function in
namespace b
c.SomeFunctions tempClass = new c.SomeFunctions(); // make
class
//from
namespace c
tempClass.Function(); // ececute call from class in
namespace c
}
public static void Function()
{
Console.WriteLine("This is the Function is namespace a");
}
}
}
namespace b
{
class SomeFunctions
{
static public void Function()
{
Console.WriteLine("This is the Function is namespace b");
}
}
}
namespace c
{
class SomeFunctions
{
public void Function()
{
Console.WriteLine("This is the Function is namespace c");
}
}
}

Nov 15 '05 #5
My bad,

thank for the correction :)

Andy
Nov 15 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Tonguç Yumruk | last post: by
2 posts views Thread by Kurt Häusler | last post: by
9 posts views Thread by Mike Oliszewski | last post: by
3 posts views Thread by Vivek Sharma | last post: by
3 posts views Thread by AdamM | last post: by
3 posts views Thread by Frederick Gotham | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.