Pass in Array to Select Case - vb.net

this may be a dense question and I'm not finding it to be a dup of this one, but I need some help with understanding if an array can be used in a Select Case statement.
I have a sub routine that I will create a string array dynamically. The XML is also listed, but it could be any of the values listed below. It will be something like this:
Dim offensiveLine() As String = New String() {"center", "right wing", "left wing"}
Dim defensiveLine As String = "defense"
Dim playerInfo = <Player><Name>John</Name><Position val="right wing"/></Player>
What I want to do is see if this player is in one of the offensiveLine. So I say:
Dim playerPosition = playerInfo.Position.#val
Select Case playerPosition
Case offensiveLine
'do something
Case defensiveLine
'do something
Case Else
'do nothing
End Select
Here is lies the issue: Case offensiveLine is invalid. I know I could write out Case "center", "right wing", "left wing", but that would defeat the purpose of what I'm trying to do, which is to make a generalized variable that is an array that can be read from in a Case statement. Secondly, I know I can't create a variable like Dim offensiveLine = ""center", "right wing", "left wing"" and pass that in.
Any insight on how I may be able to pass in an array to a Case statement and have each one evaluated?

You may want to consider using an if clause here rather than a switch. Try this logic: if offensiveLine contains playerPosition, then offensive line, etc.

You actually can use a Select statement:
Dim playerPosition = playerInfo.Position.#val
Select Case True
Case offensiveLine.Contains(playerPosition)
'do something
Case defensiveLine.Contains(playerPosition)
'do something
Case Else
'do something - otherwise you don't need the 'Case Else'
End Select
The trick is the True in the first line of the Select.

The Select..Case construct does not work like that. However, it is easy to test if an element exists in an array:
If offensiveLine.Contains(playerPosition) Then
'Do something
ElseIf defensiveLine.Contains(playerPosition) Then
'Do something else
End If

Related

MS Acces - select case mixing tables

I have problem with VBA code select case. Code is linked to combobox(Combo0) and event after update. In combobox there is a list of values: BHP, PPOZ, ISO. When i choose BHP, it should change source object property in subfrom(Child03) to subfrmBHP. Same with PPOZ and ISO. But for some reason, when i choose BHP it shows table from PPOZ, when i choose PPOZ it shows BHP and when i choose ISO it shows BHP. Code looks like this:
Private Sub Combo0_AfterUpdate()
Select Case ChangeCombo
Case Combo0 = "BHP"
Forms("frmInstrukcje").Form.Child3.SourceObject = "subfrmBHP"
Case Combo0 = "PPOŻ"
Forms("frmInstrukcje").Form.Child3.SourceObject = "subfrmPPOZ"
Case Combo0 = "ISO"
Forms("frmInstrukcje").Form.Child3.SourceObject = "subfrmISO"
End Select
End Sub
I aleredy recreated tables, form, sub form. Someone suggested me it could be data binding in subform, but there is nothing written there.
Your method references two controls, ChangeCombo and Combo0. Not sure if it's a typo or you have something else in mind.
Anyway, Case Combo0 = "BHP" is incorrect, so assuming Combo0 is the correct control, try the below.
Private Sub Combo0_AfterUpdate()
With Forms("frmInstrukcje").Form.Child3
Select Case Me.Combo0.Value
Case "BHP":
.SourceObject = "subfrmBHP"
Case "PPOŻ":
.SourceObject = "subfrmPPOZ"
Case "ISO":
.SourceObject = "subfrmISO"
End Select
End With
End Sub

Calling an Access VBA function with IF statement as an argument

Assuming I have the following MS Access 2016 function:
Public Function TestFunction(ByVal strTest As String) As String
Is there a way to call the function with something similar to:
strReturn = TestFunction(If a = 1 Then "Apple" Else "Orange")
I would like to write something as compact as possible, and avoid having to write multiple lines for the call, such as:
If a = 1 Then strArg = "Apple" Else strArg = Orange
strReturn = TestFunction(strArg)
You can accomplish this using IIf, e.g.:
strReturn = TestFunction(IIf(a=1,"Apple","Orange"))
But this is generally discouraged when working with expressions other than constants, because IIf will always evaluate both the then and else argument before returning the appropriate value depending on the outcome of the test expression, which can sometimes lead to undesired results.
For example, evaluating the following expression at the Immediate Window (Ctrl+G) will result in a division by zero error, even though the else expression will never be returned:
?iif(true,"Apple",1/0)
If the value for a comes from a well defined range then the solution would be to use either the Choose Function for a contiguous range or Switch Function for a non contiguous well defined range.
Of course the more grown up solution would be to replace both Choose or Switch with a Dictionary.
Why not something like this for multiple possibilities
Dim myArg as String
Select Case a
Case 1: myArg = "Apple"
Case 2: myArg = "Orange"
Case 3: myArg = "Pear"
'etc.
End Select
strReturn = TestFunction(myArg)

Using SQL Wildcards with LINQ

I have a question about using LINQ to access a Database and trying to make use of it's version of accessing the LIKE comparison operator.
I know that LINQ has .Contains(), .StartsWith(), and .EndsWith() as comparison methods. However I am wondering if there is a way to programatically imcorporate SQL Wildcards into a LINQ statement without explicitly using these query operators. Let me explain my situation.
I am writing a program that accesses a database, and part of the program is a search window which the user can use to help them find specific database data. I would like to try and incorporate SQL Wildcards into the textbox fields for these search pages.
For example if a user enters the input 17% I'd want the program to check for anything in that specific column that starts with a 17. The same is true with %17 and 17 where I'd want it to search for columns that end with, and contain the values.
Currently, this is the code I have for my search method:
Public Function Data_Search(sData As List(Of String), DB As CustomDataContext) As DataGridView
Dim gridData As New DataGridView
Dim query = From p In DB.Parts
Select p.Part_Number, p.Part_Description, p.Supplier.Supplier_Name
for i = 0 To sData.Count - 1
If Not sData(i).ToString() = "" Then
Select Case i
Case 0
Dim partNum As String = sData(i).ToString()
query = query.Where(Function(x) x.Part_Number.Contains(partNum))
Case 1
Dim description As String = sData(i).ToString()
query = query.Where(Function(x) x.Part_Description.Contains(description))
Case 2
Dim supp As String = sData(i).ToString()
query = query.Where(Function(x) x.Supplier_Name.Contains(supp))
End Select
End If
Next
gridData.DataSource = query.ToList()
Return gridData
End Function
So right now I am trying to see if there is a way for me to modify the code in a way that doesn't essentially involve me putting a substring search into each Case section to determine if I should be using StartsWith(), Contains(), or EndsWith.
Thanks for your time.
If you are using LINQ to SQL, and you are talking to Microsoft SQL Server, then you can use SQLMethods.Like to implement SQL LIKE:
query = query.Where(Function(x) SQLMethods.Like(x.Part_Number, partNum))

VBA Variable Scope in a Case Statement

Wondering how variables work when used in a Case statement. It seems like they are declared in the first Case, regardless of whether that Case is relevant.
'The following code will throw an error
Select Case team
Case "Philadelphia Eagles"
dim record as String
Case "Dallas Cowboys"
dim record as String
End Select
Even if 'team' isn't Philadelphia Eagles, it claims I've already declared the variable 'record'
I was under the impression that anything in a case statement was completely skipped over if that case wasn't relevant.
'The following code works
Select Case team
Case "Philadelphia Eagles"
dim record as String
Case "Dallas Cowboys"
record = "8-8"
End Select
Just want to confirm that I am understanding the Case statement correctly here.
Thank you!
Josh
Variable declarations (Dim statements) are resolved at the start of the execution of the program, which is why your Dim record statements weren't "skipped" over in your example.
You should put your variable declarations once at the top of your code, just after you start the subroutine or function. You cannot use Dim on the same variable twice. If the variable is an array which you need to resize, you can use ReDim [Preserve] instead.
Sub Subname()
Dim record as String
Select Case team
Case "Philadelphia Eagles"
record = "16-0"
Case "Dallas Cowboys"
record = "8-8"
End Select
End Sub

Is it possible to define same variable twice in classic ASP?

Is it somehow possible to declare same variable in .asp file twice? Example below does not look very clever, but this is just an example and I have to sort it out.
Dim number : number = 1
Select Case number
Case 1
Dim a
Case 2
Dim a
End Select
Theoretically you can of course declare a variable twice, the problem is, asp will throw an error, if the variable is declared in the same scope.
whatever you want to achieve, keep in mind, you can (almost) always access the variables in parent scope, thus rendering a double declaration useless.
Dim number : number = 1
Dim a
Select Case number
Case 1:
a = "whatever"
Case 2:
a = "something different"
End Select
response.write a