StrataFrame Forum

An Enum question

http://forum.strataframe.net/Topic5232.aspx

By StarkMike - 12/12/2006

Here is the error i'm getting:

'ParentFormID' cannot expose type 'Module1.ParentFormIDEnum' outside the project through class 'MyClass'.

Here is the related code:

Module Module1

    Enum ParentFormIDEnum As Integer
        POReceiving = 1
        Production = 4
        Returns = 5
    End Enum

End Module

Public Class MyClass

    Public WriteOnly Property ParentFormID() As ParentFormIDEnum
        Set(ByVal value As ParentFormIDEnum)
         miParentFormID = value
        End Set
    End Property

End Class

I'm not sure what this error means. All I'm trying to do is create a global enum and be able to use it in all areas of my program.

By Trent L. Taylor - 12/12/2006

Since your enum and module have not been defined as public then you cannot have a public property expose those privately typed classes.  You need to add Public in front of Module and your enum.

I do not recommend using modules!  This was left for backward compatability.  You should use shared classes.  In the case of your enum, just move it outside of the module and it will reside as its own public enum:

Public Enum MyEnum As Integer
   MyValue = 1
End Enum
By Paul Chase - 12/12/2006

Mike,

Your class is public but the module isn't so it is letting you know that your class is public which exposes the module which is friend. Just make the module public and it should work. Or make the class friend

Public module whatever

Paul

By StarkMike - 12/12/2006

Thanks Trent. Thanks Paul.

Trent, How is a shared class different from a regular class?

By Paul Chase - 12/12/2006

Lol didnt see Trent already answered you. I agree about modules.

1)  If you reference your project from another project, whether VB or C#, the module routines aren't accessible even if they're public.

2)  You can name a local method in a class the same as a public module routine, and the compiler won't say anything about it.  When you use the method, the compiler won't require you to prefix it; it'll just happily use the local version instead of the module with no warning.

Shared classes, on the other hand, are accessible to outside projects (if you want them to be), and shared classes avoid the ambiguity because of the prefixed class name.

By StarkMike - 12/12/2006

Ok, I moved the enums outside of the Module and now it looks like this:

Public Module Module1

End Module

Public Enum ParentFormIDEnum As Integer
    POReceiving = 1
    Production = 4
    Returns = 5
End Enum

And here's where my VB6 brain Hehe is having trouble... I dont understand how I can place the enum 'outside' of the Module. When I created the module, it created a Module1.vb file... and inside it was

Module Module1

End Module

I assumed that everything had to reside inside there, and the only thing allowed outside was the Imports statements at the top.

By StrataFrame Team - 12/12/2006

Basically, a "sealed" class is what you use in VB to be a container for methods.  (C# has a "static" class, but "sealed" is as close as you can get in VB).  You create the class, and it becomes the module (actually, when you compile a module in VB.NET, it creates a class out of the module on the back end.. which you can see if you use reflection).

Public NotInheritable Class MyModuleClass

     '-- A private constructor keeps someone from accidentally trying to create an instance of this class
     Private Sub New() 
     End Sub

     '-- All of your methods and fields are then marked with "Shared"
     Public Shared Function MyFunc(ByVal value As String) As String
          '-- Parse the value
     End Function

End Class

'-- When you access these Shared methods and fields, you access them like this:
MyModuleClass.MyFunc("test") '-- you use the ClassName. syntax and you get all of the shared methods on the class

By StarkMike - 12/12/2006

Awesome! Thanks for the info.
By StarkMike - 12/13/2006

And here's where my VB6 brain  is having trouble... I dont understand how I can place the enum 'outside' of the Module.

I've seen examples like this:

Public Class Item

End Class

Public Class Inventory

    Dim itm as new Item

End Class

I dont understand how you can have two classes in the same file. I am under the impression that you can only have one class per file.  What gives? Tongue

P.S. I really appreciate this thread and the advice on using a shared class instead of a module... that is invaluable information that I might not otherwise pickup. Cool

By StrataFrame Team - 12/13/2006

You can have as many classes in a file as you want... you can even have nested classes.  Basically, with VB.NET, you can have a 1 file to 1 class relationship, or you can have a multiple file to 1 class relationship (partial classes... part of the class exists in each file), or you can have a 1 file to multiple class relationship.  The code file does not define a class... the Public Class <ClassName> and End Class do.  Open up one of the .designer.vb files for one of your business objects and drop down the left combo box at the top of the file... you'll see how many classes exist in that one file.  And besides the business object, they're all nested.
By Ivan George Borges - 1/4/2007

Ben Chase (12/12/2006)
Basically, a "sealed" class is what you use in VB to be a container for methods.

Hiya.

Having a sealed class being a container for methods, I might need to write many "Imports" on the top of it for all the methods that will be in the class.

Would this be a bad practice?

By Trent L. Taylor - 1/4/2007

Not necessarily.  But keep in mind that you can create multiple sealed classes as well.  It is best to keep things that are related within a single class. 
By StrataFrame Team - 1/4/2007

No, Ivan, you can have as many Imports as you want... they don't affect the runtime whatsoever.  They're more compiler directives than anything.
By Ivan George Borges - 1/4/2007

Thanks guys, got the idea.
By Trent L. Taylor - 1/4/2007

Anything for you, Ivan BigGrin