Static Classes in VB

  • Thread starter Thread starter JackRazz
  • Start date Start date
J

JackRazz

Is it possible to create a static class in vb.net like c# does? I want the code to
create a single global instance of a class that is inherited from another class. I
could use a module, but I can't inherit from a class with it. How would 'public
static class classname' be different from just creating a class with all the
properties being static?

Thanks - JackRazz
 
In VB.NET we use the keyword "shared" to accomplish what "static" does in
C#.

In VB.NET classes can't be shared, but class members can so you don't really
compare a shared class against a class with shared members, you just make
the members shared.
 
Is it possible to create a static class in vb.net like c# does? I want the code to
create a single global instance of a class that is inherited from another class. I
could use a module, but I can't inherit from a class with it. How would 'public
static class classname' be different from just creating a class with all the
properties being static?

Thanks - JackRazz

Yes, it's possible to create a class in VB.NET with all shared members -
but shared members are not inherited. They don't belong to any instance
of a class - but to the class itself. And that's true in C# as well.
 
But (technically) you can't create a shared class. Just a class with all
shared members.
 
But (technically) you can't create a shared class. Just a class with all
shared members.

Technically True, but you can't do that in C# either. It's basically
the same situation. You basically create a sealed class and a private
default constructor and make all your properties and methods static.
That's exactly what a vb.net module ends up as :)

Either way, the results are about the same.
 
Thanks guys for the help. I think I can now make vb.net do what I need! I want to
make sure I understand what your saying. Below is the 'Reflector' decompiled output
for a simple vb.net module

Private NotInheritable Class Globals Inherits Object Private Shared Sub New()
Public Shared gString As String
End Class

1) If I make all properties/methods Shared, I can achieve the same results as a
vb.net module. So that's how!
2) vb.net modules cannot inherit from another class because MS hard-wired the
module to inherit from object only.
3) vb.net module classes (all properties/methods) are somehow declared globally as
I don't need to create an instance
of the Globals module in my code to access the gString property.

Do you know how vb.net modules are made global to the application or assembly? Also,
why didn't they allow modules to be inherited from a class?

Thanks for the solution - JackRazz


| > But (technically) you can't create a shared class. Just a class with all
| > shared members.
|
| Technically True, but you can't do that in C# either. It's basically
| the same situation. You basically create a sealed class and a private
| default constructor and make all your properties and methods static.
| That's exactly what a vb.net module ends up as :)
|
| Either way, the results are about the same.
| --
| Tom Shelton [MVP]
| OS Name: Microsoft Windows XP Professional
| OS Version: 5.1.2600 Service Pack 1 Build 2600
| System Up Time: 1 Days, 20 Hours, 39 Minutes, 42 Seconds
 
Hi Jack,

I think you just may need to shift away from the "module" track and head
more towards the object track. The question is not really, how to make a
module globally available, it's how to make the object globally available.
Sure, there are still modules of code, but within that code you can create
Public classes and/or subs & functions so that they can be accessed from
other entities in your app.
 
Hey Scott,

I completely agree after the suggestions from both Tom and you. I can now do that
with the shared methods/properties. I just want to know how vb's module's object is
declared globally without seeing the declaration in the source code. I'm trying to
get some insight on how the innards work.

This is a very interesting question to me. I know, a sure sign of a nerd<grin>.

Thanks Again - Jack


| Hi Jack,
|
| I think you just may need to shift away from the "module" track and head
| more towards the object track. The question is not really, how to make a
| module globally available, it's how to make the object globally available.
| Sure, there are still modules of code, but within that code you can create
| Public classes and/or subs & functions so that they can be accessed from
| other entities in your app.
|
|
| | > Thanks guys for the help. I think I can now make vb.net do what I need!
| I want to
| > make sure I understand what your saying. Below is the 'Reflector'
| decompiled output
| > for a simple vb.net module
| >
| > Private NotInheritable Class Globals Inherits Object Private Shared Sub
| New()
| > Public Shared gString As String
| > End Class
| >
| > 1) If I make all properties/methods Shared, I can achieve the same
| results as a
| > vb.net module. So that's how!
| > 2) vb.net modules cannot inherit from another class because MS
| hard-wired the
| > module to inherit from object only.
| > 3) vb.net module classes (all properties/methods) are somehow declared
| globally as
| > I don't need to create an instance
| > of the Globals module in my code to access the gString property.
| >
| > Do you know how vb.net modules are made global to the application or
| assembly? Also,
| > why didn't they allow modules to be inherited from a class?
| >
| > Thanks for the solution - JackRazz
| >
| >
| > | > | > But (technically) you can't create a shared class. Just a class with
| all
| > | > shared members.
| > |
| > | Technically True, but you can't do that in C# either. It's basically
| > | the same situation. You basically create a sealed class and a private
| > | default constructor and make all your properties and methods static.
| > | That's exactly what a vb.net module ends up as :)
| > |
| > | Either way, the results are about the same.
| > | --
| > | Tom Shelton [MVP]
| > | OS Name: Microsoft Windows XP Professional
| > | OS Version: 5.1.2600 Service Pack 1 Build 2600
| > | System Up Time: 1 Days, 20 Hours, 39 Minutes, 42 Seconds
| >
| >
|
|
 
I just want to know how vb's module's object is
declared globally without seeing the declaration in the source code. I'm trying to
get some insight on how the innards work.

Well, I guess that's the thing. The module isn't an object (at least not to
the CLR anyway). It's the stuff in the module that are the objects. Make
the things in the module global (using correct declaration keywords [public,
friend, etc.]) and they will be available accordingly. The only catch is
that the code in the module needs to be loaded into memory or run before the
items will take on their scope.

Good luck!

-Scott
 
Back
Top