Click to See Complete Forum and Search --> : VB.NET Equivalent for C#


jack_99
July 12th, 2008, 01:03 PM
Hi all. I am primarily used to program in C#. However, I have a certain requirement which needs some VB.NET coding. I want to know the equivalent code for the following in VB.NET:

public static class A
{
// Class definition
}

and

public sealed class A
{
// Class definition
}


Another, question that comes to my mind is what is the meaning of WithEvents keyword in VB.NET?

Thanks.

bhushan1980
July 12th, 2008, 02:49 PM
Not sure about this...But the following is the equivalent:


' Equivalent code for a C# static class
Public NotInheritable Class A
Private Sub New()
'Initializations
End Sub
' Class Implementation
End Class

and

'Equivalent code for a C# sealed class
Public NotInheritable Class A
'Class Implementation
End Class

I just obtained these from the C# to VB.NET convertor available online. Here you can clearly see the difference between the two codes. Where as a C# static class should not be initialized, a C# sealed class must be. Hence, the constructor in the static class has been especially made Private so as to prevent instantiation. Also, it seems that the default access modifier in VB.NET is Public, it seems. (Not sure about this, Gurus might comment). In C++, I am sure that it is private for a class and public for a struct. I do not know for structs in VB.NET. In C#, it is private or internal by default for a class and public for a struct. So others might comment on that as well.

Regards,
Bhushan.

TheCPUWizard
July 12th, 2008, 04:13 PM
In C#, it is private or internal by default for a class and public for a struct. So others might comment on that as well.


In C++ the ONLY difference between a class and a struct is the default visibility. In .NET class and struct are COMPLETELY DIFFERENT CONCEPTS. The former is a reference type, while the latter is a value type.

Failure to understand this and know which to use for specific cases is the #2 reason why so many .NET programs (written by C++ programmers) are so poor.

bhushan1980
July 12th, 2008, 09:23 PM
I have known that fact that classes are reference types and the structs are the value types in .NET environment. Also, it was you along with some other Gurus who cleared my doubts, on C# forums on CG. However, I was trying to explain OP about the difference between the two syntaxes. I just happened to put the C# code in the C# to VB.NET convertor and got the code that I have posted in my previous reply to OP. Also, I want to know if the explanation given by me to OP holds any relevance. I am talking about the fact that C# has static and sealed for two different purposes and VB.NET has NotInheritable for expressing both of them; one with a private constructor to prevent its instantiation. Then I went on to explain OP how the default access modifiers are different in VB.NET. I do not know anything about the default modifiers for the VB.NET "Structure". However, it seems that for the classes, it is Public by default. And in C#, initially I thought that the access modifier that is default is private just as in C++. However, I stumbled upon an article that mentioned that it is internal? So, I myself do not know what is the default access modifier for the C# class members?

Regards,
Bhushan.

TheCPUWizard
July 12th, 2008, 10:14 PM
Bhushan,

I am aware you know the difference between class and struct, I was not sure about the OP knowing this.

YES, be uses the "noninheritable" attribute to express both "static" and "sealed" classes, with one being non-constructable.

The default access for classes in C# is internal. C++ does no have a concepts of access specifiers at the class level.

Both C++ and C# default to private for all members of classes.

jack_99
July 12th, 2008, 11:30 PM
Hi Bhushan and TheCPUWizard. Thankyou for both of your inputs. This post was very much useful to me in terms of knowing things that are not easily known by most of the developers around you.

Thanks again.
Jack

ccubed
July 13th, 2008, 12:20 AM
Bhushan,

I am aware you know the difference between class and struct, I was not sure about the OP knowing this.

YES, be uses the "noninheritable" attribute to express both "static" and "sealed" classes, with one being non-constructable.

The default access for classes in C# is internal. C++ does no have a concepts of access specifiers at the class level.

Both C++ and C# default to private for all members of classes.

Then public and private are what?

TheCPUWizard
July 13th, 2008, 09:13 AM
Then public and private are what?

I am not sure what you mean....

In C++, public, private (and protected) are used to control access to MEMBERS.

C++ does NOT have the concept of assemblies and multiple assemblies, so the "internal" keyword does not exist at all (I am talking ISO C++ not C++/CLI).

ccubed
July 13th, 2008, 02:25 PM
I am not sure what you mean....

In C++, public, private (and protected) are used to control access to MEMBERS.

C++ does NOT have the concept of assemblies and multiple assemblies, so the "internal" keyword does not exist at all (I am talking ISO C++ not C++/CLI).


Well, you had said that c++ doesn't have a concept of access modifiers at a class level, but then Public and Private are technically access modifiers at a class level aren't they? Or are you talking about A class being defined as either public or private in and of itself?

TheCPUWizard
July 13th, 2008, 04:56 PM
Or are you talking about A class being defined as either public or private in and of itself?

That is exactly what I am talking about.

Well, you had said that c++ doesn't have a concept of access modifiers at a class level, but then Public and Private are technically access modifiers at a class level aren't they?

NO they impact the visibility of the MEMBERS that follow the keywords, and have NO impact on the visibility of the CLASS.

ccubed
July 13th, 2008, 05:05 PM
That is exactly what I am talking about.


NO they impact the visibility of the MEMBERS that follow the keywords, and
have NO impact on the visibility of the CLASS.

Okay, that makes sense.

bhushan1980
July 13th, 2008, 09:01 PM
As I know for C#, there are only two modifiers at the class level i.e., public and internal. That is what TheCPUWizard was saying, I suppose. Does that mean that, VB.NET has Public and Friend class modifiers at the class level? Or it is just Public? And I suppose that default access modifier for members is public in case of VB.NET?

Regards,
Bhushan.

TheCPUWizard
July 13th, 2008, 09:06 PM
As I know for C#, there are only two modifiers at the class level i.e., public and internal. That is what TheCPUWizard was saying, I suppose.

WRONG!
I NEVER said only two. Protected and Private are also available at the class level, but can only be applied to nested classes (ie classes that are not at the namespace level....

David Anton
July 14th, 2008, 09:44 PM
There is no good equivalent in VB for a C# static class. Declaring a class as static in C# means that you can't declare an instance member in the class. The closest way to do this in VB is to use a 'Module', which may be undesirable since it's not a true class.

Regarding 'WithEvents', this just allows you to add a 'Handles' clause to wire up events of the object with an event handling method. Without 'WithEvents' you can still use 'AddHandler' to dynamically wire up event handlers.