sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
=?Utf-8?B?Qi4gQ2hlcm5pY2s=?='s Avatar

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


Question posted by: =?Utf-8?B?Qi4gQ2hlcm5pY2s=?= (Guest) on August 28th, 2008 07:25 PM
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.)
1 Answer Posted
Tom Shelton's Avatar
Guest - n/a Posts
#2: Re: C# to VB Translation problem (Is this a singleton?)

On 2008-08-28, B Chernick <BChernick@discussions.microsoft.comwrote:
Quote:
Originally Posted by
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
 
Not the answer you were looking for? Post your question . . .
197,014 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 197,014 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top Community Contributors