Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Desktop Developer

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

Search

XML Feeds

Google Ads

« VB.Net and C# - the difference in OO syntax part 3VB.Net and C# - the difference in OO syntax part 1 »
comments
Rate Post:
submit to reddit Digg!FacebookDotnetkicks

Part 1

Classes and their access modifiers

So let’s see what the differences are between the access modifiers of VB.Net and C#. I found this list.

For VB.Net that would be:

  • Private
  • Protected
  • Friend
  • Protected friend
  • Public

In C# that will be:

  • private
  • protected
  • internal
  • protected internal
  • public

So Friend becomes internal in C# and C# uses a different capitalization. That is a good thing to remember.

Now let’s see if they do the same thing.

Private or private

Let’s try something.

In C_Class_Library_2.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace C_Class_Library_2
  7. {
  8.     private class Class1
  9.     {
  10.     }
  11. }

This gives me the error.

Elements defined in a namespace cannot be explicitly declared as private, protected, or protected internal

So I can’t do this. In C#, I had to build it before getting this error.

In VB_Class_Library_2.

  1. Private Class Class1
  2.  
  3. End Class

Slightly different error.

Types declared ‘Private’ must be inside another type.

The VB.Net error seems to be more to the point. And I didn’t need to compile.

So lets use Private class for what it is intended.

In C_Class_Library_2.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace C_Class_Library_2
  7. {
  8.     public class Class1
  9.     {
  10.         private class Class1_1
  11.         {
  12.         }
  13.     }
  14. }

In VB_Class_Library_2

  1. Public Class Class1
  2.     Private Class Class1_1
  3.  
  4.     End Class
  5. End Class

These private classes can only be used inside the class where they are declared.

So doing this in the consoleapp will not work.

In C_Console.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace C_Console
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             C_Class_Library_2.Class1_1 class1 = new C_Class_Library_2.Class1_1();
  13.             C_Class_Library_2.Class1.Class1_1 class2 = new C_Class_Library_2.Class1.Class1_1();
  14.         }
  15.     }
  16. }

The first line gives this error.

The type or namespace name ‘Class1_1′ does not exist in the namespace ‘C_Class_Library_2′ (are you missing an assembly reference?)

And the second line will give.

‘C_Class_Library_2.Class1.Class1_1′ is inaccessible due to its protection level

Now let’s do the same in VB.Net.

In VB_Console.

  1. Module Module1
  2.  
  3.     Sub Main()
  4.         Dim class1 As New VB_Class_Library_2.Class1_1
  5.         Dim class2 As New VB_Class_Library_2.Class1.Class1_1
  6.     End Sub
  7.  
  8. End Module

The first line gives this.

Type ‘VB_Class_Library_2.Class1_1′ is not defined.

The second line gives.

‘VB_Class_Library_2.Class1.Class1_1′ is not accessible in this context because it is ‘Private’.

Now lets see if we can return something of Class1_1.

In C_Class_Library_2

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace C_Class_Library_2
  7. {
  8.     public class Class1
  9.     {
  10.         public Class1_1 getClass1_1()
  11.         {
  12.         }
  13.  
  14.         private class Class1_1
  15.         {
  16.         }
  17.     }
  18. }

After compilation we get this error message.

Inconsistent accessibility: return type ‘C_Class_Library_2.Class1.Class1_1′ is less accessible than method ‘C_Class_Library_2.Class1.getClass1_1()’

Which is very logical.

In VB_Class_Library_2.

  1. Public Class Class1
  2.     Public Function GetClass1_1() As Class1_1
  3.  
  4.     End Function
  5.  
  6.     Private Class Class1_1
  7.  
  8.     End Class
  9. End Class

We get this error message.

‘GetClass1_1′ cannot expose type ‘Class1_1′ in namespace ‘VB_Class_Library_2′ through class ‘Class1′.

Seems fine by me.

Apart from a difference in wording, they seem to be both in agreement. I wish the two teams could sit together and get the error messages the same, preferably taking the good from both sides.

This post is long enough. I think I covered all the basics of private class. You might think this is too basic, but I learned something from it. I could have gone on and on with nesting, but I guess you get the point. Next up is protected or Protected.

About the Author

User bio imageChristiaan is a forensic technician who programs on the side, although my function description says that I do IT-things for 90% of the time . I'm an avid VB.NET fan and I use lots of the ALT.Net techniques, like unit-testing, nhibernate, logging, IoC, ...
Social SitingsTwitterLinkedInHomePageLTD RSS Feed
1366 views
submit to reddit Digg!FacebookDotnetkicks

Comments and Feedback

No feedback yet

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)