Multiple select cases in VB.NET - 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

Related

Automatic Calculation with given numbers

I would like to make CPU to calculate declared result from the given numbers that are also declared.
So far:
Dim ArrayOperators() As String = {"+", "-", "*", "/", "(", ")"}
Dim GlavniBroj As Integer = GBRnb() 'Number between 1 and 999 that CPU needs to get from the numbers given below:
Dim OsnovniBrojevi() As Integer = {OBRnb(), OBRnb(), OBRnb(), OBRnb()} '4 numbers from 1 to 9
Dim SrednjiBroj As Integer = SBRnb() '1 number, 10, 15 or 20 chosen randomly
Dim KrajnjiBroj As Integer = KBRnb() '25, 50, 75 or 100 are chosen randomly
Private Function GBRnb()
Randomize()
Dim value As Integer = CInt(Int((999 * Rnd()) + 1))
Return value
End Function
Private Function OBRnb()
Dim value As Integer = CInt(Int((9 * Rnd()) + 1))
Return value
End Function
Private Function SBRnb()
Dim value As Integer = CInt(Int((3 * Rnd()) + 1))
If value = 1 Then
Return 10
ElseIf value = 2 Then
Return 15
ElseIf value = 3 Then
Return 20
End If
Return 0
End Function
Private Function KBRnb()
Dim value As Integer = CInt(Int((4 * Rnd()) + 1))
If value = 1 Then
Return 25
ElseIf value = 2 Then
Return 50
ElseIf value = 3 Then
Return 75
ElseIf value = 4 Then
Return 100
End If
Return 0
End Function
Is there any way to make a program to calculate GlavniBroj(that is GBRnb declared) with the help of the other numbers (also without repeating), and with help of the given operators? Result should be displayed in the textbox, in a form of the whole procedure of how computer got that calculation with that numbers and operators. I tried to make it work by coding operations one by one, but that's a lot of writing... I'm not looking exactly for the code answer, but mainly for the coding algorithm. Any idea? Thanks! :)

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

Using Excel's comparison operator in VBA?

I'm trying to figure out how I can use the same comparison operator that Excel uses in sorting a mix of alphanumeric values like the following:
0
9
34
51
123abc
15
a
a1b23c
i
z
34ui
10
d
1
12
When sorting this, this is the result:
0
1
9
10
12
15
34
51
123abc
34ui
a
a1b23c
d
i
z
Is it possible to use the comparison operator that Excel uses to get this result? Or is it necessary to create my own function for this?
I just went ahead and created a comparison function with the same return values as StrComp() since it seems there isn't one already.
Function ExcelCompare(ByVal str1 As String, ByVal str2) As Integer
Dim isnum1 As Boolean
Dim isnum2 As Boolean
isnum1 = IsNumeric(str1)
isnum2 = IsNumeric(str2)
ExcelCompare = StrComp(str1, str2)
If isnum1 And Not isnum2 Then
ExcelCompare = -1
ElseIf Not isnum1 And isnum2 Then
ExcelCompare = 1
ElseIf isnum1 And isnum2 Then
Dim num1 As Double
Dim num2 As Double
num1 = CDbl(str1)
num2 = CDbl(str2)
If num1 = num2 Then
ExcelCompare = 0
ElseIf num1 < num2 Then
ExcelCompare = -1
Else
ExcelCompare = 1
End If
End If
End Function

Generate random string in text field

We have that old software (made by one of the first employees many years ago) in company that uses Microsoft Access to run. Boss asked me to add a random string generation in the specific text box on click but i have no idea how to do that. I dont have any Microsoft Access programming experience, thats why i am askin you to help.
I managed to create button and text field so far. Thats where it stops. I also managed to access the code for the button action:
Private Sub command133_Click()
End Sub
This is one way, will work in Access VBA (which is an older basic than vb.net). It will generate a string with letters and numbers.
Sub test()
Dim s As String * 8 'fixed length string with 8 characters
Dim n As Integer
Dim ch As Integer 'the character
For n = 1 To Len(s) 'don't hardcode the length twice
Do
ch = Rnd() * 127 'This could be more efficient.
'48 is '0', 57 is '9', 65 is 'A', 90 is 'Z', 97 is 'a', 122 is 'z'.
Loop While ch < 48 Or ch > 57 And ch < 65 Or ch > 90 And ch < 97 Or ch > 122
Mid(s, n, 1) = Chr(ch) 'bit more efficient than concatenation
Next
Debug.Print s
End Sub
Try this function:
Public Function GetRandomString(ByVal iLength As Integer) As String
Dim sResult As String = ""
Dim rdm As New Random()
For i As Integer = 1 To iLength
sResult &= ChrW(rdm.Next(32, 126))
Next
Return sResult
End Function
Workin on #Bathsheba code, I did this. It will generate a random string with the number of characters you'd like.
Code :
Public Function GenerateUniqueSequence(numberOfCharacters As Integer) As String
Dim random As String ' * 8 'fixed length string with 8 characters
Dim j As Integer
Dim ch As Integer ' each character
random = ""
For j = 1 To numberOfCharacters
random = random & GenerateRandomAlphaNumericCharacter
Next
GenerateUniqueSequence = random
End Function
Public Function GenerateRandomAlphaNumericCharacter() As String
'Numbers : 48 is '0', 57 is '9'
'LETTERS : 65 is 'A', 90 is 'Z'
'letters : 97 is 'a', 122 is 'z'
GenerateRandomAlphaNumericCharacter = ""
Dim i As Integer
Randomize
i = (Rnd() * 2) + 1 'One chance out of 3 to choose one of 3 catégories
Randomize
Select Case i
Case 1 'Numbers
GenerateRandomAlphaNumericCharacter = Chr(Rnd() * 9 + 48)
Case 2 'LETTERS
GenerateRandomAlphaNumericCharacter = Chr(Rnd() * 25 + 65)
Case 3 'letters
GenerateRandomAlphaNumericCharacter = Chr(Rnd() * 25 + 97)
End Select
End Function
I use it with random number of characters, like this :
'Generates random Session ID between 15 and 30 alphanumeric characters
SessionID = GenerateUniqueSequence(Rnd * 15 + 15)
Result :
s8a8qWOmoDvC4jKRjPr5hOY12u 26
TB24qZ4cNfr6EdyY0J 18
6LZRQ9P5WHLNd71LIdqJ 20
KPN0RmlhhJKnVzPTkW 18
R2pNOKWJMKl9KpSoIV2egUNTEb1QC2 30
X8jHuupP6SvEI8Dt2wJi 20
NOTE: This is still not completely random. It will give a higher count of numbers than normal as approx 1/3 of all chars generated will be numbers.
Normally distribution will look like:
10 numbers plus 26 lowercase plus 26 uppercase = 62 possible chars.
Numbers will normally be 10/62 parts of the string or 1/6.2
With the code
i = (Rnd() * 2) + 1 'One chance out of 3 to choose one of 3 catégories
the count of numbers is pushed up to 1/3 (on average)
Probably not too much of a worry - unless you are trying to beat the NSA and then you have decreased your range significantly.

Simple rot13 encoder in vb.net

I am looking for a simple way to encode an inputted text into Rot13. I am hitting a brick wall at the stage of being able to separate out words into individual characters and integers so that I can change each one and output the result. I can do it with single letters using a simple if statement listed bellow but if anyone can help with a way of doing it for whole words I would be very appreciative.
If kInput = "a" then kOutput = "n"
Thanks, Kai
Looks like people are giving good answers to this but here's my try at it.
Dim input As String = "This is a Test!! Guvf vf n Grfg!!"
Dim result As StringBuilder = New StringBuilder()
For Each ch As Char In input
If (Not Char.IsLetter(ch)) Then
result.Append(ch)
Continue For
End If
Dim checkIndex As Integer = Asc("a") - (Char.IsUpper(ch) * -32)
Dim index As Integer = ((Asc(ch) - checkIndex) + 13) Mod 26
result.Append(Chr(index + checkIndex))
Next
Console.WriteLine(result.ToString())
EDIT: improved to remove need for uppercase check. This will properly handle case and special characters with only 1 if statement inside the loop.
It seems like you're making this way harder than it has to be. No need to separate words, etc, and definitely no need for a large If/Else block:
Public Function Rot13(ByVal input As String) As String
Dim result As Char() = input.ToCharArray()
For i As Integer = 0 To result.Length - 1
Dim temp As Integer = Asc(result(i))
Select Case temp
Case 65 to 77, 97 To 109 'A - M
result(i) = Chr(temp + 13)
Case 78 to 90, 110 To 122 'N - Z
result(i) = Chr(temp - 13)
End Select
Next i
Return New String(result)
End Function
Note that this was entered directly into the browser window and is completely untested.
Just call it once to encode, call it again to decode.
Private Function ROT13_Encode(ByVal Input As String) As String
Dim chrs As Char() = Input.ToCharArray()
Dim ReturnString As String = ""
Dim CharInt As Integer
For Each Chr As Char In chrs
CharInt = Asc(Chr)
If CharInt >= 65 And CharInt <= 77 Then 'A-M
CharInt += 13
ElseIf CharInt >= 78 And CharInt <= 90 Then 'M-Z
CharInt -= 13
ElseIf CharInt >= 97 And CharInt <= 109 Then 'a-m
CharInt += 13
ElseIf CharInt >= 110 And CharInt <= 122 Then 'm-z
CharInt -= 13
End If
ReturnString &= ChrW(CharInt)
Next
Return ReturnString
End Function