4

A class that contains utility methods is needed over and over all over the place. So, I'd like to make it a static class.

Why static is being converted to NotInheritable?

public static class MyClass 
{
    public static string MyProperty { get; set; } 

    public static void MyMethod()
    {

    }
}

to

Public NotInheritable Class Employee
   Private Sub New()
   End Sub
   Public Shared Property MyProperty() As String
      Get
          Return m_MyProperty
      End Get
      Set
          m_MyProperty = Value
      End Set
  End Property
  Private Shared m_MyProperty As String

  Public Shared Sub MyMethod()
  End Sub
 End Class

In my opinion, this looks more like a sealed class, doesn't it?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Richard77
  • 20,343
  • 46
  • 150
  • 252
  • 4
    "NotInheritable" is the equivalent of "sealed" in c#. https://msdn.microsoft.com/en-us/library/h278d2d4.aspx. It seems that "Shared" is the equivalent of "static"? http://stackoverflow.com/questions/1980207/static-shared-in-vb-net-and-c-sharp-visibility –  Feb 22 '16 at 17:09
  • The static means sealed which means you can't inherit them.... Also cannot contain an instance constructor, but a static one ... – Trevor Feb 22 '16 at 17:09
  • @Codexer, well it gets a bit complex. In C# you can inherit from a static class, however it's not "proper" inheritance, it's "faked". http://stackoverflow.com/questions/2281775/c-sharp-static-member-inheritance-why-does-this-exist-at-all –  Feb 22 '16 at 17:12
  • What converter did you use? You're right, it made it a sealed class rather than static. – Tim Feb 22 '16 at 17:12
  • @Codexer, static != sealed. See Squimmy's correct answer below. – Joshua Feb 22 '16 at 17:12
  • 2
    @Joshua are you saying Microsoft is wrong? https://msdn.microsoft.com/en-us/library/79b3xss3.aspx – Trevor Feb 22 '16 at 17:13
  • @MatthewWhited thank you for confirming this. – Trevor Feb 22 '16 at 17:15
  • 1
    Static classes are sealed but sealed classes are not all static. – Matthew Whited Feb 22 '16 at 17:16
  • @Codexer, no. I won't go so far as to say that you are mistaken, but instead I'll limit myself to say that your comment of "the static means sealed" is incorrect. Those two access modifiers are not the same. A static class is sealed, but a sealed class is not static. The only comparable modifier to NotInheritable is Sealed. A Static class in VB is a Module. – Joshua Feb 22 '16 at 17:45

3 Answers3

11

Why static is being converted to NotInheritable?

It's not getting directly converted. The fact that static classes are sealed is what makes the equivalent class in VB.NET NonInheritable

In my opinion, this looks more like a sealed class, doesn't it?

Yes, but it's not "wrong" because static classes are also sealed.

VB.NET does not have the concept of a "static class". The closest it has is module, which is not the exact equivalent of static since it cannot, for example, nest another module within it, while you can define a static class within another static class in C#. So the converter may just be erring on the side of caution by not translating a static class to a Module.

Since static classes in C# are sealed classes that can't be instantiated, the closest thing to an equivalent class in VB.NET is a NotInheritable class with all Shared methods and properties, with a private default instance constructor so the class cannot be instantiated.

So the resulting VB class has all of the characteristics of the source class, but the mechanisms to provide those features are somewhat different.

Blackwood
  • 4,504
  • 16
  • 32
  • 41
D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • You're right that the closest equivalent in VB is 'Module', but why would you say that it can't be represented in a pure code conversion? – Dave Doknjas Feb 22 '16 at 17:32
  • @DaveDoknjas: That make sense. Just to be in the safe side, what about the controversy between `Shared` and `Module`? Shouldn't I for instance, write either of one to indicate my real intention? I want a static class, not a sealed class per se, even if static imply sealed? – Richard77 Feb 22 '16 at 17:34
  • @Richard77: I don't 'get' the controversy - 'Module' is as close as you can get to a C# static class. – Dave Doknjas Feb 22 '16 at 17:37
  • @DaveDoknjas: I've read a couple of place where it was said that Module is a old way of doing things. Can you write the above code with Module keyword instead of inheritance? – Richard77 Feb 22 '16 at 17:42
  • @DaveDoknjas I'll have to look more but I believe there are some differences between `Module` in VB and `static class` in C# that make them not exactly equivalent. One that comes to mind is that you cannot "nest" modules in VB but you can have a class (static or not) defined within a static class in C#. – D Stanley Feb 22 '16 at 17:43
  • @Richard77 I haven't seen anything official to that extent, so I'd say that's just the opinion of those that don't fully respect VB. – D Stanley Feb 22 '16 at 17:48
  • @DStanley: You are correct - VB modules cannot be nested, but I think this is mostly academic since I can't remember noticing anyone using nested static classes in C#. – Dave Doknjas Feb 22 '16 at 17:51
7

I'm not sure what you've used to convert static to NotInheritable, but they are not equivalent.

NotInheritable in VB.NET is equivalent to sealed in C#.
C#'s static is called Shared in VB.NET.

Of note, is that Shared cannot be applied to classes, only members of a class. This is outlined in the VB.NET specification

Class Declarations:

ClassDeclaration ::=
   [ Attributes ] [ ClassModifier+ ] Class Identifier LineTerminator
   [ ClassBase ]
   [ TypeImplementsClause+ ]
   [ ClassMemberDeclaration+ ]
   End Class LineTerminator
ClassModifier ::= AccessModifier | Shadows | MustInherit | NotInheritable

(note the conspicuous lack of Shared under ClassModifier ::=)

As mentioned by other users, a Module is a better equivalent to a C# static class.

Squimmy
  • 567
  • 3
  • 5
  • What's is confusing is that, in some articles online, it's said that `shared` is enforced by the compiler - whatever that means. Some other articles say that the equivalent of static should be `Module`. Yet, others criticize `Module` as it supposed to be a VBScript relic. I'm lost. – Richard77 Feb 22 '16 at 17:17
  • That telerik conversion tool looks wrong. Try to convert C# -> VB, take the result and convert back to c# – alex.b Feb 22 '16 at 17:18
  • @alex.b What _should_ the output be if the converter is wrong? The resulting C# class has the same characteristics (sealed, no instance methods, no public constructor) without using the `static` keyword. – D Stanley Feb 22 '16 at 17:30
  • 1
    @DStanley: The output 'should' be 'Module' - this is identical to a C# static class (with some added VB magic which allows calling members without qualification - but that's just VB magic, not the nature of 'Module' itself). – Dave Doknjas Feb 22 '16 at 17:34
1

The OP asked (in the comments) for a version using 'Module' - here it is:

Public Module [MyClass]
    Public Property MyProperty() As String

    Public Sub MyMethod()
    End Sub
End Module

Note that the members of the module are called either with qualification:

Dim s As String = [MyClass].MyProperty

Or without qualification (this is what I referred to as 'VB magic' in my comment) - I'm not recommending this - just showing for completeness:

Dim s As String = MyProperty

And yes, this is an older feature, but it is still the feature in VB that is the closest to a C# static class - there is no 'Shared Class' in VB (yet).

Dave Doknjas
  • 6,394
  • 1
  • 15
  • 28