Re: VB.NET Equivalent for C#
Not sure about this...But the following is the equivalent:
Code:
' Equivalent code for a C# static class
Public NotInheritable Class A
Private Sub New()
'Initializations
End Sub
' Class Implementation
End Class
and
Code:
'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.
Re: VB.NET Equivalent for C#
Quote:
Originally Posted by bhushan1980
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.
Re: VB.NET Equivalent for C#
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.
Re: VB.NET Equivalent for C#
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.
Re: VB.NET Equivalent for C#
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
Re: VB.NET Equivalent for C#
Quote:
Originally Posted by TheCPUWizard
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?
Re: VB.NET Equivalent for C#
Quote:
Originally Posted by ccubed
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).
Re: VB.NET Equivalent for C#
Quote:
Originally Posted by TheCPUWizard
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?
Re: VB.NET Equivalent for C#
Quote:
Originally Posted by ccubed
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.
Quote:
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.
Re: VB.NET Equivalent for C#
Quote:
Originally Posted by TheCPUWizard
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.
Re: VB.NET Equivalent for C#
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.
Re: VB.NET Equivalent for C#
Quote:
Originally Posted by bhushan1980
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....
Re: VB.NET Equivalent for C#
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.