Correspond first line of arrays with second line VB - arrays

I have a file that shows A song name on the first line, then the genre on the second, and the time on the third.
Example
All You Need is Love-Beatles
Rock
4.25
This goes on. I need some way of corresponding the songs with the genres and then display them in a list box called lstPlayList using Arrays
I have
strSongs(intCount) = objReader.ReadLine()
strGenre(intCount) = objReader.ReadLine()
dblLength(intCount) = Convert.ToDouble(objReader.ReadLine())
For storing every variable but I can't append songs to genres

Instead of having three separate arrays for one item, you could indeed as other commenters have said, create a database, but if you don't want to go that far try this.
Create a structure at the top of your Form's class definition list this :-
Structure Song
Dim Name As String
Dim Genre As String
Dim Duration As Single
End Structure
This effectively creates a new Datatype.
Next create a list of this datatype like this :-
Dim Songs As New List(Of Song)
When you read your data from the file, instead of what you've included in your question, you can add the data to the list like this :-
Dim newSong As Song
newSong.Name = objReader.ReadLine()
newSong.Genre = objReader.ReadLine()
newSong.Duration = Convert.ToDouble(objReader.ReadLine())
Now that you have one list of all the data, you can search the list and return results as a new list of all the results. This is the function that does the searching and returning of a list :-
Private Function SearchSongsByGenre(searchGenre As String) As List(Of Song)
Dim returnList As New List(Of Song)
For Each searchSong As Song In Songs
If searchSong.Genre = searchGenre Then
returnList.Add(searchSong)
End If
Next
Return returnList
End Function
To use it in another sub, just create a list in the sub called.. say.. SearchResults :-
Dim SearchResults As New List(of Song)
Searchresults = SearchSongsByGenre("Rock")
Now you have a list of all the Songs that have the genre Rock
A lot safer than searching an array and getting data from 3 arrays that could become desynchronised.

Related

How do I sort an array of different times?

So I have an array of various timeslots, but I want to sort out the times from the earliest ones to the latest ones, however, I also want to delete any elements within the array that has the time value of 12:00am.
Appointment(0) = #10:45:00 AM#
Appointment(1) = #12:00:00 AM# 'My actual has up 80 elements of different timeslots but I'm using 3 elements as an example
Appointment(2) = #12:00:00 AM#
Is there anyone who can help me with this problem?
Using a string array is not a good idea if that's what you used just use a list of DateTime and the Sort method.
Dim list As List(of DateTime) = new List(of DateTime)
For Each val As String In Appointment
list.Add(val.replace(" ",""))
Next
list.Sort(New Comparison(Of Date)(Function(x As Date, y As Date) y.CompareTo(x)))
Nothing in .Net for removing specific items from array, but it can be "simplified" a bit with LINQ:
Dim Appointment = {#10:45#, #12AM#, #12AM#}
Appointment = (From a In Appointment Where a <> #12AM# Order By a).ToArray
or
Appointment = Appointment.Except({#12AM#}).ToArray
Array.Sort(Appointment)
A bit more efficient alternative can be to use generic collection like List or SortedSet instead:
Dim list = New List(Of Date) From {#10:45#, #12AM#} ' or Dim list = Appointment.ToList
list.Add(#12AM#)
list.RemoveAll(Function(a) a = #12AM#)
list.Sort()

How do i pass values from view model to controller without use of list?

Continuing from my previous question, i pass the id value from 'Lookup' controller to another controller method 'Charges'.
Function Charges(id As String) As ActionResult
Dim Patient As New LookupVM()
Patient.GetHistory(paid)
Return View(Patient)
End Function
In the view model, i have the GetHistory(id) list method, and listProcedure is the list holding the values that i want to pass back to the controller to be displayed in the view.
Public Property listProcedure As List(Of LookupVM)
Public Function GetHistory(id As String) As List(Of LookupVM)
Using db As New DbContext()
' SQL query
'====================
Dim idd = New SqlParameter("#id", "%" & id & "%")
Dim query As String = "select p.id id, pv.pvid pvid
from patient p
join patient_visit pv on p.id = pv.id
where p.paid like #id"
Dim Results = db.Database.SqlQuery(Of LookupVM)(query, idd).ToList()
For Each item In Results
Dim pvid = New SqlParameter("#pvid", "%" & item.pvid & "%")
Dim query2 As String = "select po.remarks remarks, po.entereddt entereddt, po.Consultant Consultant
from procedure_order po
join procedures p on po.pdid = p.pdid
where po.pvid like #pvid
order by po.entereddt desc"
Dim Results2 = db.Database.SqlQuery(Of LookupVM)(query2, pvid).ToList()
For Each item2 In Results2
Dim ProcedureList2 As New LookupVM()
ProcedureList2.remarks = item2.remarks
ProcedureList2.entereddt = item2.entereddt
ProcedureList2.ForConsultant = item2.ForConsultant
listProcedure.Add(ProcedureList2)
Next
Next
Return listProcedure
End Using
End Function
I'm wondering if i'm able to pass those values to the controller through something else other than a list. if possible, what could it be? Because if its a list, i can't display them on the page without the use of foreach loop.
Another question is, is my code conceptually correct or wrong? Because when i run, it took me 5 minutes to display a list of 72 values which involves the code above. I think it took way too long. Is there any way to make it load faster?
About the List, there is nothing wrong with doing For Each in a View. If it is needed then it is needed, and no reason to avoid it.
Some query tips (it is hard to do better for lack of details):
If your paid and pvid columns are INT, then do not query them using LIKE #id but using = #id (without % in it). Even if they are (N)VARCHAR then using LIKE would need to be justified and explicit. If done by accident then output will often be wrong.
LIKE '%value%' causes indexes to be ignored, possibly leading to bad performance.
Make sure that you have indexes on the columns that you use to JOIN on.
Try combining the two queries into one, to limit the number of .NET/DB roundtrips.

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

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.

How to write content in a text file at the end of each line

I am having one text file which contain following kind of data.
"3P","Smith","Richard","3 Point Promotions","3P","richs51#gmail.com","IDA","Yes",,,,0,4,5,83.33,10,
"A1","Ernest","Amy","TAKE 1 Promotional Products","LCOOK","lcpromos#adelphia.net","IDA","Yes",,,,0,6,7,,0,
"A2","Derek","Eaton","Advertising Edge Promotions","AE","dereke#adedgepro.com","IDA","Yes",,,,0,8,8,,10,
"AAA","Abercrombie","Jerry","AAA Specialty Wholesale Inc","AAA","wabercro#bellsouth.net","IDA","Yes",,,,0,9,9,,10,
"AAP","Halberstam","Mendy","All About Promotions","AAP","mendyaap#yahoo.com","IDA","Yes",,,,0,10,10,,12,
Each of them are separate line.Now i want add another column in each like this
"3P","Smith","Richard","3 Point Promotions","3P","richs51#gmail.com","IDA","Yes",,,,0,4,5,83.33,10,**96**
"A1","Ernest","Amy","TAKE 1 Promotional Products","LCOOK","lcpromos#adelphia.net","IDA","Yes",,,,0,6,7,,0,**97**
"A2","Derek","Eaton","Advertising Edge Promotions","AE","dereke#adedgepro.com","IDA","Yes",,,,0,8,8,,10,**98**
"AAA","Abercrombie","Jerry","AAA Specialty Wholesale Inc","AAA","wabercro#bellsouth.net","IDA","Yes",,,,0,9,9,,10,**99**
"AAP","Halberstam","Mendy","All About Promotions","AAP","mendyaap#yahoo.com","IDA","Yes",,,,0,10,10,,12,**100**
How i read content in line by line.And also how to write another value in same text file at each line.Please send solution for this problem.I am waiting for your reply.Thanks for reply.
-Saravanan
Maybe something like this:
Dim lines() As String
Using reader As New IO.StreamReader(filePath)
Dim sep() As String = {vbNewLine}
lines = reader.ReadToEnd().Split(sep, StringSplitOptions.RemoveEmptyEntries)
End Using
Dim Index as Int32 = 1
Using writer As New IO.StreamWriter(filePath, False)
For Each line As String In lines
writer.WriteLine(line & "," & Index.ToString())
Next
End Using

Resources