Exposing enums from another class?

M

M O J O

Hi,

I want to expose a enum from another class, is that possible and how?

Here's an example


Public Class ClassMaster

Public Enum Colors
Red
Green
Yellow
End Enum

End Class


Public Class Cars

Public ReadOnly Property Colors() As System.Enum
Get
Return ClassMaster.Colors ' <- Error
End Get
End Property

End Class


Module Main

Public Sub Main()
Private color As Cars.Colors ' <- Error
End Sub

End Module


I know I can make the Enum global by putting it in a module, but I want
to expose it from another class.

I'm creating an application using three layer style, that is
"Presentation layer", "Business layer" and "Data layer".

I do not want the "Presentation layer" to reference the "Data layer".
They must know nothing about each others.

All communication to the "Data layer" must go through the "Business layer".

I have an enum in the "Data layer" which I want the "Presentation layer"
to use, but it must go through the "Business layer".

Hope you get what I mean.

Any idea?

Thanks!

M O J O
 
M

Maniaque |CIA|

On Fri, 30 Jan 2004 14:37:07 +0100, M O J O

----- begin ClassMaster.vb -----
Public Enum Colors
Red
Green
Yellow
End Enum

Public Class ClassMaster
'bla-bla
End Class

Public Class Cars

Public ReadOnly Property Colors() As System.Enum
Get
Return ClassMaster.Colors ' <- Error
End Get
End Property

End Class


Module Main

Public Sub Main()
Private color As Cars.Colors ' <- Error
End Sub

End Module

------------ Now read here :)
You must declare an enum at the global section of the class, before you
begin the "Public Class" construction. AFAIC it works...
 
A

Armin Zingler

M O J O said:
Hi,

I want to expose a enum from another class, is that possible and
how?

Here's an example


Public Class ClassMaster

Public Enum Colors
Red
Green
Yellow
End Enum

End Class


Public Class Cars

Public ReadOnly Property Colors() As System.Enum
Get
Return ClassMaster.Colors ' <- Error

"ClassMaster.Colors" is a type, not a value. You would have to write

Return ClassMaster.Colors.Red

- or, if you want to return the type -

Public ReadOnly Property Colors() As Sytem.Type
Get
Return GetType(ClassMaster.Colors)

End Get
End Property

End Class


Module Main

Public Sub Main()
Private color As Cars.Colors ' <- Error
End Sub

End Module


I know I can make the Enum global by putting it in a module, but I want
to expose it from another class.

I'm creating an application using three layer style, that is
"Presentation layer", "Business layer" and "Data layer".

I do not want the "Presentation layer" to reference the "Data layer".
They must know nothing about each others.

All communication to the "Data layer" must go through the "Business layer".

I have an enum in the "Data layer" which I want the "Presentation layer"
to use, but it must go through the "Business layer".

Hope you get what I mean.


I think I do, but where is the problem in exposing the "data layer" types?
If you don't want to do it, you'd have to write wrapper classes within the
business layer for all items in the data layer that you want to use in the
presentation layer.
How is this problem related to the Enum problem?


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
H

Herfried K. Wagner [MVP]

* M O J O said:
I want to expose a enum from another class, is that possible and how? [...]
I know I can make the Enum global by putting it in a module, but I
want to expose it from another class.

I'm creating an application using three layer style, that is
"Presentation layer", "Business layer" and "Data layer".

You may want to put the enum into a separate namespace, but I don't see
the sense of doing that.
 
G

Gary Chang

Hi M O J O,

Thanks for posting in the community.

Currently I am looking for somebody who could help you on it. We will reply
here with more information as soon as possible.
If you have any more concerns on it, please feel free to post here.


Thanks!

Best regards,

Gary Chang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
 
P

Peter Huang

Hi Mojo,

Thanks for posting in the community.

First of all, I would like to confirm my understanding of your issue.
From your description, I understand that you wants to use an Enum type
defined in a class and encounter error.
Have I fully understood you? If there is anything I misunderstood, please
feel free to let me know.

I agree with Armin's suggestion.
Return ClassMaster.Colors ' <- Error
ClassMaster.Colors is not a type, you may need to return an variable .
Private color As Cars.Colors ' <- Error
Cars.Colors is a property not a type. Also you should define a local
variable with private.

Here I change your code as follows.

Public Class ClassMaster
Public Enum Colors
Red
Green
Yellow
End Enum
End Class

Public Class Cars
Public ReadOnly Property Colors() As System.Enum
Get
Return ClassMaster.Colors.Red
End Get
End Property
End Class

Module Main
Public Sub Main()
Dim color As ClassMaster.Colors = ClassMaster.Colors.Green
Dim oCar As New Cars
Console.WriteLine(oCar.Colors)
End Sub
End Module

If you have any concern on this issue,please post here.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
Date: Fri, 30 Jan 2004 14:37:07 +0100
From: M O J O <mojo@_no_spam_delete_this_newwebsolutions.dk>
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.4)
Gecko/20030624 Netscape/7.1 (ax)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top