CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Jun 2008
    Posts
    7

    VB.NET Equivalent for C#

    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:
    Code:
    public static class A
    {
        // Class definition
    }
    and
    Code:
    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.

  2. #2
    Join Date
    Jun 2006
    Posts
    645

    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.

  3. #3
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    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.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  4. #4
    Join Date
    Jun 2006
    Posts
    645

    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.

  5. #5
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    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.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  6. #6
    Join Date
    Jun 2008
    Posts
    7

    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

  7. #7
    Join Date
    Jun 2007
    Location
    .NET 3.5 Beta SP1, Visual Basic 2008 Express
    Posts
    225

    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?
    Last edited by ccubed; July 13th, 2008 at 12:31 AM.
    Microsoft Visual Basic 2008 Express Edition
    .NET Framwork 3.5 Beta SP1

  8. #8
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    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).
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  9. #9
    Join Date
    Jun 2007
    Location
    .NET 3.5 Beta SP1, Visual Basic 2008 Express
    Posts
    225

    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?
    Microsoft Visual Basic 2008 Express Edition
    .NET Framwork 3.5 Beta SP1

  10. #10
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    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.

    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.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  11. #11
    Join Date
    Jun 2007
    Location
    .NET 3.5 Beta SP1, Visual Basic 2008 Express
    Posts
    225

    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.
    Microsoft Visual Basic 2008 Express Edition
    .NET Framwork 3.5 Beta SP1

  12. #12
    Join Date
    Jun 2006
    Posts
    645

    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.

  13. #13
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    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....
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  14. #14
    Join Date
    Aug 2005
    Posts
    198

    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.
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured