Accessing another namespace

  • Thread starter Thread starter Mike Oliszewski
  • Start date Start date
M

Mike Oliszewski

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 ?
 
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
 
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");
}
}
}
 
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.FunctionA()");
}
}
}

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.FunctionA()");
}
}
}

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: (e-mail address removed) (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: <[email protected]>
| References: <#[email protected]>
<[email protected]>
| 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: (e-mail address removed)
| NNTP-Posting-Date: Wed, 22 Oct 2003 00:46:54 +0000 (UTC)
| Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onlin
e.de!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!postnews1.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");
| }
| }
| }
|
 
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
 
Back
Top