Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Enum reverse use? 1

Status
Not open for further replies.

Qik3Coder

Programmer
Jan 4, 2006
1,487
US
I am working with dates. I am writing a function which accepts the month abbr and returns the integer value for the month. I was hoping that this would be super easy and i wouldnt need a case statement.

What i have:
Code:
    Class Month
        Enum Months
            JAN = 1
            FEB = 2
            MAR = 3
            APR = 4
            MAY = 5
            JUN = 6
            JUL = 7
            AUG = 8
            SEP = 9
            OCT = 10
            NOV = 11
            DEC = 12
        End Enum
        Public Shared Function strHostMonthToIntMonth(ByVal strMonth) As Integer
            Dim intMonth As Integer
            Select Case strMonth
                Case "JAN"
                    intMonth = Month.Months.JAN
                Case "FEB"
                    intMonth = Month.Months.FEB
            End Select
            Return intMonth
        End Function
    End Class

What i am trying to do is find a "one liner" for giving the string "constant" with the enum and getting the integer value back.

Thanks,


-The answer to your problem may not be the answer to your question.
 
I'm not sure if it'll work (or if it's the best way) but you could try CallByName(objectref, proc name, call type). If not that, maybe through reflection.

Or someone else will post a super easy 1 line way to do it ;)

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
i thought it would be possible.
I mean, you have a set of pairs, you are able to get the values from the name, why not the name from the value?

-The answer to your problem may not be the answer to your question.
 
You could try:

Code:
Class Month
	Enum Months
		JAN = 1
		FEB = 2
		MAR = 3
		APR = 4
		MAY = 5
		JUN = 6
		JUL = 7
		AUG = 8
		SEP = 9
		OCT = 10
		NOV = 11
		DEC = 12
	End Enum

	Public Shared Function strHostMonthToIntMonth(ByVal strMonth As String) As Integer

		Try
			Return CType([Enum].Parse(GetType(Months), strMonth.ToUpper), Integer)
		Catch
			Return Nothing
		End Try


	End Function
End Class

Hope this helps.

[vampire][bat]
 
I almost understand what that is doing.

Now, for double or nothing (double what?):

is there a way to do the reverse of that, using the number get the string from the integer value?
So that i could give it 1 and get JAN?

Thanks,

-The answer to your problem may not be the answer to your question.
 
Yea...


[Enum].GetName(GetType(Month.Month), 1)


So now i can do this:

Enum MON
JAN = 1
FEB = 2
End enum

Enum Month
January = 1
February = 2
End enum
Code:
Function shortToLong(strMonAbbr as string) as string
return _
[Enum].GetName(GetType(Month), _
CType([Enum].Parse(GetType(MON), strMonth.ToUpper), Integer))
end function

so now i can give it JAN and get January...

Yea

Thanks,

-The answer to your problem may not be the answer to your question.
 
... or you could build a number of functions to give flexibilty, such as:

Code:
Class Month
	Enum ShortMonths
		JAN = 1
		FEB = 2
		MAR = 3
		APR = 4
		MAY = 5
		JUN = 6
		JUL = 7
		AUG = 8
		SEP = 9
		OCT = 10
		NOV = 11
		DEC = 12
	End Enum

	Enum FullMonths
		JANUARY = 1
		FEBRUARY = 2
		MARCH = 3
		APRIL = 4
		MAY = 5
		JUNE = 6
		JULY = 7
		AUGUST = 8
		SEPTEMBER = 9
		OCTOBER = 10
		NOVEMBER = 11
		DECEMBER = 12
	End Enum

	Public Shared Function GetMonthIntFromShortName(ByVal strMonth As String) As Integer

		Try
			Return CType([Enum].Parse(GetType(ShortMonths), strMonth.ToUpper), Integer)
		Catch
			Return Nothing
		End Try

	End Function

	Public Shared Function GetFullMonthNameFromShortMonthNameIndirect(ByVal strMonth As String) As String

		Try
			Return [Enum].GetName(GetType(FullMonths), GetMonthIntFromShortName(strMonth))
		Catch
			Return Nothing
		End Try

	End Function

Hope this helps.

[vampire][bat]
 
That's what i was going for. I wanted the concept, so that i can use it for other things that i have, like days of the week, and stuff.

Thanks e&f and Rick

-The answer to your problem may not be the answer to your question.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top