Results 1 to 14 of 14

Thread: A little confused about Enumerations

Hybrid View

  1. #1

    Thread Starter
    Hyperactive Member The Fire Snake's Avatar
    Join Date
    Sep 2009
    Location
    USA
    Posts
    401

    Re: A little confused about Enumerations

    Quote Originally Posted by NickThissen View Post
    It looks like you wrote that code outside of the designer because there's a "silly" mistake in the param1 declaration that would not have compiled. I assume you mean "ByVal" instead of "dim" there, correct?
    Yes, you are right. I wrote this code outside the editor paraphrasing what I had. Just a typo.

    Quote Originally Posted by NickThissen View Post
    Anyway, the 'error' is that you require param1 to be of type Integer. You should require it to be of type Greeting if you want to pass a Greeting.

    An enumeration is just a type like any other. However, enumerations have an 'underlying type' (not sure how to call that) which is an Integer. The first value in your enumeration for example has value 1, and the second has value 2 (you could have used 28 and 982 if you wanted). For this reason, enumerations can be converted to integers (and the other way around too). I think that's the reason that you code compiled in the first place. VB is implicitly converting the Greetings.Hi value to an integer (which is 1 by your enum declaration). If you turn Option Strict On (google it or search this forum) then I think it will not let you compile.

    So the only thing you need to change is the TestEnum method signature, to this:
    Code:
    Private Sub TestEnum(ByVal param1 as Greeting)
    Ok, you hit the nail on the head. I think this is the heart of my confusion. The implicit conversion is exactly what I wanted to avoid, I knew that was what was going on since when I change the type from Greeting to Integer I got it to work with the same results. So here is where I am confused. I understand the concept of classes and there members. Doesn't the enum kind of work the same way? Aren't the Hi and Bye like members that have a value assigned to them? I had read and knew about the "underlying" type you mentioned and that was what made me think that the Hi and Bye were Integer values, hence my use of Integer for the type of param1 in the TestEnum method I wrote. This is bizarre to me. I guess I need some sleep or something or I am just retarded

    So Greeting is an enum and so is Greeting.Hi and Greeting.bye??

  2. #2
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: A little confused about Enumerations

    Quote Originally Posted by The Fire Snake View Post
    So Greeting is an enum and so is Greeting.Hi and Greeting.bye??
    No. Greeting is an Enum, and Hi and Bye are its members. You can think of an Enum as a class (or possibly structure) with constant members, for example:
    Code:
    Public Class FakeEnum
       Public Shared Const Hi As Integer = 1
       Public Shared Const Bye As Integer = 2
    End Class
    With that class you can get the same functionality. However, I wouldn't dare to say that they are the same (they are most probably not), but it might help to think about it that way.

    Did you read the MSDN documentation? It explains it pretty well, albeit a little technical:
    http://msdn.microsoft.com/en-us/libr...8VS.80%29.aspx

  3. #3

    Thread Starter
    Hyperactive Member The Fire Snake's Avatar
    Join Date
    Sep 2009
    Location
    USA
    Posts
    401

    Re: A little confused about Enumerations

    Quote Originally Posted by NickThissen View Post
    No. Greeting is an Enum, and Hi and Bye are its members. You can think of an Enum as a class (or possibly structure) with constant members, for example:
    Code:
    Public Class FakeEnum
       Public Shared Const Hi As Integer = 1
       Public Shared Const Bye As Integer = 2
    End Class
    With that class you can get the same functionality. However, I wouldn't dare to say that they are the same (they are most probably not), but it might help to think about it that way.
    Ok. This is exactly how I was thinking about Enums. If this is the case then why is Greeting.Hi seen as an Enum? From your code snippet above it is an Integer. How can it be both?

    For example lets say that the Greeting enum was implemented as you mention above(I know that this might not be a 100% true) and I wanted to access the Hi member. I would do something like FakeEnum.Hi, where it's type would be Integer, not something else. Then if I wanted to pass it to a method I would do the following(see call and method signature it calls):

    Code:
    ' Call to method
    TestEnum(FakeEnum.Hi)
    
    ' Signature of the method called
    Public Sub TestEnum(ByVal p1 as Integer)
    .....
    End Sub
    I would not do the following(or it makes no sense to me to do it this way):
    Code:
    ' Call to method
    TestEnum(FakeEnum.Hi)
    
    ' Signature of the method called
    Public Sub TestEnum(ByVal p1 as FakeEnum)
    .....
    End Sub
    I read MSDN but I am missing something...
    Last edited by The Fire Snake; Oct 28th, 2009 at 05:14 PM.

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