Hey MS 2nd Posting: Accessing another namespace

  • Thread starter Thread starter Kalpesh Shah
  • Start date Start date
K

Kalpesh Shah

If I understand it correctly, you want to call
[Company2].{SomeFunctions}.FunctionA()

FROM

[Company1.Company2].{SomeFunctions}.FunctionB()

I tried on the following code & the IDE shows an error
that FunctionA is not defined, as you said

Now, this is how I solved it
Since the user is Company1.Company2 & the FunctionA is inside Company2
namespace, Add a using statement, see below for the code

using c2 = Company2;

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

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

I hope, this solves the problem & I am in sync with you

Kalpesh
 
Mike,

The function FunctionA is not defined in Company2, but in the class
SomeFunctions in the namespace Company2. Because of this, you will have to
create an instance of SomeFunctions and make the call, like this:

public void FunctionB()
{
// Create the instance.
Company2.SomeFunctions pobjFunctions = new Company2.SomeFunctions();

// Call the method.
pobjFunctions.FunctionA();
}

Hope this helps.
 
I assume you just left off the static keyword. The easiest way to do this
would be to add a using alias to effectively rename the conflicting
namespace:

using GlobalCompany2 = Company2;

namespace Company1.Company2
{
public class SomeFunctions
{
public void FunctionB()
{
GlobalCompany2.SomeFunctions.FunctionA();
}
}
}
 
Shhhhhhhh

Don't tell anyone, It's just the two of us reading this right? He is in
2004!, He's part of the secret government time travel experiments being
conducted on the moon!

BW

-- Roses are red, violets are blue,
I'm schizophrenic
and so am I
 
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 the Company2
namespace. I
need to find a way to call FunctionA from FunctionB, does anyone know how I
can do this ?
 
Oops, I always manage to have bugs in my postings, it's hard to test things
that don't work in the first place. I should have said that
Company2.SomeFunctions.FunctionA() is undefined. But it looks like I got an
answer in the next posting anyway. Thanks.

Nicholas Paldino said:
Mike,

The function FunctionA is not defined in Company2, but in the class
SomeFunctions in the namespace Company2. Because of this, you will have to
create an instance of SomeFunctions and make the call, like this:

public void FunctionB()
{
// Create the instance.
Company2.SomeFunctions pobjFunctions = new Company2.SomeFunctions();

// Call the method.
pobjFunctions.FunctionA();
}

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Mike Oliszewski said:
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 the Company2
namespace. I
need to find a way to call FunctionA from FunctionB, does anyone know
how
I
can do this ?
 
Back
Top