473,405 Members | 2,373 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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 2435
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Tonguç Yumruk | last post by:
I'm trying to build a completely plug-in based system. One of my problems is importing a package dynamically. I'm trying to emulate the import command. The __import__() function or imp module...
2
by: Kurt Häusler | last post by:
Hi. I have am using c# and the outlook object model to access the outlook address book, but once you have more than a 100 or so contacts in the folder it takes far too long to iterate through...
4
by: Hans De Winter | last post by:
Why does my compiler complain with the message "`int*My::Test::_shape' is private" when I try to compile the following code? It seems it has something to do with the namespace since when I leave...
9
by: Mike Oliszewski | last post by:
Given the following c# code: namespace Company2 { public class SomeFunctions { public void FunctionA() { // Do Something. }
3
by: Vivek Sharma | last post by:
Hi, I have created a dropdownlist as a web user control. I am using its multiple instances on the webpage. How do I access the selectedValue of each instance? All the instances have different...
3
by: AdamM | last post by:
Hi all, When I run my VbScript, I get the error: "ActiveX component can't create object: 'getobject'. Error 800A01AD". Any ideas what I did wrong? Here's my VBScript: dim o set...
1
by: Asfar | last post by:
Here is my problem: In file form1.h I have the following code: #pragma once #include "Test.h" namespace AccessCheck { using namespace System; using namespace System::ComponentModel; using...
3
by: Frederick Gotham | last post by:
Back in the day, if you wanted a function to be self-contained within a translation unit, you defined the function as "static". If there were an external linkage function by the same name...
2
by: Smithers | last post by:
Using 3.5, I am stuck in attempting to: 1. Dynamically load an assembly 2. Instantiate a class from that assembly (the client code is in a different namespace than the namespace of the...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.