Results 1 to 7 of 7

Thread: Using Enum in an interface

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2001
    Posts
    21

    Question Using Enum in an interface

    Hi ppl,

    I want to use enumerations in an ActiveX DLL. This ActiveX DLL uses interfaces. I want to use those enumerations in properties, so the user is restricted from certain values. For example, now I have a property to which I assign an integer, but I have a check inside the property which checks if the input is 0,1,2 or 3. If not, the property raises an error. This is very difficult especially when another developer wants to use my DLL, because he has to read about 2000 lines in which all the valid inputs are given (and trust me, there are a LOT !)

    Could anybody please help me with this?!?

  2. #2
    jim mcnamara
    Guest
    Code:
    Public Enum IColors
       icMistyRose = &HE1E4FF&
       icSlateGray = &H908070&
       icDodgerBlue = &HFF901E&
       icDeepSkyBlue = &HFFBF00&
       icSpringGreen = &H7FFF00&
       icForestGreen = &H228B22&
       icGoldenrod = &H20A5DA&
       icFirebrick = &H2222B2&
    End Enum
    
    Property Let Color(Ic as IColors)
    
    End Property
    This uses an enum as a property, limits the entries to the items enumerated { icMistRose ... icFireBrick}, and will show up in the type libaray (& object browser).

    That what you need?

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2001
    Posts
    21

    Exclamation Not really working

    I've already tried some implementations of an enum in an interface, but it doesn't seem to work in ActiveX DLL's. On the other hand, it does work work with ActiveX EXE's! But I don't want to use A' EXE's!

    Greetz,

    Bartozz

  4. #4
    jim mcnamara
    Guest
    Please show me how you define the interface, then show me an implementation.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Oct 2001
    Posts
    21

    Post Some implementation

    Here is some implementation (it's an ActiveX DLL):

    iClass
    -------
    Option Explicit

    Property Get Interface() As String
    End Property

    ' Filename.
    Property Let FileName (nv As String)
    End Property

    Property Get FileName () As String
    End Property

    ' Name.
    Property Let Option (nv As Integer)
    End Property

    Property Get Option () As Integer
    End Property
    .
    .
    .

    cClass
    --------
    Option Explicit

    Implements iClass

    Private msFileName$
    Private miOption%

    Property Get iClass_Interface() As String
    iClass_Interface = "iClass"
    End Property

    Private Property Let iClass_FileName(nv As String)
    msFileName = nv
    End Property

    Private Property Get iClass_FileName() As String
    iClassFileName = msFileName
    End Property

    Property Let iClass_Option (nv As Integer)
    Select Case nv
    Case 0, 1, 2, 3
    miOption = nv
    Case Else
    Err.Raise c__INVALID_VALUE, iClass_Interface, c__INVALID_VALUE_TEXT
    End Select
    End Property

    Property Get iClass_Option () As Integer
    iClass_Option = miOption
    End Property
    .
    .
    .

    Now it's about the Option-thing... The Option property has only 4 options, as shown: 0, 1, 2 and 3. Now I do it with the Select Case thingie, but it has to be arranged with Enumerations! Not only the possible values will be shown explicitly to the developer using my component, so he/she doesn't have to read stacks of paper to figure out which values can be used!

    Do you get my idea?

  6. #6
    jim mcnamara
    Guest
    This DOES work in an ActiveX dll...

    In the interface definition you have to use the enumeration

    ' Name.
    Property Let Option (nv As enumMyclass)
    End Property

    Property Get Option () As enumMyClass
    End Property
    .
    .
    In the implementation - (another .cls module)

    Public enum enumMyClass
    enumMyClassRed = 1
    enumMyCLassGreen = 2
    End enum

    Private mMyClass as enumMyClass


    Property Let iClass_Option (nv As enumMyCLass)
    ' you don't need a case statement because you'll always
    ' get the right range of values
    mMyCLass = nv
    End Property

    Property Get iClass_Option () As enumMyClass
    iClass_Option = mMyCLass
    End Property

  7. #7
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    Orlando
    Posts
    392
    Remember, however, though enumeration suggests a list of possible values, it doesn't restrict the user/developer from assigning values which are not in the Enum List. Consider the following:

    VB Code:
    1. Enum Bang
    2.     BangOne
    3.     BangTwo
    4. End Enum

    A property of type Bang will cause VB to suggest BangOne and BangTwo as the possible values which correspond to 0 and 1, however, it is possible to assign values other than these two to the property and VB will not perform any Validation checking, nor it will restrict from any other numeric value.

    Enums perfectly work with all type of projects.
    Abu Haider
    ____________________________
    100% Data Validation for the MS DataGrid Control. Plus Support for Custom and Foreign Lists, DatePicker and much more...
    The DataGridEnhancer


    I often point to a place where the problem has been discussed, instead of giving you the code that solves it. This is for good, may be you will understand some day...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width