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

« Financial Functions for .NET releasedVB.Net and C# - the difference in OO syntax part 2 »
comments
Rate Post:
submit to reddit Digg!FacebookDotnetkicks

Part 2

Classes and their access modifiers

Protected or protected

Let’s start with an easy one.

In VB_Class_Libraby_2

  1. Protected Class Class1
  2.  
  3. End Class

Which gives us a very interesting error message.

Protected types can only be declared inside of a class.

Which seems more than logical.

Now let’s see what C# has to say about this.

In C_Class_Librabry_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.     protected class Class1
  9.     {
  10.     }
  11. }

And it seems to come to the same conclusion.

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

In VB_Class_Library_2

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

And this.

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.         protected class Class1_1
  11.         {
  12.         }
  13.     }
  14. }

Should work just fine.

We can now also state that the protected class will only be available to its parent or to subclasses of the parent.

So if I do this:

In VB_Class_Library_2

  1. Public Class Class1
  2.     Protected Class Class1_1
  3.  
  4.     End Class
  5. End Class
  6.  
  7. Public Class Class2
  8.     Inherits Class1
  9.  
  10.     Public Sub New()
  11.         Dim _Class1 As New Class1_1
  12.     End Sub
  13. End Class

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.         protected class Class1_1
  11.         {
  12.         }
  13.     }
  14.  
  15.     public class Class2 : Class1
  16.     {
  17.         public Class2()
  18.         {
  19.             Class1_1 class1_1;
  20.         }
  21.     }
  22. }


But I am digressing. I was going to talk about the differences.

Friend or internal

Here is the biggest difference. They decided to use a different keyword for the same thing, neither of them seem to catch the real essence of what the keyword is trying to say. I can say the same for protected BTW, but I think that would have been hard. A Friend or an Internal class can only be used in the same assembly and not by a referencing assembly.

In VB_Class_Library_2

  1. Friend Class Class1
  2.  
  3. End Class
  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.     internal class Class1
  9.     {
  10.     }
  11. }

needless to say, we will get error messages if we try to call them from another project/assembly.

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 class1 = new C_Class_Library_2.Class1();
  13.         }
  14.     }
  15. }

You actually get three error messages on this one line of code (Well done).

2 times this one.

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

And one of these.

The type ‘C_Class_Library_2.Class1′ has no constructors defined

In VB_Console

  1. Module Module1
  2.  
  3.     Sub Main()
  4.         VB_Class_Library_2.Class1 = New VB_Class_Library_2.Class1()
  5.     End Sub
  6.  
  7. End Module

Here I only get 2 error messages.

2 times

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

Protected Friend or protected internal

This is even more restrictive because it makes it protected and Friend/internal at the same time. I won’t go deeper into this because we will get more of the same.

Public or public

This is the simplest one of all. It makes it so you can use your class everywhere and it is the same for everyone.

No Access modifiers

This is the most curious one. Do both languages default to the same access modifier when you leave it out?

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.     class Class1
  9.     {
  10.     }
  11. }

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 class1 = new C_Class_Library_2.Class1();
  13.         }
  14.     }
  15. }

And now run this and see what error messages you get.

Yes, you get the exact same ones as for Friend/Internal, so we now know that csharp defaults to internal when you specify no access modifier.

Now let’s see what VB.Net gives us.

In VB_Class_Library_2.

  1. Class Class1
  2.  
  3. End Class

and

In VB_Console

  1. Module Module1
  2.  
  3.     Sub Main()
  4.         VB_Class_Library_2.Class1 = New VB_Class_Library_2.Class1()
  5.     End Sub
  6.  
  7. End Module

And the error message tells us immediately that it also considers it as friend.

Next up Namespaces and variable declaration.

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
1455 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.)