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

Namespaces in VC++.NET and C#

Hi All,

I created a .NET class library in C# (VS 2005 Beta 2) sometime back. I used
to group my interfaces and classes in namespaces like-

abc.cs
--------
namespace CompName.DomName.TechName.ProductName.Namespace1
{
public class/interface xyz
{
method1();
method2();
}
}
where there is no namespace like only CompName or DomName or TechName or
ProdName or combination of these. Actual namespace containing
interfaces/classes is only Namespace1 or Namespace2.
The above strategy compiled and worked fine.
Now, my client has said to convert the code to VC++ .NET (VS 2005 Beta2).

Now I am declaring the same C# code in C++ like-

abc.cpp
----------
#include "StdAfx.h"
#include "abc.h"

abc.h
-------
using namespace System;
namespase CompName::DomName::TechName::ProductName::Namespac e1
{
public class/interface xyz
{
method1();
method2();
}
}
This gives compiler error "Namespace1 is not a valid class/namespac".

Can anybody help me? How to do it in VC++.NET.

Regards,
RS
Sep 8 '05 #1
3 1539
"Ratan" <Ra***@discussions.microsoft.com> wrote in message
news:A8**********************************@microsof t.com...
using namespace System;
namespase CompName::DomName::TechName::ProductName::Namespac e1
{
public class/interface xyz
{
method1();
method2();
}
}
This gives compiler error "Namespace1 is not a valid class/namespac".


Oh the joy. It's so long since I've done any managed C++ I'd forgotten about
this little gem. Assuming this hasn't been fixed, and from your post it
looks like it hasn't, here's how you do it. Actually, here's how you do it
in C++ generally, not just MC++.

namespace A
{
namespace B
{
namespace C
{
//Class Def
}
}
}

This will define a class in the namespace A::B::C. Joyous isn't it :)

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton
Sep 8 '05 #2
Hi!
Now I am declaring the same C# code in C++ like-

abc.cpp
----------
#include "StdAfx.h"
#include "abc.h"

abc.h
-------
using namespace System;
namespase CompName::DomName::TechName::ProductName::Namespac e1
{
public class/interface xyz
{
method1();
method2();
}
}
This gives compiler error "Namespace1 is not a valid class/namespac".


Try this:

namespace CompName
{
namespace DomName
{
namespace TechName
{
namespace ProductName
{
namespace Namespace1
{
public class/interface xyz
{
method1();
method2();
}
}
}
}
}
}

A bit nasty, isn't it? But according to C++ grammar in VC++ help there's
no other way... Or maybe I'm wrong.

Cheers,
Piotrek

Sep 8 '05 #3
Hi,

Thanks to both of you Tim Haughton and Piotr Szukalski.
I was doing that way but wanted to confirm if that is a good way. Now since
both of you suggested the same technique, it is clear that this is the only
way.

It is very lengthy so Microsoft should support declaring namespaces in the
same way as it is with C#.

Regards,
RS

"Piotr Szukalski" wrote:
Hi!
Now I am declaring the same C# code in C++ like-

abc.cpp
----------
#include "StdAfx.h"
#include "abc.h"

abc.h
-------
using namespace System;
namespase CompName::DomName::TechName::ProductName::Namespac e1
{
public class/interface xyz
{
method1();
method2();
}
}
This gives compiler error "Namespace1 is not a valid class/namespac".


Try this:

namespace CompName
{
namespace DomName
{
namespace TechName
{
namespace ProductName
{
namespace Namespace1
{
public class/interface xyz
{
method1();
method2();
}
}
}
}
}
}

A bit nasty, isn't it? But according to C++ grammar in VC++ help there's
no other way... Or maybe I'm wrong.

Cheers,
Piotrek

Sep 8 '05 #4

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

Similar topics

24
by: Marcin Vorbrodt | last post by:
Here is an example of my code: //Header file #include <vector> using std::vector; namespace Revelation { // class definitions, etc... // class members are of type std::vector }
12
by: Santiago de Compostela | last post by:
Hi The following program doesn't compile on MS VC++ or Bloodshed Dev-C++ #include <iostream> int strlen(const char *in) {
12
by: RA Scheltema | last post by:
Hi all, I have the following code: namespace A { inline void func(int) { ...; } inline void func(float) { ...; } inline void func(char) { ...; } }
4
by: Sean Quinn | last post by:
Recently I've come across an oddity in .NET, going back to a Managed C++ implementation for GUIs since I never really learned how to code in MFC. Any way, I attempted to put some standard code I...
9
by: Patty O'Dors | last post by:
Hi Can somebody please tell me what namespaces are actually for? I notice that when I start a new project in C#, it puts everything in a namespace of the same name as the project. I found them...
2
by: Chris Bearchell | last post by:
I am trying to get browse information for a VS.net 2003 C++ project, however I am running into trouble resolving methods within a namespace. From within the object browser, all methods within a...
0
by: kashiDeveloper | last post by:
-------------------------------------------------------------------------------- Hi! Plz anybody can give me idea about using Different My-own-created Namespaces in a single VC++.NET...
3
by: Ratan | last post by:
Hi All, I created a .NET class library in C# (VS 2005 Beta 2) sometime back. I used to group my interfaces and classes in namespaces like- abc.cs -------- namespace...
11
by: jakester | last post by:
I am using Visual C++ 2007 to build the code below. I keep getting linkage error. Could someone please tell me what I am doing wrong? The code works until I start using namespace for my objects. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...

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.