none
ENUM Declaration

    Question

  • Hi MSDN,

    i have one Code Like "Pencilin-1324" , i Want to Declare it in Enum with the same name without Replacing the "-" With "_" .

    So Please suggest can i declare like that. If Within Brackets or Some Extra Charactres then also no Problem . Please suggest the possible declaration methods.


    Thanks And Regards
    Satya Ranjan Biswal
    Wednesday, May 20, 2009 5:59 AM

Answers

  • As far as I know, there is no way to use the "-" character in an Enum member name. 

    I suggest you go ahead and use "_" instead of "-" in the Enum member name, and just replace it with "-" when you need to display it to the user.  The following snippet has a function to convert from a string that may include "-" to the corresponding Enum member (with "_" instead of "-"), and one to convert an Enum member to a string that has "-" instead of "_".  It's a bit more work, but I think this is what you will need to do.

    Enum Medicine
        Invalid
        Pencilin_1324
    End Enum
    
    Private Function GetMedicineValue(ByVal medName As String) As Medicine
        Dim medValue As Medicine
        Try
            medValue = CType([Enum].Parse(GetType(Medicine), medName.Replace("-", "_")), Medicine)
        Catch ex As ArgumentException
            medValue = Medicine.Invalid
        End Try
    
        Return medValue
    End Function
    
    Private Function GetMedicineName(ByVal medValue As Medicine) As String
        Return medValue.ToString.Replace("_", "-")
    End Function
    



    Wednesday, May 20, 2009 12:27 PM
  • This version of GetMedicineValue is better as it avoids throwing and catching an exception when the input string is invalid.

    Private Function GetMedicineValue(ByVal medName As String) As Medicine
        Dim myName As String = medName.Replace("-", "_")
        If [Enum].IsDefined(GetType(Medicine), myName) Then
            Return CType([Enum].Parse(GetType(Medicine), myName), Medicine)
        Else
            Return Medicine.Invalid
        End If
    End Function



    Wednesday, May 20, 2009 1:03 PM

All replies

  • I don't think there is any way to include a "-" in the name of the Enum member.  If you explain why you need to use the "-", perhaps we can suggest a workaround.
    Wednesday, May 20, 2009 11:21 AM
  • There is no way to do that, you cant use that char (-) for naming variable. Take a look at General Naming Conventions
    http://msdn.microsoft.com/en-us/library/ms229045.aspx
    http://www.akadia.com/services/naming_conventions.html

    kaymaf

     


    I hope this helps, if that is what you want, just mark it as answer so that we can move on
    Wednesday, May 20, 2009 11:25 AM
  • Hi ,

    actually i have a big project in which medicine Names Are reffered Directly , So Instead Of Taking Medicine Names We Are Making Those As ENUM , So That No text Comparision Will Be There . But Some Medicine Names As i mentioned earlier can not added into ENUM Becoz of  hypen problem , Is There any way to add enum variables like [abc-123] . 










    Thanks and Regards
    Satya Ranjan Biswal
    Wednesday, May 20, 2009 11:29 AM
  • As far as I know, there is no way to use the "-" character in an Enum member name. 

    I suggest you go ahead and use "_" instead of "-" in the Enum member name, and just replace it with "-" when you need to display it to the user.  The following snippet has a function to convert from a string that may include "-" to the corresponding Enum member (with "_" instead of "-"), and one to convert an Enum member to a string that has "-" instead of "_".  It's a bit more work, but I think this is what you will need to do.

    Enum Medicine
        Invalid
        Pencilin_1324
    End Enum
    
    Private Function GetMedicineValue(ByVal medName As String) As Medicine
        Dim medValue As Medicine
        Try
            medValue = CType([Enum].Parse(GetType(Medicine), medName.Replace("-", "_")), Medicine)
        Catch ex As ArgumentException
            medValue = Medicine.Invalid
        End Try
    
        Return medValue
    End Function
    
    Private Function GetMedicineName(ByVal medValue As Medicine) As String
        Return medValue.ToString.Replace("_", "-")
    End Function
    



    Wednesday, May 20, 2009 12:27 PM
  • This version of GetMedicineValue is better as it avoids throwing and catching an exception when the input string is invalid.

    Private Function GetMedicineValue(ByVal medName As String) As Medicine
        Dim myName As String = medName.Replace("-", "_")
        If [Enum].IsDefined(GetType(Medicine), myName) Then
            Return CType([Enum].Parse(GetType(Medicine), myName), Medicine)
        Else
            Return Medicine.Invalid
        End If
    End Function



    Wednesday, May 20, 2009 1:03 PM
  • Hi , BlackWood ,Thanks For U r Reply ,

    I Already Mentioned in My Question , That without Replacing THe "-" With "_" . I Know The Process But It is not fulfill my requirement.. so If Any Other Sugessions u hav then please send me .









    Thanks again For You Reply .


    Regards

    Satya Ranjan Biswal
    Wednesday, May 20, 2009 1:06 PM
  • Hi Satya.  As I said (and so did kaymaf), you just can't use the "-" character in the name of an Enum member, it's not allowed.  I think your only alternative is a workaround like the one I posted.  Can you explain why that workaround will not work for you?  If you do, perhaps someone can post another alternative. 
    Wednesday, May 20, 2009 1:12 PM
  • Hi , Blackwood ,   Using "-" is impossible in Fact Not According to the  Naming Conventions OF Microsoft For Its General Programming   Conventions. I am Asking For Any Alternative Using "-" .. Just Like [abc-123] or Something Like that ..


    if it is possible then please reply me





    Thanks For your Reply Mr. Blackwood.




    Thanks And Regards


    Satya Ranjan Biswal
    Wednesday, May 20, 2009 1:22 PM
  • well, may be you should use array of string to hold your list of drugs because the option you insist to use is not supported in variable naming conventions.
    Dim MedicineNames() As String={"Pencilin-1324"}

    kaymaf
    I hope this helps, if that is what you want, just mark it as answer so that we can move on
    Wednesday, May 20, 2009 1:25 PM
  • At least in Visual Basic, the "-" character is not allowed in names.  The following is taken from the Visual Basic Programming Guide section called Declared Element Names

    Rules

    An element name in Visual Basic must observe the following rules:

    • It must begin with an alphabetic character or an underscore (_).

    • It must only contain alphabetic characters, decimal digits, and underscores.

    • It must contain at least one alphabetic character or decimal digit if it begins with an underscore.

    • It must not be more than 1023 characters long.

    Maybe there is another .net language that allows the "-" character in names.  But even if there is, I think changing to another language for this reason may ne more trouble than it is worth.



    Wednesday, May 20, 2009 1:35 PM
  • Hi , Blackwood ,



    Thanks  A Lot For Your Reply , I Have To Search For An alternative to Create Or Place Names Like That .







    thanks And Regards
    Satya Ranjan Biswal
    Wednesday, May 20, 2009 1:45 PM
  • Hi , Blackwood ,

    Thanks  A Lot For Your Reply , I Have To Search For An alternative to Create Or Place Names Like That .

    thanks And Regards
    Satya Ranjan Biswal

    Hi Satya,

    You will have a long search as there isn't an alternative that I know about, apart from the earlier suggestions, even if you do use square brackets like.>>

       Public Enum MedicineNames
    
            Aspirin_1
    
            [Enum]
    
        End Enum
    


    Regards,

    John
    Thursday, May 21, 2009 12:22 PM
  • Hi ,
    THanks John For u r reply ..














    Thanks And regards
     
    Satya Ranjan Biswal
    Thursday, May 21, 2009 12:45 PM
  • if you dont want to follow up this thread, then close it
    kaymaf
    I hope this helps, if that is what you want, just mark it as answer so that we can move on
    Monday, May 25, 2009 11:30 PM