Select Case conversion not displaying properly, how can i fix? - vb.net

I have an executable that when run takes an inputted date and then selects what date suffix is needed with it, however it is always defaulting to the case else.
when i have 02/04/2017 as format DD/MM/YYYY the result of the below case is 2th April instead of 2nd...
Could anyone enlighten me on the problem.
Dim Datewc As Date = Nothing
If CheckBox1.Checked Then
Datewc = TextBox1.Text
End If
'Determine date suffix
Dim datsuff As String = ""
Select Case CInt(Datewc.Day)
Case 1 Or 21 Or 31
datsuff = "st"
Case 2 Or 22
datsuff = "nd"
Case 3 Or 23
datsuff = "rd"
Case Else
datsuff = "th"
End Select

Parse you date
Dim dDate As DateTime =
DateTime.ParseExact(TextBox1.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture)
Change your code a little, should work.
Day function returns integer, no need to Cast using CInt.
'Determine date suffix
Dim datsuff As String = ""
Select Case Datewc.Day
Case 1 , 21 , 31
datsuff = "st"
Case 2 , 22
datsuff = "nd"
Case 3 , 23
datsuff = "rd"
Case Else
datsuff = "th"
End Select
Explanation
Case 2 Or 22 means Case ((2 Or 22) ==true), It will be false hence going to else part.

Related

Change month to letter in VBA

Hello I have variable with current date and I want to change the month of the date to the name of the month or any letter.
My variable has value: 19.10.2020
In short, I want to change 10 to oct or, for example, AA
How can i do it in VBA?
Whenever having a formatting question, a good option is to see what the VBA Excel macro recorder does, when the task is carried out.
In this case, formatting the cells like this:
will generate the following code:
Sub Macro1()
'
' Macro1 Macro
'
'
Selection.NumberFormat = "[$-409]d-mmm-yy;#"
Range("G8").Select
End Sub
Thus, Range("A1").NumberFormat = "[$-409]d-mmm-yy;#" would be a working answer.
You can write a conversion function.
For example:
Private Sub CommandButton1_Click()
MsgBox (GetNewMonth("19.10.2020"))
End Sub
Private Function GetNewMonth(ByVal datestring As String) As String
Dim splitted() As String
splitted = Split(datestring, ".")
Dim day As String
Dim month As String
Dim year As String
day = splitted(0)
month = splitted(1)
year = splitted(2)
GetNewMonth = day & " " & ConvertMonthNumToLetter(CInt(month)) & " " & year
End Function
Private Function ConvertMonthNumToLetter(ByVal monthnum As Integer)
Select Case monthnum
Case 1
ConvertMonthNumToLetter = "JAN"
Case 2
ConvertMonthNumToLetter = "FEB"
Case 3
ConvertMonthNumToLetter = "MAR"
Case 4
ConvertMonthNumToLetter = "APR"
Case 5
ConvertMonthNumToLetter = "MAI"
Case 6
ConvertMonthNumToLetter = "JUN"
Case 7
ConvertMonthNumToLetter = "JUL"
Case 8
ConvertMonthNumToLetter = "AUG"
Case 9
ConvertMonthNumToLetter = "SEP"
Case 10
ConvertMonthNumToLetter = "OCT"
Case 11
ConvertMonthNumToLetter = "NOV"
Case Else
ConvertMonthNumToLetter = "DEC"
End Select
End Function
OUTPUT:
19 OCT 2020
First, convert to a string that can be read as a date, then format this - like:
=Format(Replace("AA", ".", "/"), "d.mmm.yyyy")
-> 10.Okt.2020

Select Case statement and data validation

I am trying to validate data ranges using the select case statement. I am having issues with the other nested select cases. Is this possible or am i wishfull thinking? Or should i separate the select case statements to be stacked?
For instance this is my code in vb:
Select Case intyear
Case 2000 To 2025
Select Case intmonth
Case 1 To 12
BlnDateValid = True
End Select
Select Case intDay
Case 1 To 31
BlnDateValid = True
End Select
Select Case intHours
Case 0 To 23
BlnDateValid = True
End Select
Select Case intAddDays
Case 0 To 60
BlnDateValid = True
End Select
Select Case intAddHours
Case 0 To 23
BlnDateValid = True
End Select
Case Else
BlnDateValid = False
End Select
If blnDatevalid = false then
MessagebBox.Show("Please check all fields and enter valid
data", "Invalid data", MessageBoxButtons.OK)
Unfortunately, the indenting making sense doesn't help the code make sense. The whole point of Select Case is to neatly select one of multiple cases. A Select Case with one case is bad code and you should be using an If statement instead. You should especially be using a If statement in this case because you can replace all those Select Case statements with a single If statement.
If Not (intyear >= 2000 AndAlso intyear <= 2025 AndAlso
intmonth >= 1 AndAlso intmonth <= 12 AndAlso
intDay >= 1 AndAlso intDay <= 31 AndAlso
intHours >= 0 AndAlso intHours <= 23 AndAlso
intAddDays >= 0 AndAlso intAddDays <= 60 AndAlso
intAddHours >= 0 AndAlso intAddHours <= 23) Then
MessagebBox.Show("Please check all fields and enter valid data", "Invalid data", MessageBoxButtons.OK)
End If
All the Select or If checks will still leave you vulnerable to non-sensical values like February 30. Better to actually attempt to create a DateTime value.
Dim d As DateTime
Dim t As TimeSpan
Try
d = New DateTime(intYear, intMonth, intDay, intHours, 0, 0)
t = New TimeSpan(intAddDays, intAddHours, 0, 0)
If t > (New TimeSpan(60, 23, 0, 0)) Then Throw New ArgumentOutOfRangeException()
Catch
MessagebBox.Show("Please check all fields and enter valid data", "Invalid data", MessageBoxButtons.OK)
End Try
or you can create a string and try parsing it:
If Not DateTime.TryParse($"{intYear}-{intMonth}-{intDay} {intHours}:00:00")
MessagebBox.Show("Please check all fields and enter valid data", "Invalid data", MessageBoxButtons.OK)
End If

convert a text to a number

I don't know why this code doesn't work ..i wanna just convert text to number .. It doesn't give me any error but it doesn't work
Public Function ConvertCOMPLEXITYToNumber(ByVal chain As String) As Integer
Select Case chain
Case "1 - Très difficile"
ConvertCOMPLEXITYTToNumber = 1
Case "2 - Difficile"
ConvertCOMPLEXITYTToNumber = 2
Case "3 - Modérée"
ConvertCOMPLEXITYTToNumber = 3
Case "4 - Facile"
ConvertCOMPLEXITYTToNumber = 4
Case Else
ConvertCOMPLEXITYTToNumber = 0
End Select
Exit Function
End Function
That may be because you may have unwanted leading or trailing spaces which fails the comparison. Also you do not need Exit Function at the end of the code. It will exit any ways :)
Try this
Public Function ConvertCOMPLEXITYToNumber(ByVal chain As String) As Integer
Dim Num As Integer
Select Case Trim(chain)
Case "1 - Très difficile": Num = 1
Case "2 - Difficile": Num = 2
Case "3 - Modérée": Num = 3
Case "4 - Facile": Num = 4
Case Else: Num = 0
End Select
ConvertCOMPLEXITYToNumber = Num
End Function
If IsNumeric(Left(Trim(chain),1)) Then
ConvertCOMPLEXITYToNumber = Left(Trim(chain),1)
Else
ConvertCOMPLEXITYToNumber = 0
End If
Here, my approach for you:
Public Function ConvertCOMPLEXITYToNumber(ByVal chain As String) As Integer
'Get the first character
chain = Left(Trim(chain), 1)
'If frist character is numeric
If IsNumeric(chain) Then
'If first number is less than 5, return value
If chain < 5 Then
ConvertCOMPLEXITYToNumber = CInt(chain)
End If
End If
End Function

Multiple select cases in VB.NET

I have tried the below:
Select Case Combo1.SelectedItem Or Combo2.SelectedItem
But I get the error:
Conversion from String "string here" to type 'Long' is not valid
Is it possible to have multiple select cases?
You separate multiple values by using a comma:
Case Combo1.SelectedItem, Combo2.SelectedItem
Using Or would make it an expression that would be evaluated before compared to the value in the Select.
If your value in the Select is a Long value, then you may need to convert the strings from the controls:
Case CLng(Combo1.SelectedItem), CLng(Combo2.SelectedItem)
To address the question directly, using multiple values as a test expression in a select is not possible:
Select Case v1, v2 'Not possible
Hi Googled and saw this question without an answer. Upon further research, I found this to work for my purposes.
Basically, you start with:
Select case True
Then each case statement can be combinations of the two variables. When both are met, the case is true and will execute the appropriate code.
https://forums.asp.net/t/611892.aspx?To+do+a+select+case+using+two+variables+or+parameters
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim i, j As Integer
Dim value As Integer
For i = 1 To 3
For j = 1 To 5
value = (GetCode(i, j))
TextBox1.Text = TextBox1.Text & "i=" & i & "->j=" & j & "=" & value & vbCrLf
Next
Next
End Sub
Function GetCode(ByVal v1 As Integer, ByVal v2 As Integer) As Integer
Dim retval As Integer
Dim forselect As String
forselect = v1 & v2
Select Case forselect
Case 11
retval = 11
Case 12
retval = 12
Case 13
retval = 13
Case 14
retval = 14
Case 15
retval = 15
Case 21
retval = 21
Case 22
retval = 22
Case 23
retval = 23
Case 24
retval = 24
Case 25
retval = 25
Case 31
retval = 31
Case 32
retval = 32
Case 3, 3
retval = 33
Case 34
retval = 34
Case 35
retval = 35
End Select
Return retval
End Function

subtract if the day of the week is a saturday or sunday

if the subtracted date is a saturday or sunday then subtract more days, before adding to arraylist
when i do this, the date stays the same and doesnt subtract, i get a conversion string to double error
Dim aftersubtraction As Date
Dim fromatafter As Date
aftersubtraction = departuredate.AddDays(-dates1.Text)
fromatafter = aftersubtraction.AddDays(-gracep.Text)
If fromatafter.DayOfWeek = "Saturday" Then
fromatafter.AddDays(-1)
dates.Add(fromatafter.ToString("MM/dd/yyyy"))
ElseIf fromatafter.DayOfWeek = "Sunday" Then
fromatafter.AddDays(-2)
dates.Add(fromatafter.ToString("MM/dd/yyyy"))
Else
dates.Add(fromatafter.ToString("MM/dd/yyyy"))
End If
While fromatafter.DayOfWeek = DayOfWeek.Saturday OrElse fromatafter.DayOfWeek = DayOfWeek.Sunday
fromatafter = fromatafter.AddDays(-1)
End While
dates.Add(fromatafter.ToString("MM/dd/yyyy"))
You have to assigne the date returned from AddDays to your variable.
And please set option strict and explicit. Why?
if im not mistaken the vb dayofweek function returns an integer and not a string.
' 0 = Sunday
' 1 = Monday
' 2 = Tuesday
' 3 = Wednesday
' 4 = Thursday
' 5 = Friday
' 6 = Saturday
hence the error message.
your getting the string to double error on the
line with the if statement... becase .dayofweek does not return a string... it returns an integer... so an integer is never going to be = "Saturday" or "Sunday"... its going to be equal to 0 or 1