473,246 Members | 1,802 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,246 software developers and data experts.

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 31836
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Joanna Carter \(TeamB\) | last post by:
Following on from the other discussion, I have to just check something out with reference to disposal of resources held in static fields. I have a Persistence Framework that is 'globally...
9
by: Chuck Cobb | last post by:
I am creating some static classes with static methods and static variables, but I have a question: What is the lifetime of static classes and static variables? Is there any risk that these...
4
by: Brian Richards | last post by:
Is there a way to implement static classes in C++ the same way you do in C#? static class CompanyInfo { public static string GetCompanyName() { return "CompanyName"; } public static string...
4
by: thomson | last post by:
Hi All, Can any one tell me the demerits of using static classes in Web Application,what is the scope of this kind of class Thanks in Advance Thomson
4
by: JC | last post by:
Suppose an ASP.Net project contains a public static class with public methods and members that are used throughout the application. Of course being static, there is only copy of the class within...
1
by: BLUE | last post by:
I know it's a stupid question but... I want to use a class that represents the current user of my application, another one that reads position from a GPS receiver and another one that reads RFID...
1
TRScheel
by: TRScheel | last post by:
Platform: Windows Vista Language: ASPX / C# I am trying to make a business library for where I work, and I have everything figured out and done except for the ObjectDataSource portion. If any...
6
by: Smithers | last post by:
In consideration of a "Utilities" class that contains methods intended to be used amongst multiple projects... Is there any general consensus on whether such a class should be static? I have...
4
by: Rene | last post by:
Hi, I was wondering if anyone could tell me why extension methods must be declared on static classes. I mean, why not allowing them to be declared as static methods in regular instance classes?...
9
by: puzzlecracker | last post by:
It looks like static classes can have non-static member classes -- does it make sense what I just wrote? If not, let me illustrate it: public static class Foo{ private struct Bar{ } }
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.