visual basic array case statement - arrays

I'm new to visual basic and have been using vb.net to create a console/text based game. I want to make my game a little bit more "smart". The idea I've had to do this is to create an array of synonyms for yes and an array of synonyms for no, and similar arrays for over words. I intended on using a case statement with the array to decide weather the users input was a synonym for yes or for no. I have had no luck so far, and I was wondering if anybody here knew how it can be done or if I'm barking up the wrong tree. Maybe there is a different way for me to approach this?
My Select attempt:
Select Case userInput
Case yes(0) To yes(34)
Console.WriteLine("you said something like yes, you said {0}", userInput)
End Select
The start of my array: (there are 34 synonyms so far)
Dim yes(0 To 34) As String
yes(0) = "yes"
yes(1) = "ok"
yes(2) = "yep"
yes(3) = "yeah"
If anybody can help me it would be very much appreciated :) Thank you very much!

You do not have to use a Select Case for this purpose. A simple List(Of T).Contains can do the trick. Then you can go ahead to put it in a function so you can call it several times in your application.
Sample Code:
Public Enum Answer
Yes
No
Other
End Enum
Public Function GetAnswer(answer As String) As Answer
Static yesAnswers = New List(Of String)({"yes", "yeah", "yep", "aye"})
Static noAnswers = New List(Of String)({"no", "nope", "nay"})
If yesAnswers.Contains(answer.ToLower()) Then
Return Answer.Yes
ElseIf noAnswers.Contains(answer.ToLower()) Then
Return Answer.No
Else
Return Answer.Cancel
End If
End Function

I would create a translation dictionary
Dim translations As New Dictionary(Of String, String)
translations.Add("yes", "yes")
translations.Add("ok", "yes")
translations.Add("yep", "yes")
translations.Add("yeah", "yes")
translations.Add("no", "no")
translations.Add("nope", "no")
With these definitions you can get the standard version of an answer very easily
Dim userInput = "yeah"
Dim response As String = Nothing
translations.TryGetValue(userInput, response)
Select Case response
Case "yes"
Console.WriteLine("ok")
Case "no"
Console.WriteLine("cancel")
Case Else
Console.WriteLine("rubbish!")
End Select
Dictionaries have the fastest lookup times.

Related

why is the index of the one-dimensional array changing this way?

I'm a beginner and I'm encountering a problem with an index of a one-dimensional array of strings (m_nameList in the code) that I'm struggling to understand. I have one method (in a class of a Windows Form Application that is not the MainForm) that returns FALSE if a string of the array at a certain index is empty, and viceversa. It looks like this:
Public Function IsReserved(index As Integer) As Boolean
Dim reserved As Boolean = False
If (Not String.IsNullOrEmpty(m_nameList(index))) Then
reserved = True
Else
reserved = False
End If
Return reserved
End Function
I have also a method on the MainForm that calls for it, and if the first method returns true then the second one displays a dialog box, otherwise it doesn't (lstResults is a listbox that has for items the strings of the array):
Private Function CheckIfSeatIsAlreadyReserved() As Boolean
Dim reserved As Boolean = m_seatMngr.IsReserved(lstResults.SelectedIndex)
If (reserved) Then
Dim msgBox As Integer = MessageBox.Show("The seat is already reserved. Would you like to continue?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If (msgBox = DialogResult.Yes) Then
Return True
Else
Return False
End If
Else
Return True
End If
End Function
The problem I find is: if as in the code of the first method above I don't initialize index to any value, the second method will work perfectly, but I'll get a runtime error if I don't select any item on the listBox (and therefore index = -1). On the other hand, if I initialize index to 0 or to m_nameList.Length, then the program won't have any problems if I don't select any item on the ListBox and it will nicely display a warning message I wrote for that case, BUT the second method will behave in a weird way: it will find ALL the strings of the array full even if I only "write" the first one, or it will find ALL the strings empty if I "write" any string that it's not the first one (by writing I mean that I call for a method that puts my input as the text of the string selected). Any idea on why this happens and how I can fix it? Thank you so much.
I will get to the answer below, follow the logic, lets look at your first method. You can refactor it to
Public Function IsReserved(index As Integer) As Boolean
Return Not String.IsNullOrEmpty(m_nameList(index))
End Function
Everything else is called "noise". But as Fabio pointed, what if index is bad? Lets add useful code
Public Function IsReserved(index As Integer) As Boolean
If index < 0 OrElse index > m_nameList.Length - 1 Then
Throw New ArgumentOutOfRangeException(". . . . .")
End If
Return Not String.IsNullOrEmpty(m_nameList(index))
End Function
If you get this exception, your program has a bug - and you know to fix the consumer of this function.
And now lets get to your message
second method will behave in a weird way: it will find ALL the strings of the array full even if I only "write" the first one, or it will find ALL the strings empty if I "write" any string
Most likely reason why it happens - simply because you create condition when you send same value over and over again. Therefore result is same. And this is most likely a bug
If (reserved) Then
. . . . . .
Else
Return True ' <---- BUG ALLERT
End If
And two more things, your second method can shrink down to 1 line too.
Private Function CheckIfSeatIsAlreadyReserved() As Boolean
Return m_seatMngr.IsReserved(lstResults.SelectedIndex) AndAlso _
DialogResult.Yes = MessageBox.Show("The seat is already reserved. Would you like to continue?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
End Function
And, if you do some sort of reservation, array is not really good idea. How about Dictionary(Of String, Boolean)? Your theater or airplane has bunch of seats and you pre-load your dictionary with those
d.Add("1A", False)
d.Add("1B", False)
d.Add("1C", False)
d.Add("1D", False)
d.Add("1E", False)
d.Add("1F", False)
This is your whole airplane row. Now you can do much more with it

Using Variables as module/array names

My ultimate goal is to load a comboBox with elements from different arrays (vba coded). I have 8 different arrays and 6 option buttons. The first 2 optionButtons are grouped to eliminate 4 arrays which leaves the last 4 grouped optionButtons to determine the actual input for the combo box.
It works like this:
'first grouped option buttons
maleOptionButton
femaleOptionButton
'second grouped option buttons
basketballOptionButton
footballOptionButton
soccerOptionButton
hockeyOptionButton
The array's as you can guess are filled with student names that play the sports. So when the user clicks the first grouped buttons of Male/Female the click even does nothing.. however when they click the second group of option buttons of sports, it calls the same sub procedure within a module that has IF's and Else Ifs to determine the combination of buttons that were selected.
sub maleInitArray()
'declaration section:
Public maleSoccerArray(1 to 6) as string
'sub section
dim mike as student 'calls the class student
set mike = new student
with mike
.name = "Michael"
.age = 14
.so on and so on = something
end with
maleSoccerArray(1) = mike.name
End Sub
What I'm trying to do is this:
dim i as integer
dim l as integer
dim gender as string
dim sport as string
if inputForm.soccerOptionButton.value = true then
if inputForm.maleOptionButton.value = true then
gender = "male"
sport = "maleSoccerArray"
call male.maleInitArray ' inits the array thats hard coded.
else
gender = "female"
sport = "femaleSoccerArray"
call female.femaleInitArray
Else If...
' the list goes on to assign variables depending on the combo boxes.
' doesn't work, but it beats using this every time
l = UBound(sport) ' Doesn't recognize "sport" as an Array
for i = 1 to l
' .AddItem(gender.sport(i)) will not work as well.
inputForm.studentComboBox.AddItem(gender.sport(i))
next i
Seems as though UBound(variable) and AddItem(Variable.Variable)
will not work...
I found a few things so far, but none of them have worked.. such as the application.run method and assigning the actual "male.maleSoccerArray" method.
Any help would be greatly appreciated.. thanks

How to perform a count using LINQ?

I am using SQL server and in my program I am currently using a count like this:
Function NumberOfApplications(inCandidateID As Integer) As Integer
Dim iCount As Integer = 0
Dim oApplication As DataModels.Application
For Each oApplication In Me.Applications
If oApplication.CandidateID = inCandidateID Then
iCount = iCount + 1
End If
Next
Return iCount
End Function
I have created my entities and bought in the correct Imports but I need to know how to perform a count using LINQ and VB. As you can see above I am using the Candidate ID to count. Any help would be appreciated. Thank you.
As #Pleun mentions, you can use the Count method, but the method itself has a predicate option, so you can use:
return Me.Applications.Count(app => app.CandidateID == inCandidateID);
In C#. I always have to think hard what that would look like in VB, this is what my brain tells me. The compiler migt disagree though ;):
Return Me.Applications.Count(Function(app) app.CandidateID = inCandidateID)
If you prefer the sql-like syntax, you could also do this:
Function NumberOfApplications(inCandidateID As Integer) As Integer
Return (From app As DataModels.Application In Me.Applications
Where app.CandidateID = inCandidateID).Count
End Function

Remove duplicate items from processes array using VB .net

I have to code to get all accessible process, but I need to remove duplicated items on this array and show only one time each process.
How is the best method to do this, because I think processes array is not like a normal array.
My code:
For Each p As Process In Process.GetProcesses
Try
'MsgBox(p.ProcessName + " " + p.StartTime.ToString)
Catch ex As Exception
'Do nothing
End Try
Next
Thanks in advance
The Process.GetProcesses() method returns an array. You can use the Distinct method, providing an IEqualityComparer to it.
An example would be as comparer:
Public Class ProcessComparer
Implements IEqualityComparer(Of Process)
Public Function Equals1(p1 As Process, p2 As Process) As Boolean Implements IEqualityComparer(Of Process).Equals
' Check whether the compared objects reference the same data.
If p1 Is p2 Then Return True
'Check whether any of the compared objects is null.
If p1 Is Nothing OrElse p2 Is Nothing Then Return False
' Check whether the Process' names are equal.
Return (p1.ProcessName = p2.ProcessName)
End Function
Public Function GetHashCode1(process As Process) As Integer Implements IEqualityComparer(Of Process).GetHashCode
' Check whether the object is null.
If process Is Nothing Then Return 0
' Get hash code for the Name field if it is not null.
Return process.ProcessName.GetHashCode()
End Function
End Class
And you can use it like this:
Sub Main()
Dim processes As Process() = Process.GetProcesses()
Console.WriteLine(String.Format("Count before Distinct = {0}", processes.Length))
' Should contain less items
Dim disProcesses As Process() = processes.Distinct(New ProcessComparer()).ToArray()
Console.WriteLine(String.Format("Count after Distinct = {0}", disProcesses.Length))
Console.ReadLine()
End Sub
You probably have to refine the Comparer to your specifications and for the situation you are going to use it.

Searching an Array in Word (Visual Basic for Applications)

I have set up three arrays in Word. The arrays are as follows:
tacData = Array("32064600", "33001000", "33001100", "33002400", "33003000", "33002400", "35011001", "35013200", "35124100", "35124100")
makeData = Array("Alcatel", "Alcatel", "Sagem", "Samsung", "AEG Mobile", "Samsung", "Nokia", "Maxon", "Siemes", "Nokia")
modelData = Array("One Touch EasyHD", "91009109MB2", "RC410G14", "SGH-200", "Sharp TQ6", "SGH-5300", "DCT-3", "MX6832", "MC399 Cellular Termnial", "DCT-4")
I have made a user form which get a TAC (Value) from the User. Can I get this value and search for it in the first array and get its ID so that I can get the make and model from the other two arrays? I have tried using some code samples that I found but they do not seem to work with Word throwing errors. They were things like Application.Match.
On a side note, would there be a better way to store this information rather than three arrays?
I think you need a collection object. Look at the code below
Dim tacData() As Variant, makeData() As Variant, modelData() As Variant
tacData = Array("32064600", "33001000", "33001100", "33002400", "33003000", "33002401", "35011001", "35013200", "35124100", "35124101")
makeData = Array("Alcatel", "Alcatel", "Sagem", "Samsung", "AEG Mobile", "Samsung", "Nokia", "Maxon", "Siemes", "Nokia")
modelData = Array("One Touch EasyHD", "91009109MB2", "RC410G14", "SGH-200", "Sharp TQ6", "SGH-5300", "DCT-3", "MX6832", "MC399 Cellular Termnial", "DCT-4")
Dim i As Integer, N As Integer
N = UBound(tacData, 1) 'Get the data size
Dim item As Phone 'Temp variable for creating elements into a collection
Dim list As New VBA.Collection 'Define a new collection
For i = 1 To N
Set item = New Phone 'Assign a new Phone object
With item
.TAC = tacData(i) 'Assign values to the phone
.Make = makeData(i)
.Model = modelData(i)
End With
list.Add item, item.TAC 'Add the phone to the list with a KEY
Set item = Nothing
Next i
Dim TAC As String
TAC = "33002400" 'get user input
Set item = list(TAC) '** lookup Phone using Key **
If Not item Is Nothing Then ' Show results
Debug.Print item.TAC, item.Make, item.Model
Else
Debug.Print "Not Found"
End If
It works with a new class called 'Phone' containing
Option Explicit
Public TAC As String
Public Make As String
Public Model As String
and inserted into VBA via this menu item, and renaming in the project tree as "Phone"
The requirement is that the 'TAC' code is unique. In your case it wasn't and I had to change some numbers to make them unique. How have to consider what to do if in real life there are multiple phones with the same TAC code.
PS. Welcome to the world of object oriented programming. This was your first step.

Resources