2

I have a class library project named MyWidget, with the only class being named MyWidget.

In another project, I've added a reference to my class library, and in my new class, I've tried typing in

Imports MyWidget

and

Imports MyWidget.MyWidget

However, later in my class when I try to create a new reference, Visual Studio is not allowing me to type in this:

Private widget As MyWidget

However, Visual Studio is giving me a "Type Expected." error and forcing me to also include the namespace, like so:

Private widget As MyWidget.MyWidget

I read the MSDN documentation regarding the Imports statement. I should be able to leave off the namespace when declaring the object because I have the imports statement at the top of the program. I've tested this with standard namespaces, and it works fine, but when I try it out with my class, it doesn't.

Am I missing something in the MyWidget class that will allow me to leave off the namespace when declaring the object?

I also tried renaming the namespace to MyClasses, thinking maybe that Visual Studio was getting the namespace confused with the class. However, even with

Imports MyClasses.MyWidget

I still get an error when trying to define a MyWidget object without the MyClasses Namespace.

6
  • Are you adding the Imports MyWidget statement at the top of every file that uses it? Commented Dec 5, 2013 at 16:12
  • Yes. It's just a single class where I'm trying to reference it. It's odd that it works with standard .NET namespaces. I have System.Text.RegularExpressions imported at the top of my class and I'm able to reference the Regex class without the namespace. Seems like it should work with my class as well. Commented Dec 5, 2013 at 16:21
  • Are you importing both MyWidget and MyWidget.MyWidget, or are you saying that you have tried it with both individually and neither worked? Commented Dec 5, 2013 at 16:22
  • 1
    Don't import the class name. Just import the namespace and then it will work. Commented Dec 5, 2013 at 16:28
  • 1
    That worked! Lesson of the day: Keep namespaces and class names different. Thanks @StevenDoggart! Commented Dec 5, 2013 at 16:32

2 Answers 2

5

Since the Namespace and Class have the same name the compiler gets confused when you try to instantiate MyWidget, despite the Imports statement. Just because there is an Imports statement, doesn't mean you can't fully quanlify a type (even if you have Imports System.IO.File, you can still call System.IO.File.WriteAllText), thus the confusion on the compilers end. An alternative would be to use an Alias.

Imports AWidget = MyWidget.MyWidget

Then..

Dim objWidget As New AWidget
Sign up to request clarification or add additional context in comments.

2 Comments

Or, even better, don't use the same name for the class and the namespace.
I tried it with the namespace and the class having different names, and I'm still seeing the same results.
1

It appears that the issue was the namespace and the class having the same name. After changing the namespace to MyClasses and the class to MyWidget, the following statements worked:

Imports MyClasses

...

Private widget as MyWidget

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.