473,395 Members | 1,941 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,395 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 31848
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.