By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,854 Members | 869 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,854 IT Pros & Developers. It's quick & easy.

C# to VB Translation problem (Is this a singleton?)

P: n/a
I'm getting a little confused here. I have a C# class that I'm trying to
translate to VB.

The C# class is essentially:
public static class Class1
{
..... some private static variables and public static properties.
}

The translation (from several different translation programs) is:
Public NonIneritable Class Class1
private sub New()
End sub
.... some private shared variables and public shared properties
end Class

Does this look right? Just want a second opinion.

(I'm pretty sure the VB version is a singleton but not so sure about the C#
version.)
Aug 28 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On 2008-08-28, B Chernick <BC*******@discussions.microsoft.comwrote:
I'm getting a little confused here. I have a C# class that I'm trying to
translate to VB.

The C# class is essentially:
public static class Class1
{
.... some private static variables and public static properties.
}

The translation (from several different translation programs) is:
Public NonIneritable Class Class1
private sub New()
End sub
... some private shared variables and public shared properties
end Class

Does this look right? Just want a second opinion.

(I'm pretty sure the VB version is a singleton but not so sure about the C#
version.)
The translation is accurate. In C#, a static class is essentially equivalent
to a VB.NET module - in that it can only have static (vb.net shared) memebers.
Before C#v2, you would declare such a class as:

public sealed class Class1
{
private Class1(){};

// now only add static members
}

Which is essentially what you showed in the VB.NET code. This was a common
enough patter that in C#v2 they started allowing static to be applied to a
clas declaration - which gave compiler enforcement to the above pattern.
Since, you can add not static members to the above C# class.

I wouldn't really call that a Singleton... Though, I suppose it could be
thought of that way :) Usually, you'll see an implementation similar to this
for a singleton:

public class Singleton
{
private static Singleton instance = new Singleton();

private Singleton() {}
public Singlton Instance
{
get
{
return instance;
}
}

// public members
}
Which in VB.NET would look like:

Public Class Singleton
Private Shared _instance As New Singleton()

Private Sub New()
End Sub

Public ReadOnly Property Instance As Singleton
Get
Return _instance
End Get
End Property

' other public members...
End Class

HTH

--
Tom Shelton
Aug 28 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.