Namespaces in VC++.NET and C#

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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::Namespace1
{
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
 
Ratan said:
using namespace System;
namespase CompName::DomName::TechName::ProductName::Namespace1
{
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
 
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::Namespace1
{
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
 
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
 
Back
Top