Thursday, May 16, 2013

Public Enum inside Module in VB.NET

Public Enum inside Module in VB.NET

'choice' cannot expose type 'Module1.Enum_choice' outside the project through class 'Class1'

You have this error but you don’t know why. You have an Enumeration inside a Module and you want to use it inside a class. Why this isn’t working?

I have a sample project at the end of this post if you want to experiment the case.
First of all, if you have this problem, it is probably that you haven’t told the program that your module is public. You could easily correct the problem by adding the keyword PUBLIC at the beginning of your Module.

Assuming the module and the class are in the same project.

In yellow, the error you might have:

Public Class Class1
    Private _Choice As Enum_Choice
    Public Sub New()
        _Choice = Enum_choice.none
    End Sub

    Public Sub New(choice As Enum_choice)
        _Choice = choice
    End Sub
End Class

Here is the error I copy from Microsoft Visual Studio 2010:

Error      1             'choice' cannot expose type 'Module1.Enum_choice' outside the project through class 'Class1'.                C:\Users\checkkay\Documents\Visual Studio 2010\Projects\GlobalEnumSample\GlobalEnumSample\Class1.vb                7             30           GlobalEnumSample

The solution for you problem is in Yellow. You have to tell your program that the Enumeration is accessible.

Public Module Module1
    Public Enum Enum_choice
        none = 0
        first = 1
        twice = 2
    End Enum
End Module

Download sample Project:

I work with Microsoft Visual Studio 2010 for most of my projects. You could get or download it from this link: Download Visual Studio or Express Edition

You could also buy the same version I am using from Amazon here: Visual Studio 2010 Professional (Old Version)
Microsoft Visual Studio 2012 Professional is now cheaper than the older version. If you want to buy it, I advise you to buy the newer version. If you want to compare both versions, I made a little post on the subject here: VS2010 vs VS2012

Others interesting post on Modules: