Displaying Array in One Message Box in VB.NET - arrays

I'm trying to display this separated string in one message box rather than them all popping up separately one after another. Any help would be greatly appreciated!
Dim variables As String
Dim count As Integer
Dim arrResult() As String
variables = "3.09, 1.26, 3.4, 2.098"
arrResult = variables.Split(",")
For count = 0 To arrResult.Length - 1
MsgBox(arrResult(count))
Next

There's two way you could go about it.
FIRST
Before your loop, you could declare a variable say Dim allElements As String = "" then
replace your MsgBox(arrResult(count)) inside your loop to allElements &= arrResult(count) & vbNewLine, finally after your loop MsgBox(allElements)
SECOND
Just remove your loop, then use MsgBox(variables)

Related

Split String into array and insert it into datagridview

I have a string of the form "1|2,3|4,56|7|8|9,10|93". This strin shall be split into an array at the "|". This array is then inserted into a 1-column datagridview.
I wrote this:
Private Function foo(input As String)
If input <> "" Then
Dim StringTable() As String = Split(input, "|")
Dim length As Integer = StringTable.Length
Debug.Print("length " & length)
For i = 0 To length - 1
Debug.Print(StringTable(i))
dg_ctdi.Rows.Add()
dg_ctdi.Rows(i).Cells(0).Value = StringTable(i)
Next
End If
End Function
Problem is, that "length" is now always 1, no matter how many elemenets the string has. Thus my datagridview has only one row. What am I missing?
Thanks!
Actually, the piece of code did the job correctly. Unfortunately I had some issues with the database-fild, where the input was previously stored. So feel free to use this code and have a look at the comments above. Thanks!

Getting the first 6 strings in a string array in VB

I have a String array which gets all the files from a directory. Dim files() as string = IO.Directory.GetFiles(xxx)
These files are being added as nodes on a TreeView. The issues I am having is, when there are 300 files in the array. I loop through and get each file and add all 300 to the Treeview. But I want to only get the first 100 files from the array and only add those. I feel like this should be pretty simple but trying to figure out how to get the first 100 strings(files) from the array is slipping my mind. Any help would be greatly appreciated.
Opt 1) Use take()
Dim files() as string = IO.Directory.GetFiles("C:\Temp").Take(6)
Opt 2) Use a for loop
Dim files() as string = IO.Directory.GetFiles("C:\Temp")
For i as Integer = 0 to 5
TreeView1.Nodes.Add(files(i))
Next

File is located from another project, but won't read or load into arrays

I'm trying to use a file from one project in a new project. The code has no errors in debugging, but skips to my programmed error "Problem reading data from file." Any attempt to run the program anyway results in a calculation of 0 - because the arrays don't seem to be populating.
Thanks for the help.
'opens the text file, reads it, stores the value in two arrays
'closes the file
Dim srFileName As String 'Name the file to be opened
Dim intNumItems As Integer 'number of records on file
Dim i As Integer 'loop counter for the array
Dim intLastBlank As Integer
Dim strInput As String
srFileName = "C:\StudentMarks.txt"
If File.Exists(srFileName) Then
Dim srReader As New StreamReader(srFileName)
Try
intNumItems = srReader.ReadLine()
strInput = srReader.ReadLine()
Do While srReader.Peek >= 0
Loop
strInput = srReader.ReadLine()
While Not srReader.EndOfStream
'each loop iteration, reads 2 lines and places in proper array
For i = 0 To intNumItems - 1
'find location of tab between student name and mark
intLastBlank = strInput.IndexOf(vbTab)
'take characters before tab and place in name array
arrName(i) = strInput.Substring(intLastBlank + 1)
'take characters after tab, convert to double, and
'place values in mark array
arrMark(i) = Double.Parse(strInput.Substring(intLastBlank + 1))
Next
End While
Catch ex As Exception
MessageBox.Show("Problem reading data from file")
End Try
You really should make your MessageBox display the actual error message too. It will help you troubleshooting the problem.
Example:
MessageBox.Show("Problem reading data from file:" & Environment.NewLine & ex.Message)
Anyhow, as for your problem you have created an infinite loop here:
Do While srReader.Peek >= 0
'You have no code here, so you've created yourself an infinite loop.
Loop
strInput = srReader.ReadLine()
You probably misplaced the Loop keyword. I'm guessing it's supposed to be further down the code?
EDIT:
I also don't understand the point of using both:
Do While srReader.Peek >= 0
...and:
While Not srReader.EndOfStream
You only need to use one of the statements, as both of them checks wether there is more for the StreamReader to read.
Also make sure you put the .ReadLine() methods within the loop. :)

Excel VBA: What is the Maximum Number of String Elements that can be Stored in an Array

I am trying to build an add-in which would offer the user several more functions.
The basis of these functions is to search through a reasonably large data set of 1424 string elements and return the array element position(s) of each identical match.
I wish to store the data set within the VBA code as it will never change and I do not wish to store it within a spreadsheet. Upon building the array I am faced with a problem whereby on the addition of the 1278th element, the entire array disappears.
Up until the 1277th element the array works and can be interrogated. As soon as I add one more element the entire array disappears! From what I can tell I am nowhere near to the maximum number of array elements allowed in VBA so this is failing due to some other problem.
Here is a snippet of my code:
Function matsearch(ByVal MatSpec As Variant) As String
Dim i As Integer, InstanceCount As Integer, LastInstance As Integer, FirstInstance As Integer
Dim Table_1ASpecList() As String
InstanceCount = 0
LastInstance = 1
Table_1ASpecList = Array("Spec_No", "SA/AS 1548", "SA/AS 1548", "SA/CSA-G40.21", "SA/EN 10028-2", "SA/EN 10028-3", "SA/EN 10028-7", "*****Many More Elements*****")
For i = 0 To UBound(Table_1ASpecList)
If MatSpec = Table_1ASpecList(i) Then
InstanceCount = InstanceCount + 1
LastInstance = i + 1
End If
Next i
FirstInstance = LastInstance - InstanceCount
matsearch = FirstInstance & " " & InstanceCount
Any ideas as to why this is not working?????
In the Visual Basic Editor, change the IsAddIn property of ThisWorkbook to False. Activate the workbook and enter the values to be populated in Table_1ASpecList into a sheet. Give the range a name, say "MyValues". Change the IsAddIn property back to True, and then amend your code to:
Dim Table_1ASpecList() As Variant
Table_1ASpecList = Sheet1.Range("MyValues").Value

How can I compare 2 arrays creating a new array with the changes?

I am reading the CHAT from an application with readprocessmemory which I want to monitor for events.
Attach() 'attach to process
Dim chatStart As Integer = &H6E3220
Dim lines As Integer = 150 'chatlines
Dim offset As Integer = 98 'offset between chat lines
Dim arrAddressList(0 To lines) As IntPtr
Dim addressToAdd As IntPtr = chatStart 'first line
arrAddressList(0) = addressToAdd 'assign firstline
For i = 1 To lines 'loop the other 149 lines and assign the address
chatStart += offset 'skip to the address of the next line
arrAddressList(i) = chatStart 'assign the new address to its place in the array
Next
For j = 0 To arrAddressList.Count - 1
Dim chatText As String = MEMMGR.ReadAsciiString(arrAddressList(j), 104)
If chatText = "" Then 'chat lines not filled, exit
Exit For
Else
AddLog(chatText) 'puts into text box
End If
Next
If the application has just started, and all the lines are not filled, then there is no need to cycle all 150 lines.
The chat box is max 150 lines, when it reaches the 150, all the chat is pushed up so the 150th line is now the latest line of chat text.
My current code will read all the chat in the chat box.
The only way I can think of to detect a change is to read it ALL twice, and then compare the results and loop until it finds lines in the arrays which are different - these would be the lines which are 'updated'. There could be more than one line that is updated depending on how active the chat is so would need to have another array containing ALL the line changes from the last chat retrieval.
However I've no idea where to start when I come to do it as it seems very confusing, Could someone with a bit more logic in this matter point me in the right direction, or explain the best (fastest) way to do this?
Thanks a lot for taking the time to read this, I hope it wasn't too confusing.
If you chat lines are unique, you could use LINQ:
Dim chat1() As String = {"abc", "bcd", "cde"}
Dim chat2() As String = {"abc", "bcd", "cde", "def"}
Dim diff() As String = chat2.Except(chat1).ToArray
Ideally you don't need to read all lines, just go from bottom to top.
Stop when found a record you already have in your dictionary.

Resources