Login or Sign Up to become a member!

EXPERTS, INFORMATION, IDEAS & KNOWLEDGE

Social bookmarker Add this

Your profile

Search

July 2009
Mon Tue Wed Thu Fri Sat Sun
 << <   > >>
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31    

XML Feeds

Authors

« VB.Net and C# - the difference in OO syntax part 3VB.Net and C# - the difference in OO syntax part 1 »
The Desktop Developers Journal

VB.Net and C# - the difference in OO syntax part 2

by chrissie1


Permalink 02 Dec 2008 04:01 , Categories: Microsoft Technologies Tags: access modifiers, beginners, c#, vb.net

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.

Leave a comment »Send a trackback » 894 views

Trackback address for this post

Trackback URL (right click and copy shortcut/link location)

No feedback yet

Leave a comment


Your email address will not be revealed on this site.

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