Click here to Skip to main content
11,414,670 members (66,500 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
Can we create a Static Class in VB.NET?   If so, please illustrate.   Can we derive a new class from Static class?
Posted 2-Jan-12 3:38am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

VB.NET Modules are the equivalent of C# Static Classes and no, you cannot derive from a Module (as well as you cannot derive from a C# Static Class).
You may find all the above info better explained inside MSDN documentation.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

A VB.NET Module is the equivalent of a static class. You cannot inherit from a static class nor can it be inherited from another class.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

You can create static class in vb.net

Friend NotInheritable Class DB
        Public Shared AGE As Integer = 10
    End Class

Using AGE variable in other code

Dim myage As Integer = DB.AGE
  Permalink  
Comments
Dave Kreskowiak at 11-May-13 10:14am
   
That's not a "static" class as defined by C#. That's an instance class with a static field in it.

The equivilent of a C# static class is a Module in VB.NET.
ArioSolution at 11-May-13 10:21am
   
What about this?
In C# you can put static classes inside static class but in Module you can not put module inside module.
If you define like I wrote you can create classes inside class
Dave Kreskowiak at 11-May-13 10:38am
   
The problem is that you're not making static classes. You're making classes that cannot be inherited from. There two are not the same.
ArioSolution at 11-May-13 10:47am
   
If you want to use inheritance you can declare class only with Friend keyword, If have tried and it works
Dave Kreskowiak at 11-May-13 10:56am
   
Uhhh Friend has nothing to do with being able to inherit a class or not. It has everything to do with what code can see it.

Again, you're not making static classes, so all of what you're saying is a moot point.
ArioSolution at 11-May-13 11:19am
   
You maybe right Dave,
But tell me what about module inside module?

It's sample, go to http://www.developerfusion.com/tools/convert/csharp-to-vb and write something in c#.
Convert it and you can see that the static class is converted to NotInheritable class.
Dave Kreskowiak at 11-May-13 11:34am
   
Sigh... The converter is making an approximation. NOT a static class.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 9,160
1 OriginalGriff 7,487
2 Maciej Los 3,710
3 Abhinav S 3,298
4 Peter Leow 3,084


Advertise | Privacy | Mobile
Web02 | 2.8.150427.4 | Last Updated 11 May 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100