Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET General > Class Library Namespace is hidden


Reply
 
Thread Tools Display Modes
  #1  
Old 01-26-2007, 11:15 AM
Banjo's Avatar
Banjo Banjo is offline
Hell's Angel

Retired Moderator
* Guru *
 
Join Date: Jul 2001
Location: Yorkshire, UK
Posts: 10,394
Default Class Library Namespace is hidden


Hi all,

I'm having a bizarre problem with namespaces. The setup is quite simple. I have a class library with some common functions which lives is a 2nd tier namespace.
This library is used in a program that defines a 2nd tier namespace that lives in the 1st tier namespace used in the library.

Here's the class library code:
Code:
Namespace PSquared Public NotInheritable Class Common Public Shared Function CnvArrayToList(Of T)(ByVal arr() As T) As List(Of T) Return CnvArrayToList(arr, 0, arr.Length) End Function Public Shared Function CnvArrayToList(Of T)(ByVal arr() As T, ByVal Start As Integer) As List(Of T) Return CnvArrayToList(arr, Start, arr.Length - Start) End Function Public Shared Function CnvArrayToList(Of T)(ByVal arr() As T, ByVal Start As Integer, ByVal Length As Integer) As List(Of T) Dim List As New List(Of T) Dim StartIndex As Integer = Range(Of Integer)(Start, arr.GetLowerBound(0), arr.GetUpperBound(0)) Dim EndIndex As Integer = Range(Of Integer)(Start + Length - 1, arr.GetLowerBound(0), arr.GetUpperBound(0)) For i As Integer = StartIndex To EndIndex List.Add(arr(i)) Next Return List End Function End Class End Namespace
And the main project:
Code:
Namespace PSquared.Talkback Public Class CTalkbackService Inherits System.ServiceProcess.ServiceBase Protected Overrides Sub OnStart(ByVal args() As String) ' Add code here to start your service. This method should set things ' in motion so your service can do its work. End Sub Protected Overrides Sub OnStop() ' Add code here to perform any tear-down necessary to stop your service. End Sub End Class End Namespace
When I rename the main app namespace to something else the PSquared.Common class is visible. However, with the PSquared.Talkback namespace in place the stuff from the library is unavailable.

Oddly, if I remove the namespace statement and use the project root namespace setting instead then it works.

What am I missing here?
__________________
A wise one man once said "what you talking about dog breath"
Reply With Quote
  #2  
Old 01-28-2007, 03:37 PM
Iceplug's Avatar
Iceplug Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: California, USA
Posts: 16,583
Default

Looks like the PSquared.Talkback name overrides the existing PSquared and it only expects .Talkback to be a member.

Maybe you should try Importing PSquared into the PSquared.Talkback namespace (I don't know if it will work though).

My initial thought is that your PSquared.Talkback namespace should be in the PSquared namespace.
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #3  
Old 02-02-2007, 05:00 AM
Banjo's Avatar
Banjo Banjo is offline
Hell's Angel

Retired Moderator
* Guru *
 
Join Date: Jul 2001
Location: Yorkshire, UK
Posts: 10,394
Default

Obviously having all of the PSquared namespace code in one file would make it work, but if my understanding of how namespaces are supposed to work, doesn't that defeat the point.

I thought it was supposed to allow this sort of namespace extension, effectively merging code from different files into a namespace.

The odd thing is that using the default namespace instead of an explicit one works fine. I had thought that the default namespace was simply a shortcut to avoid having to put namespace statements throughout your code. Obviously it's a little complex than that.
__________________
A wise one man once said "what you talking about dog breath"
Reply With Quote
  #4  
Old 02-02-2007, 10:23 AM
shaul_ahuva shaul_ahuva is offline
Ultimate Contributor

Retired Leader
* Expert *
 
Join Date: Jul 2003
Location: Camp Hill, PA
Posts: 1,992
Default

In VB, explicitly defined namespaces live under the root namespace defined in the project file.

For example, if your library's name is "CommonLib" then the default root namespace assigned by Visual Studio is "CommonLib" - you would need to change the root namespace to "PSquared" and define your "Common" class in the root namespace. Your "main application" project should then have the root namespace "PSquared.Talkback".

See the attached solution for an example.
Attached Files
File Type: zip PSquared.zip (6.3 KB, 1 views)
Reply With Quote
  #5  
Old 02-09-2007, 03:45 AM
Banjo's Avatar
Banjo Banjo is offline
Hell's Angel

Retired Moderator
* Guru *
 
Join Date: Jul 2001
Location: Yorkshire, UK
Posts: 10,394
Default

ok, I get it. So there's no way to break out of the root namespace using the Namespace command in code.
__________________
A wise one man once said "what you talking about dog breath"
Reply With Quote
  #6  
Old 02-09-2007, 03:48 AM
shaul_ahuva shaul_ahuva is offline
Ultimate Contributor

Retired Leader
* Expert *
 
Join Date: Jul 2003
Location: Camp Hill, PA
Posts: 1,992
Default

In VB2005 you can clear the root namespace for the project, but this means you'll need to explicitly define each namespace you want to use. I'm not sure if VB2003 has the same behavior.

Last edited by shaul_ahuva; 02-09-2007 at 03:56 AM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->