Select Case statement with multiple varaibles

  • Thread starter microsoft.public.dotnet.languages.vb
  • Start date
M

microsoft.public.dotnet.languages.vb

Hi All,

I wanted to know whether this is possible to use multiple variables to
use in the select case statement such as follows:

select case dWarrExpDateMonth, dRetailDateMonth
case "01" : dWarrExpDateMonth="Jan" : dRetailDateMonth="Jan"
case "02" : dWarrExpDateMonth="Feb" : dRetailDateMonth="Feb"
End Select

The reason is I have several date varaibles (mmddyy format) and I want
to display the months in word such as above. Right now I have repeated
the same select case statement for different variables. I was wondering
how can I avoid repeating the select case statement for different
variables which will be nothing but different months.

Thanks a million in advance.
Best regards,
mamun
 
A

Anthony Jones

microsoft.public.dotnet.languages.vb said:
Hi All,

I wanted to know whether this is possible to use multiple variables to
use in the select case statement such as follows:

select case dWarrExpDateMonth, dRetailDateMonth
case "01" : dWarrExpDateMonth="Jan" : dRetailDateMonth="Jan"
case "02" : dWarrExpDateMonth="Feb" : dRetailDateMonth="Feb"
End Select

The reason is I have several date varaibles (mmddyy format) and I want
to display the months in word such as above. Right now I have repeated
the same select case statement for different variables. I was wondering
how can I avoid repeating the select case statement for different
variables which will be nothing but different months.

Thanks a million in advance.
Best regards,
mamun

I think you're quite confused as to the Select Case syntax. You should RTM
here:-

http://msdn.microsoft.com/library/en-us/script56/html/91c340af-8ceb-4f46-86fa-7871eefb3b01.asp


In all VB dialects a new line delimits one statement from another. The
colon : is also such a delimiter allow mulitple statements to appear on one
line. A common use of : in VB is in a Case statement:-

Select Case testExpression
Case expression : somevar = "some literal"
Case expression : somevar = "some other literal"
End Select

but this is just short hand for:-

Select Case testExpression
Case expression
somevar = "some literal"
Case expression
somevar = "some other literal"
End Select

Any case block can contain any number of statements which are only executed
if it's expression it the first one in the list of expressions to match the
testExpression.

Hence this is possilbe:-


Select case dWarrExpDateMonth
Case "01" : dWarrExpDateMonth="Jan" : dRetailDateMonth="Jan"
Case "02" : dWarrExpDateMonth="Feb" : dRetailDateMonth="Feb"
End Select

However note that you can only have one test expression.

At guess though I think you may have expected that dWarrExpDateMonth and
dRetailDateMonth to contain differerent values yet be resolved to their
appropriate abbreviation.

In that case a Function is a more appropriate solution:-

Function CvtToMonthAbbrv(rsIn)
Select Case rsIn
Case "01" : CvtToMonthAbbrv="Jan"
Case "02" : CvtToMonthAbbrv="Feb"
End Select
End Function

dWarrExpDateMonth = CvtToMonthAbbrv(dWarrExpDateMonth )
dRetailDateMonth= CvtToMonthAbbrv(dRetailDateMonth)

Having said all that (for the purpose of education) the true solution to
your problem is that VBScript already has such a function built in :-

dWarrExpDateMonth = MonthName(CInt(dWarrExpDateMonth), True)
dRetailDateMonth= MonthName(CInt(dRetailDateMonth), True)

http://msdn.microsoft.com/library/en-us/script56/html/99221560-8a0a-4f65-a2c2-f263a9bdb241.asp


Anthony.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top