-1

For example:

Enum parameter1Choices
 choice1
 choice2
End Enum

Function sampleFunction(parameter1 as parameter1Choices)
 return parameter1
End Function

So if I use the above function like this

sampleFunction(parameter1Choices.choice1)

I am expecting that it will return choice1 as string

I've read this and it says that I should use Enum.GetName, some said .ToString. How do I use that?

Community
  • 1
  • 1
Cary Bondoc
  • 2,923
  • 4
  • 37
  • 60
  • Yeah, already tried it. It return 0. :( I'll try again on using enum.getnames – Cary Bondoc Jun 02 '16 at 07:51
  • 1
    `Enum.ToString()` call `GetNames` method internally. So your custom method `sampleFunction` doing exactly same job as `ToString`. What is the point? – Fabio Jun 02 '16 at 08:39

3 Answers3

2

Just use ToString:

Function sampleFunction(parameter1 As parameter1Choices) As String
    Return parameter1.ToString()
End Function
Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
  • Hi, thanks for the answer. It seems that using `parameter1.ToString` is much slower compared to `.GetName` it is based [here](http://stackoverflow.com/a/17034624/2947415) – Cary Bondoc Jun 02 '16 at 07:58
  • 1
    You didn't mention that this was speed critical in your question. The difference between them is unlikely to make any difference unless you are calling this 1000's of times. Even then the difference is in the milliseconds – Matt Wilko Jun 02 '16 at 08:04
  • Yeah, mine is not speed critical. I just thought that maybe I could bring it up here. Thanks! Your's is much straightforward. – Cary Bondoc Jun 02 '16 at 08:05
  • 2
    A quick and rough speed test reveals that ToString takes roughly `0.0011ms` per call and GetName takes roughly `0.0006ms` per call – Matt Wilko Jun 02 '16 at 08:09
0

Seems like the answer is

Function sampleFunction(parameter1 As parameter1Choices) As String
    Return [Enum].GetName(GetType(parameter1Choices), parameter1)
End Function
Cary Bondoc
  • 2,923
  • 4
  • 37
  • 60
0

If speed is a big issue the you might try looking the value up in a dictionary

Public Enum ParameterChoice
    None
    FirstChoice
    SecondChoice
End Enum

Imports System
Imports System.Collections.Generic

Public Class ChoicesLookup
    Private Shared _enumLookup As Dictionary(Of ParameterChoice, String)
    Shared Sub New()
        _enumLookup = New Dictionary(Of ParameterChoice, String)
        For Each choice As ParameterChoice In [Enum].GetValues(GetType(ParameterChoice))
            _enumLookup.Add(choice, choice.ToString())
        Next
    End Sub

    Public Shared Function GetChoiceValue(myChoice As ParameterChoice) As String
        GetChoiceValue = _enumLookup(myChoice)
    End Function

    'prevents instantiation
     Private Sub New()
     End Sub
End Class

Imports System.Text
Imports Microsoft.VisualStudio.TestTools.UnitTesting

<TestClass()> Public Class UnitTest1

    <TestMethod()> Public Sub TestMethod1()
        Assert.AreEqual("FirstChoice", ChoicesLookup.GetChoiceValue(ParameterChoice.FirstChoice))
    End Sub
End Class
AlanT
  • 3,627
  • 20
  • 28