Classes and Namespaces

T

tshad

I have a .dll that has MyFunctions as a Namespace and Class of FWData.

If I have my using statement as:

using MyFunctions.FWData

Why do I have to set a new variable as:

Dim fw = New MyFunctions.FWData(10)

Why can't I do:

Dim fw = new FWData(10)

This gives me an error:

C:\VSProjects\TestFWData\Form1.vb(73): Type 'FWData' is not defined.

Why is that? If I have it defined in my using statement - why do I need the
namespace?

The whole code is:

*******************************************************
Imports MyFunctions.FWData

Public Class Form1
Inherits System.Windows.Forms.Form

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim fw = New MyFunctions.FWData(10)
Dim j As Integer = fw.ErrorCode
End Sub
End Class
******************************************************

Thanks,

Tom
 
S

Scott M.

Not sure why you are referring to "using" when it is "imports" that we are
discussing, but anyway, don't import the class, just the namespace. Then
your code would be:

*******************************************************
Imports MyFunctions

Public Class Form1
Inherits System.Windows.Forms.Form

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim fw = New FWData(10)
Dim j As Integer = fw.ErrorCode
End Sub
End Class
******************************************************

When you import a namespace, you no longer have to fully qualify statements
that include elements of that imported namespace. If you import a class,
you can access certain elements of the class without fully qualifying them.

Your idea doesn't work because if you import this way:

Imports MyFunctions.FWData

You can now get at certain FWData class members without having to qualify
"MyFunctions.FWData".

But, when you write:

Dim fw = new FWData(10)

FWData is not found because the namespace that contains FWData has not been
imported, you are importing PAST that level.
 
R

rowe_newsgroups

I have a .dll that has MyFunctions as a Namespace and Class of FWData.

If I have my using statement as:

using MyFunctions.FWData

Why do I have to set a new variable as:

Dim fw = New MyFunctions.FWData(10)

Why can't I do:

Dim fw = new FWData(10)

This gives me an error:

C:\VSProjects\TestFWData\Form1.vb(73): Type 'FWData' is not defined.

Why is that? If I have it defined in my using statement - why do I need the
namespace?

The whole code is:

*******************************************************
Imports MyFunctions.FWData

Public Class Form1
Inherits System.Windows.Forms.Form

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim fw = New MyFunctions.FWData(10)
Dim j As Integer = fw.ErrorCode
End Sub
End Class
******************************************************

Thanks,

Tom
If I have my using statement as:

using MyFunctions.FWData

I think you mean Imports as you're in the VB newsgroup and not the C#
one....
Why is that? If I have it defined in my using statement - why do I need the
namespace?

You Imported the FWData class, not the MyFunctions namespace.
Therefore it will allow "easy" access to any member of FWData, but to
get it recognize the members of MyFunctions you need to import it
also.

i.e. Change
Imports MyFunctions.FWData

to

Imports MyFunctions
Imports MyFunctions.FWData

Thanks,

Seth Rowe
 

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

Top