473,396 Members | 1,678 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,396 software developers and data experts.

Changing Namespace at Runtime


Hi.

I want to change the used namespace at runtime depending on another
variable, but I'm not sure how to does this, if possible.

The code below will not compile, but I hope it gives you an idea of
what I'm trying to accomplish.

I guess it has to do with how namespaces are only defined withing a
block, but I have know idea how to do it in another way.
Cheers,

Kasper Kristensen
// ---------------------- CODE--------------------------
namespace A
{
int test=1;
}

namespace B
{
int test=2;
}

int main(int argc, char* argv[])
{
bool useA=true;

if(useA)
using namespace A;
else
using namespace B;

cout << test << "\n";

return 0;
}
Jul 23 '05 #1
4 2509
"Kasper Heftholm Kristensen" <no****@nospam.invalid> wrote in message
news:gs********************************@4ax.com...
I want to change the used namespace at runtime depending on another
variable, but I'm not sure how to does this, if possible.


It's not possible, any more than it is possible to change the type of a
variable at runtime.

Consider:

namespace X { int a; }
namespace Y { double a; }

If you could select a namespace at run time, you could also select at run
time whether the variable "a" has type int or double.
Jul 23 '05 #2

"Kasper Heftholm Kristensen" <no****@nospam.invalid> wrote in message
news:gs********************************@4ax.com...

Hi.

I want to change the used namespace at runtime depending on another
variable, but I'm not sure how to does this, if possible.

The code below will not compile, but I hope it gives you an idea of
what I'm trying to accomplish.

I guess it has to do with how namespaces are only defined withing a
block, but I have know idea how to do it in another way.
Cheers,

Kasper Kristensen
// ---------------------- CODE--------------------------
namespace A
{
int test=1;
}

namespace B
{
int test=2;
}

int main(int argc, char* argv[])
{
bool useA=true;

if(useA)
using namespace A;
else
using namespace B;

cout << test << "\n";

return 0;
}


You could come close to this effect using inheritance and classes...

struct testInterface {
virtual int test() = 0;
};

struct testInterfaceA : public testInterface {
virtual int test() { return 1; };
};

struct testInterfaceB : public testInterface {
virtual int test() { return 2; };
};
or you could

{
int test = 0;
bool useA=true;
if (useA)
test = A::test;
else
test = B::test;
cout << test << "\n";
}
If it can be decided on compile-time, it would be much easier to solve this.
Depending on your need, you could have a function pointer, a struct or class
with members, or a pointer to some data, which you can change at runtime..

JM
Jul 23 '05 #3
On Tue, 8 Feb 2005 21:04:20 +0100, "Jesper Madsen"
<ba***@mail.stofanet.dk> wrote:

You could come close to this effect using inheritance and classes...

struct testInterface {
virtual int test() = 0;
};

struct testInterfaceA : public testInterface {
virtual int test() { return 1; };
};

struct testInterfaceB : public testInterface {
virtual int test() { return 2; };
};
or you could

{
int test = 0;
bool useA=true;
if (useA)
test = A::test;
else
test = B::test;
cout << test << "\n";
}
If it can be decided on compile-time, it would be much easier to solve this.
Depending on your need, you could have a function pointer, a struct or class
with members, or a pointer to some data, which you can change at runtime..

JM


Hey, another Dane. Small "world" ;-)

Unfortunately I cannot change the design like this since it's given by
the middleware I use and it uses namespaces for "encapsuling" the
data.

But I have a stupid work around (that doesn't involve me) to do it
without the need for chancing namespace.

But thanks anyway.

Kasper

Jul 23 '05 #4
On Tue, 08 Feb 2005 19:18:31 GMT, "Andrew Koenig" <ar*@acm.org> wrote:
"Kasper Heftholm Kristensen" <no****@nospam.invalid> wrote in message
news:gs********************************@4ax.com.. .
I want to change the used namespace at runtime depending on another
variable, but I'm not sure how to does this, if possible.


It's not possible, any more than it is possible to change the type of a
variable at runtime.

Consider:

namespace X { int a; }
namespace Y { double a; }

If you could select a namespace at run time, you could also select at run
time whether the variable "a" has type int or double.

Yes, I had a strong suspicion it wouldn't be possible, but it was
worth a try, since it would really help me.
Thanks anyway.

Kasper
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Saradhi | last post by:
Hi Group, This seems a peculiar problem. I am wrirng this code for creating a Remote Server Here I am not able to access the namespace. System.Runtime.Remoting.Channels.Tcp Wts wrong...
7
by: www.dir | last post by:
Hi, I am interested whether there is a significant differense if I put on top of my class using CustomNameSpace or If I call methods in my code by CustomNameSpace.MyMethod() I am interested...
29
by: Natan | last post by:
When you create and aspx page, this is generated by default: using System; using System.Collections; using System.Collections.Specialized; using System.Configuration; using System.Text; using...
4
by: Kevin Newman | last post by:
The primary problem I've had with php is the lack of namespaces, which makes OOP very difficult to organize, since you end up with large number of classes cluttering up the same namespace - which...
1
by: james.cssa | last post by:
When I try to compile the following program on Visual Studio 2005 Pro, the namespace System.Runtime.Remoting.Channels.Tcp is not recongized by the compiler. (The "Tcp" part is highlighted.) And I...
4
by: =?Utf-8?B?REZC?= | last post by:
Within an XSLT transformation, I'm trying to switch the default namespace within a section of the generated XML document to a shared namespace. This way, the content of this section does not have...
17
by: blufox | last post by:
Hi All, Can i change the execution path of methods in my process at runtime? e.g a()->b()->c()->d()->e() Now, i want execution to be altered at runtime as -
5
by: Premkumar | last post by:
a.cpp ---------------------------------- #include <iostream> using namespace std; void mymain() { cout<<"my-main"<<endl; } ----------------------------------
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.