SPONSORS:






User Tag List

Results 1 to 4 of 4
  1. #1
    Junior Member
    Join Date
    Aug 2002
    Location
    Santa Cruz, CA
    Posts
    5
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Enumerated datatypes with the same values

    I'm having some trouble, apparently, understanding enumerated datatypes. I'm defining two enums, but the two enums have some values in common. E.g.:

    Notice that both enums contain the value "red". This seems like it should be fine to me, but when I compile, SilkTest argues that "Enum value red is already defined..."

    I thought I was using enumerated datatypes properly here, but I must be missing something... Can anyone explain what's going on here?

    Thanks,
    Rick

  2. #2
    Junior Member
    Join Date
    Aug 2002
    Location
    Santa Cruz, CA
    Posts
    5
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Enumerated datatypes with the same values

    Thanks for your response, John. I think I understand a bit better now.

    All I'm really trying to do here is limit the possible values someone can pass in for a particular argument in my function. So in the function declaration, I have an argument of type PrimaryColors, for example, and I want code using that function to fail to compile if something other than red, green, or blue is passed in.

    It sounds like I should use a set instead of an enum, at least, so that integers cannot be passed in.

    But even with a set, I still can't seem to have sets with common values in them... i.e., in my above example, if I make PrimaryColors and AlternateColors both sets, SilkTest still won't like it because they both contain "red". I don't quite understand why it's a problem, though. If I declare a function that takes an argument of type PrimaryColors, then Silk should just check to see if I passed in red, green, or blue. Likewise, if I declare a function of type AlternateColors, then Silk should just check to see if I passed in red, yellow, or purple. Why does it matter that red exists in both sets? You mentioned, I think, that SilkTest has no context to know which "red" I'm talking about, but aren't I providing that context by specifying the datatype of the argument in my function declaration?

    Thanks,
    Rick

    ------------------

  3. #3
    Senior Member
    Join Date
    Jul 2000
    Posts
    117
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Enumerated datatypes with the same values

    <BLOCKQUOTE><font size="1" face="Verdana, Arial, Helvetica">quote:</font><HR>... still won't like it because they both contain "red". I don't quite understand why it's a problem, though. If I declare a function that takes an argument of type PrimaryColors, then Silk should just check to see if I passed in red, green, or blue. Likewise ...<HR></BLOCKQUOTE>

    From Silk's perspective (from the compiler point-of-view) you're only specifying the datatype is "ENUM" -- which translates to INTEGER. If it were only possible to say "AlternateColor" or "PrimaryColor" and have them actually BE a datatype ...


    ------------------

  4. #4
    Senior Member
    Join Date
    Feb 2000
    Posts
    1,497
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Enumerated datatypes with the same values

    <BLOCKQUOTE><font size="1" face="Verdana, Arial, Helvetica">quote:</font><HR>Originally posted by rwaters:
    I'm having some trouble, apparently, understanding enumerated datatypes. I'm defining two enums, but the two enums have some values in common. E.g.:

    Notice that both enums contain the value "red". This seems like it should be fine to me, but when I compile, SilkTest argues that "Enum value red is already defined..."

    I thought I was using enumerated datatypes properly here, but I must be missing something... Can anyone explain what's going on here?

    Thanks,
    Rick
    <HR></BLOCKQUOTE>


    Silk is complaining because it lacks sufficient context to discern any difference between "red" and "red" - it has no way of knowing if you are referring to PrimaryColors or AlternateColors. Certainly in this case is seems reasonable that it shouldn't matter since both occupy the same zero index. However there's nothing to prevent you or a sort operation from rearranging the list leave "red" to occupy two unique locations. Again Silk has no way to determine which "red" you mean.

    Enums can be converted to integers if you need that ability (few do). One detraction of enums is that they lose their recognizable name when they appear as parameters to testcases in the results files; they are translated to their integer value instead. While you might like to see them appear as:

    mytest(red, green, blue)

    in the results they'll actually appear as:

    mytest(1 ,2 , 3)

    Note that these two calls are equivalent as is this one:

    mytest(1 ,green , 3)

    If your prefer the certainly of the first example above - that "red" always and only meaning "red" - then consider using the "set data type" instead.


    John


    ------------------

 

 

Posting Permissions

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

vBulletin Optimisation provided by vB Optimise v2.6.0 Beta 4 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
User Alert System provided by Advanced User Tagging v3.0.9 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Questions / Answers Form provided by vBAnswers (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
vBNominatevBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Feedback Buttons provided by Advanced Post Thanks / Like (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Username Changing provided by Username Change (Free) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
BetaSoft Inc.
Digital Point modules: Sphinx-based search
All times are GMT -8. The time now is 07:38 PM.

Copyright BetaSoft Inc.