Namespace Question


J

Joe Duchtel

Hello -

The following two files are part of a project in Visual Studio 2008.
I'm a little confused about the way doing the Imports NamespaceA.cTest
is hiding the getValue() from Module Main. This works without the
"NamespaceA." if I specify NamespaceA as the root namespace for the
solution.

I thought I could access both getValue() but I guess I don't
understand something quite right ...

File1.vb:

Imports NamespaceA
Imports NamespaceA.cTest ' <<< This required the "NamespaceA." below

Module Main
Dim lTest As New cTest

Sub Tester()
lTest.getValue(1)

NamespaceA.getValue("") ' <<< Why do I need "NamespaceA."
here???
End Sub
End Module

File2.vb:

Namespace NamespaceA
Module Main
Function getValue(ByVal aA As String) As Single
Return 0.0
End Function
End Module

Class cTest
Function getValue(ByVal aB As Single) As String
Return ""
End Function
End Class
End Namespace

Thanks,
Joe
 
Ad

Advertisements

A

Armin Zingler

Joe said:
Hello -

The following two files are part of a project in Visual Studio 2008.
I'm a little confused about the way doing the Imports NamespaceA.cTest
is hiding the getValue() from Module Main. This works without the
"NamespaceA." if I specify NamespaceA as the root namespace for the
solution.

I thought I could access both getValue() but I guess I don't
understand something quite right ...

File1.vb:

Imports NamespaceA
Imports NamespaceA.cTest ' <<< This required the "NamespaceA." below

Module Main
Dim lTest As New cTest

Sub Tester()
lTest.getValue(1)

NamespaceA.getValue("") ' <<< Why do I need "NamespaceA."
here???

If you import NamespaceA.cTest, the name "getValue" is found inside
cTest first. As getValue is not a shared member, the error message
says that an object reference must be used to access an instance
member.

I guess I know what probably confuses you. You assume that the compiler
doesn't find the correct signature (argument of type String) and therefore
it keeps on searching in the other imported namespaces til it finds
a function matching the signature. But this is not true. It finds
getValue inside cTest, but not with the right signature. Therefore the
error message. No further search is performed in other imported namespaces.

I don't know if this behavior is by design or an implementation fault.
You may have a look in the VB language specification to figure it out:

http://www.microsoft.com/downloads/...d0-f775-40bf-a191-09f5a95ef500&displaylang=en


Anyway, I recommend not using modules because it's a loss of structure IMO.

BTW, if you change the Module to a Class, you'll get the error message
"'getvalue' is ambiguous, imported from the namespaces or types 'NamespaceA.cTest, NamespaceA.cTest2'"

Imports NamespaceA.cTest
Imports NamespaceA.cTest2

Module Main

Sub Tester()

getValue("") '<<<<< error BC30561
End Sub
End Module


Namespace NamespaceA
Class cTest2
Public Function getValue(ByVal aA As String) As Single
Return 0.0
End Function
End Class

Class cTest
Function getValue(ByVal aB As Single) As String
Return ""
End Function
End Class
End Namespace
 
Ad

Advertisements

J

Joe Duchtel

Joe Duchtel schrieb:











If you import NamespaceA.cTest, the name "getValue" is found inside
cTest first. As getValue is not a shared member, the error message
says that an object reference must be used to access an instance
member.

I guess I know what probably confuses you. You assume that the compiler
doesn't find the correct signature (argument of type String) and therefore
it keeps on searching in the other imported namespaces til it finds
a function matching the signature. But this is not true. It finds
getValue inside cTest, but not with the right signature. Therefore the
error message. No further search is performed in other imported namespaces.

I don't know if this behavior is by design or an implementation fault.
You may have a look in the VB language specification to figure it out:

http://www.microsoft.com/downloads/details.aspx?FamilyID=39de1dd0-f77....

Anyway, I recommend not using modules because it's a loss of structure IMO.

BTW, if you change the Module to a Class, you'll get the error message
"'getvalue' is ambiguous, imported from the namespaces or types 'NamespaceA.cTest, NamespaceA.cTest2'"

Imports NamespaceA.cTest
Imports NamespaceA.cTest2

Module Main

   Sub Tester()

      getValue("")    '<<<<< error BC30561
   End Sub
End Module

Namespace NamespaceA
   Class cTest2
      Public Function getValue(ByVal aA As String) As Single
         Return 0.0
      End Function
   End Class

   Class cTest
      Function getValue(ByVal aB As Single) As String
         Return ""
      End Function
   End Class
End Namespace

Hello -

Thanks so much for all the information and taking time to respond!!!
I inherited that code and I agree that the Module isn't the best
solution but I can just have users add the appropriate root namespace
to their solutions to make this work.

Thanks again,
Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top