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.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace C_Class_Library_2
{
    private class Class1
    {
    }
}```
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.

```vbnet
Private Class Class1

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.

```csharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace C_Class_Library_2
{
    public class Class1
    {
        private class Class1_1
        {
        }
    }
}

In VB_Class_Library_2

Public Class Class1
    Private Class Class1_1

    End Class
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.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace C_Console
{
    class Program
    {
        static void Main(string[] args)
        {
            C_Class_Library_2.Class1_1 class1 = new C_Class_Library_2.Class1_1();
            C_Class_Library_2.Class1.Class1_1 class2 = new C_Class_Library_2.Class1.Class1_1();
        }
    }
}

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.

Module Module1

    Sub Main()
        Dim class1 As New VB_Class_Library_2.Class1_1
        Dim class2 As New VB_Class_Library_2.Class1.Class1_1
    End Sub

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace C_Class_Library_2
{
    public class Class1
    {
        public Class1_1 getClass1_1()
        {
        }

        private class Class1_1
        {
        }
    }
}

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.

Public Class Class1
    Public Function GetClass1_1() As Class1_1

    End Function

    Private Class Class1_1

    End Class
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.