Namespace variable

D

dotNETnews

Little confused...

When I declare a namespace variable in module I can use it anywhere in the
project. That's clear.

I read a book in which the author said that namespace variables can be
declared in a class outside of any subs or functions... below " Windows Form
Designer generated code " with the Public keyword and I could use this
variable in the whole project's namespace.

I discovered that it can be used but I have to instantiate that class which
before I can use that variable in let say some other form in that same
project. I thought I could use it without the need to instantiate that
class.
 
H

Herfried K. Wagner [MVP]

dotNETnews said:
When I declare a namespace variable in module I can use it anywhere in the
project. That's clear.

I read a book in which the author said that namespace variables can be
declared in a class outside of any subs or functions... below " Windows
Form Designer generated code " with the Public keyword and I could use
this variable in the whole project's namespace.

What's a "namespace variable"? I have never heard this term. Can you post
a code sample that demonstrates what you want to archieve?
 
G

Greg Burns

Use the shared keyword in a class.

Public Class Class1
Public Shared MyVar As Integer
End Class

You can use it like Class1.MyVar without instaniating it.

A Module is just a VB specific construct. It is basically a shared class.
One unique thing about a Module is that you can reference the public
variables/methods without prefixing with the Module name.

I don't believe "namespace variable" is correct terminology. At least, I've
never heard of it before.

Greg
 
D

dotNETnews

Greg Burns said:
Use the shared keyword in a class.

Public Class Class1
Public Shared MyVar As Integer
End Class

You can use it like Class1.MyVar without instaniating it.

A Module is just a VB specific construct. It is basically a shared class.
One unique thing about a Module is that you can reference the public
variables/methods without prefixing with the Module name.

I don't believe "namespace variable" is correct terminology. At least,
I've never heard of it before.

Greg

It from Murach's book, Beginning Visual Basic.NET. They call variables with
namespace scope - namespace variables.
 

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