Q: "Type Expected"

  • Thread starter Thread starter Geoff Jones
  • Start date Start date
G

Geoff Jones

Hi

I'm trying to use a class that I've written in a form. Unfortunately, when I
write something like:

Public x As New myClass

I get the compile time error: "Type Expected". Indeed, the "squiggly" blue
line does appear under myClass so I half expeced it. However, I had written

Imports myClass

in the form so I thought it would work.

Can anybody help? I'm obviously missing something obvious.

Geoff
 
Geoff,
"MyClass" is a reserved word, you cannot name a class the same as a reserved
word without "escaping" it, in VB.NET you use square brackets [] to "escape"
keywords.

Something like:

Public Class [MyClass]
End Class

Public x As New [MyClass]

Hope this helps
Jay
 
Geoff Jones said:
I'm trying to use a class that I've written in a form. Unfortunately, when
I write something like:

Public x As New myClass

I get the compile time error: "Type Expected". Indeed, the "squiggly" blue
line does appear under myClass so I half expeced it. However, I had
written

'MyClass' is a keyword of VB.NET and thus cannot be used as a class name
unless you put it into square brackets:
Imports myClass

You don't need to import the class. Instead, import the namespace the class
is part of. I suggest not to name a class 'MyClass' and use a more
meaningful class name instead.
 
Ah, sorry Jay, but I mislead you. Actually I've called the class:

SmoothProgressBar

and it still does it :( i.e. I typed myClass in the hope of simplifying
things. Sorry.

Geoff

Jay B. Harlow said:
Geoff,
"MyClass" is a reserved word, you cannot name a class the same as a
reserved word without "escaping" it, in VB.NET you use square brackets []
to "escape" keywords.

Something like:

Public Class [MyClass]
End Class

Public x As New [MyClass]

Hope this helps
Jay

Geoff Jones said:
Hi

I'm trying to use a class that I've written in a form. Unfortunately,
when I write something like:

Public x As New myClass

I get the compile time error: "Type Expected". Indeed, the "squiggly"
blue line does appear under myClass so I half expeced it. However, I had
written

Imports myClass

in the form so I thought it would work.

Can anybody help? I'm obviously missing something obvious.

Geoff
 
Okay, sorted it! I'd forgotten to add the namespace - D'OH!

Geoff

Jay B. Harlow said:
Geoff,
"MyClass" is a reserved word, you cannot name a class the same as a
reserved word without "escaping" it, in VB.NET you use square brackets []
to "escape" keywords.

Something like:

Public Class [MyClass]
End Class

Public x As New [MyClass]

Hope this helps
Jay

Geoff Jones said:
Hi

I'm trying to use a class that I've written in a form. Unfortunately,
when I write something like:

Public x As New myClass

I get the compile time error: "Type Expected". Indeed, the "squiggly"
blue line does appear under myClass so I half expeced it. However, I had
written

Imports myClass

in the form so I thought it would work.

Can anybody help? I'm obviously missing something obvious.

Geoff
 
If you are importing myClass then myClass is a namespace. A namespace isn't
a type hence the error message. Is myClass actually a class? You defined
Public Class myClass somewhere?

You are basically trying to do this:
Dim x As New System.Windows.Forms
What you wanted to do was:
Dim x As New System.Windows.Forms.Form

Hope it helps some.
Chris
 
Hi Cor

I'd forgotten to add the namespace. Sorted now.

Thanks for your suggestion.

Geoff
 
As per the posts above, I've solved it by writing:

Public x As New SmoothProgressBar.SmoothProgressBar

This now works i.e. the first part is the namespace, the next is the class.

Geoff
 
Geoff,
To avoid some subtle & potentially hard to follow ambiguity errors I avoid
naming a namespace the same as a Class name.

Hope this helps
Jay
 
Chris,
If you are importing myClass then myClass is a namespace.
You can import either a Class or a Namespace!

Importing a class is handy when you want to access the Shared members of a
class without qualifying the identifiers with a class name. Which is similar
to simply defining the class as a Module (or better to get a C# class with
only static members to behave similar to a VB.NET module).

For example:

Imports System.Math

Public Module MainModule

Public Sub Main()
Dim x, y, z As Integer
x = 10
y = 11
z = Min(x, y)
End Sub

End Module

Notice that I used Math.Min without qualifying the Math class name. I
understand that C# 2.0 (VS.NET 2005, aka Whidbey, due out later in 2005)
will have a similar ability...

Hope this helps
Jay
 
Hi Jay

This is still puzzling me slightly - although the code is now working. The
class i.e. SmoothProgressBar, does not have a namespace explicitly in the
code. So, as I have already mentioned, I can only get the code to compile if
I write

Dim x As New SmoothProgressBar.SmoothProgressBar

Is the namespace implicitly set to the name of the class?

I take your point about the namespace and the class name being the same - if
only because it appears overkill and a lot of unnecessary typing.
Interestingly enough, when I wrap the class in my own namespace e.g.

Namespace MyUtilityCollection

' The SmoothProgressBar class

End Namespace

I can call the tool by using

Dim x As New MyUtilityCollection.SmoothProgressBar

Which is less confusing!

Can anybody explain if the namespace is set implictly to the name of the
class and if not why it appears to be happening in my application?

Thanks in advance

Geoff
 
Geoff,
The class i.e. SmoothProgressBar, does not have a namespace explicitly in
the code.
The class will implicitly have a namespace based on the project that you
create it in.

For example if you create a project called SmoothProgressBar, it will have a
root namespace of SmoothProgressBar, if you then create a class called
SmoothProgressBar in that project it will have a fully qualified name of
SmoothProgressBar.SmoothProgressBar.

Is SmoothProgressBar in the same project or a different project then the
form?

What is the name of the project SmoothProgressBar is in?

What is the name of the root namespace SmoothProgressBar is in?

You can use "Project - ... Properties - Common Properties - General - Root
namespace" to view & change the name of the root namespace for a project.

Hope this helps
Jay

Geoff Jones said:
Hi Jay

This is still puzzling me slightly - although the code is now working. The
class i.e. SmoothProgressBar, does not have a namespace explicitly in the
code. So, as I have already mentioned, I can only get the code to compile
if I write

Dim x As New SmoothProgressBar.SmoothProgressBar

Is the namespace implicitly set to the name of the class?

I take your point about the namespace and the class name being the same -
if only because it appears overkill and a lot of unnecessary typing.
Interestingly enough, when I wrap the class in my own namespace e.g.

Namespace MyUtilityCollection

' The SmoothProgressBar class

End Namespace

I can call the tool by using

Dim x As New MyUtilityCollection.SmoothProgressBar

Which is less confusing!

Can anybody explain if the namespace is set implictly to the name of the
class and if not why it appears to be happening in my application?

Thanks in advance

Geoff
<<snip>>
 
Hi Jay

Yes, the project has the name Smooth Progress Bar (note the spaces, but
maybe that doesn't make a difference in terms of namespace i.e. it will
still be SmoothProgressBar). It is in a different project to the main
application form.

This all makes sense now. Many thanks.

HAPPY NEW YEAR!!!!!

Geoff
 
Geoff,
As I stated you need to use "Project - ... Properties - Common Properties -
General - Root namespace" to see what the root namespace on your "Smooth
Progress Bar" project is. I would probably make the "Smooth Progress Bar"
namespace "myapp.controls" or "myapp.ui" where "myapp" is the root namespace
for the application itself. Remember that multiple projects can have the
same namespace...

I would have expected "Smooth_Progress_Bar", which is what I thought VS.NET
2003 does, however VS.NET 2002 & VS.NET 2005 may do something else, I almost
always change the root namespace, so I really don't remember the default...

Of course you can change either the root namespace or the project name after
the fact.

Hope this helps
Jay
 

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

Back
Top