468,490 Members | 2,576 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,490 developers. It's quick & easy.

Static Classes in VB

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
Nov 20 '05 #1
8 31524
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.
"JackRazz" <Ja******@NotValid.com> wrote in message
news:ea**************@TK2MSFTNGP12.phx.gbl...
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

Nov 20 '05 #2
In article <ea**************@TK2MSFTNGP12.phx.gbl>, JackRazz wrote:
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.

--
Tom Shelton [MVP]
OS Name: Microsoft Windows XP Professional
OS Version: 5.1.2600 Service Pack 1 Build 2600
System Up Time: 1 Days, 18 Hours, 9 Minutes, 42 Seconds
Nov 20 '05 #3
But (technically) you can't create a shared class. Just a class with all
shared members.
"Tom Shelton" <to*@mtogden.com> wrote in message
news:uJ**************@TK2MSFTNGP09.phx.gbl...
In article <ea**************@TK2MSFTNGP12.phx.gbl>, JackRazz wrote:
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.

--
Tom Shelton [MVP]
OS Name: Microsoft Windows XP Professional
OS Version: 5.1.2600 Service Pack 1 Build 2600
System Up Time: 1 Days, 18 Hours, 9 Minutes, 42 Seconds

Nov 20 '05 #4
In article <OZ**************@TK2MSFTNGP10.phx.gbl>, Scott M. wrote:
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
Nov 20 '05 #5
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
"Tom Shelton" <to*@mtogden.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
| In article <OZ**************@TK2MSFTNGP10.phx.gbl>, Scott M. wrote:
| > 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
Nov 20 '05 #6
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.
"JackRazz" <Ja******@NotValid.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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
"Tom Shelton" <to*@mtogden.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
| In article <OZ**************@TK2MSFTNGP10.phx.gbl>, Scott M. wrote:
| > 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

Nov 20 '05 #7
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
"Scott M." <s-***@BADSPAMsnet.net> wrote in message
news:ei**************@TK2MSFTNGP09.phx.gbl...
| 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.
|
|
| "JackRazz" <Ja******@NotValid.com> wrote in message
| news:%2****************@TK2MSFTNGP10.phx.gbl...
| > 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
| >
| >
| > "Tom Shelton" <to*@mtogden.com> wrote in message
| > news:%2****************@TK2MSFTNGP09.phx.gbl...
| > | In article <OZ**************@TK2MSFTNGP10.phx.gbl>, Scott M. wrote:
| > | > 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
| >
| >
|
|
Nov 20 '05 #8
> 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
Nov 20 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Joanna Carter \(TeamB\) | last post: by
9 posts views Thread by Chuck Cobb | last post: by
4 posts views Thread by Brian Richards | last post: by
4 posts views Thread by thomson | last post: by
1 post views Thread by BLUE | last post: by
4 posts views Thread by Rene | last post: by
9 posts views Thread by puzzlecracker | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.