How to write to textfile in For loop, vb.net - for-loop

I have 3 string variables already declared and with values set. I want to write these 3 strings to a text file in a For loop, one string for each pass of the loop. I can't figure out how to get vb.net to understand that I want to add an integer counter to a string, and for it to understand that is not a literal string, but one of the 3 string variables that are already declared and have values.
sample code --->
Dim writer As New StreamWriter("c:\temp\test_out.txt")
Dim datastr1 As String
Dim datastr2 As String
Dim datastr3 As String
datastr1 = "test string 1"
datastr2 = "test string 2"
datastr3 = "test string 3"
Dim i As Integer
For i = 1 To 3
writer.WriteLine("datastr" & i)
Next i
The result is:
datastr1
datastr2
datastr3
But I want the result to be:
test string 1
test string 2
test string 3
Thank you for helping me.

Dim datastr() As String = {"test string 1",
"test string 2",
"test string 3"}
' Loop over each element with For Each.
For Each str As String In datastr
writer.WriteLine(str)
Next

Related

How to pass an array with fixed length strings to a function

I am trying to pass an array of fixed length strings to a function. However, the function will not know the size of the strings. The parameter should take any size strings. How would I go about this? Here is my code:
Public Type myRecord
names(15) As String * 250
End Type
Function def:
Function HasQuotes(textArr() As String) As String
End Function
Usage:
HasQuotes(rowRec.names)
where rowRec is a MyRecord.
When I run this code I get the following error Type Mismatch: array or user defined type expected. This is because the parameter is not defined as textArr() As String * 250. How can I make the parameter accept any string length?
You Have to follow Proper syntax you can Try Following updated Code
Private Type myRecord
names(15) As String * 250
End Type
Private Sub Command1_Click()
Dim rowRec As myRecord
rowRec.names(0) = ("Vaibhav")
Dim v As String
v = HasQuotes(rowRec.names)
End Sub
Function HasQuotes(ByRef textArr() As String * 250) As String
MsgBox textArr(0)
End Function
You are going to have trouble passing an array of fixed-length strings of any length. One simple solution is to convert them to an array of variable-length strings:
Private Sub Test()
Dim rowRec As myRecord
Dim names(15) As String
Dim i As Integer
Dim s As String
rowRec.names(0) = "Brian"
rowRec.names(15) = "Stafford"
'move the data to an array of variable-length strings
For i = 0 To 15
names(i) = rowRec.names(i)
Next
s = HasQuotes(names)
'move the data back to the original array if needed
For i = 0 To 15
rowRec.names(i) = names(i)
Next
End Sub

How to collect a certain string anywhere in an item in a ListBox?

I have been trying to find out how to collect a string anywhere in a Listbox, I use Visual Basic 2010 and this is more of an request, but there is code I found so you fix the code I found or tell me me an another code to use.
I have tried using ListBoxName.Items.Contains but that did not work, I tried a lot of methods and it would be hard to say all of then at once.
' Split string based on space
Dim textsrtring As String = ListBox.Text
Dim words As String() = textsrtring.Split(New Char() {" "c})
Dim found As Boolean = False
' Use For Each loop over words
Dim word As String
For Each word In words
If ListBox.Items.Contains(word) Then
found = True
Exit For
End If
Next
MessageBox.Show(found)
They were no errors, the message box that appeared kept on telling me false and there is no string when I clearly put it in, no error messages.
You'll need an inner loop to see if each word is contained in your main listbox entry using String.Contains():
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim searchFor As String = TextBox1.Text.Trim
If searchFor.Length > 0 Then
Dim words As String() = searchFor.Split(New Char() {" "c})
Dim found As Boolean = False
Dim foundAt As Integer
' Use For Each loop over words
Dim word As String
For Each word In words
For i As Integer = 0 To ListBox.Items.Count - 1
If ListBox.Items(i).ToString.Contains(word) Then
found = True
foundAt = i
Exit For
End If
Next
If found Then
Exit For
End If
Next
If found Then
ListBox.SelectedIndex = foundAt
Label1.Text = "Search string found."
Else
ListBox.SelectedIndex = -1
Label1.Text = "Search string NOT found."
End If
Else
ListBox.SelectedIndex = -1
Label1.Text = "No search string."
End If
End Sub

I'm trying to pass a parameter using this

Option Explicit
Public Sub Main(): If Con.Initialize = conLaunchExplorer Then Con.Visible = False
Dim strParameters As String
Dim readL As String
strParameters = Split(Command$, "/")
Con.WriteLine strParamers
readL = Con.ReadLine
End Sub
This is code right now as you can see I'm trying to split the strParameters but the error says mismatch please help me.
When Splitting a string, the data is placed in an array, and according to your declaration strParameters is a simple string. How would the different parts fit in one string? You just need to change your declaration as follows;
Dim strParameters() As String
Assuming that your Command$ is correctly declared as string, with delimiters "/"

Syntax error when passing an array to a method

I have created and array which will store the value of the inputbox, it is saying i have a syntax error and i am unsure how to fix it.
I have used parameter passing which i will display below also
Dim name() As String
For counter = 1 To 5
Call enter_questionnaire_data(name()) '2.0
Next
End sub
2nd sub-routine
Private Sub enter_questionnaire_data(ByRef name())
name() = InputBox("Enter the party name")
why do you have name as a String array?
you just need to declare
Dim name As String
for allowing name to store a String
also you cannot assign values to array members like this
name() = InputBox("Enter the party name")
you need to specify the index also
EDIT:
if you want string array to store the names, then
declare a static array of sufficient length
Dim name(10) As String
and use:
name(index) = InputBox("Enter the party name")
index = index+1;
where the index is incremented after every input till 10
(using dynamic array would be a bit complicated for you right now , so i am omitting dynamic array from discussion)
Use name without brackets
Dim name As String
and in other method
Private Sub enter_questionnaire_data(ByRef name)
name = InputBox("Enter the party name")
About your program:
Dim name As String <---Without () you can use this for array
For counter = 1 To 5
Call enter_questionnaire_data(name as string)<--- can you insert variable/tipe
Next
End sub
Private Sub enter_questionnaire_data(name as string)
name = InputBox("Enter the party name")

vb6 split space delimited string at every nth space

I have a space delimited string variable. I would like to store the contents of the variable into an array. Using split, I can store every space delimited value in an array. I would prefer if I could separate the string variable at every 7th space. For example, the text could read:
"hello hello hello hello hello hello hello hi hi hi hi hi hi hi hey hey hey hey hey hey hey"
This isn't the actual content of the string, but a simpler version that is easier to read. I want to separate at the places where the words change, or every 7th space. Any help would be greatly appreciated. My current code looks like this, which splits at every space.
ReDim StatsArray(ArrInc)
StatsArray = Split(Stats)
For i = 0 To UBound(StatsArray())
If i > UBound(StatsArray()) Then
ReDim Preserve StatsArray(i + ArrInc)
End If
' MsgBox StatsArray(i) ' When not commented out, this help me check the array
Next
There isn't any built-in function that will do this for you. A couple of solutions come to mind: (1) Do your split, then iterate your array. Concatenate seven array elements in a string variable. Write the result to a new array. Rinse and repeat. (2) Create an Array. Iterate through the string character by character, pushing each character into a variable and keeping track of the spaces you encounter; when you reach the seventh space add an element to your array, copy the variable to it, and clear the variable. Rinse and repeat.
The first one strikes me as a bit faster, but I could be quite wrong about that.
If I understand what you're trying to do, a string of "1234 12345678 123" would get split into 1234, 1234567, 8, 123. Is this correct?
If so, then you can use Regular Expressions to do the split for you.
Function Split7(S As String)
Dim regex As New RegExp
regex.Pattern = "[^ ]{7}"
regex.Global = True
Split7 = Split(regex.Replace(S, "$& "), " ")
End Function
This will insert a space after every 7th character that is not a space. Then use the split function to get the whole thing into an array.
Another stab at it, though I'd probably optimize the ReDim Preserves, doing them in chunks:
Option Explicit
Private Function SplitN( _
ByRef Source As String, _
ByVal Delim As String, _
ByVal N As Long) As String()
'Delim can be multi-character.
'Always returns at least 1-element result,
'even if Source = "".
Dim SearchPos As Long
Dim PartPos As Long
Dim DelimPos As Long
Dim Parts() As String
Dim DelimCount As Long
Dim PartsCount As Long
SearchPos = 1
PartPos = SearchPos
Do
DelimPos = InStr(SearchPos, Source, Delim)
If DelimPos > 0 Then
DelimCount = DelimCount + 1
If DelimCount = N Then
DelimCount = 0
ReDim Preserve Parts(PartsCount)
Parts(PartsCount) = _
Mid$(Source, PartPos, DelimPos - PartPos)
PartsCount = PartsCount + 1
PartPos = DelimPos + Len(Delim)
End If
SearchPos = DelimPos + Len(Delim)
End If
Loop While DelimPos > 0
ReDim Preserve Parts(PartsCount)
Parts(PartsCount) = Mid$(Source, PartPos)
SplitN = Parts
End Function
Private Sub Form_Load()
Dim S As String
Dim Parts() As String
Dim P As Long
S = "hello hello hello hello hello hello hello " _
& "hi hi hi hi hi hi hi " _
& "hey hey hey hey hey hey hey"
Text1.SelStart = 0
Text1.SelText = S & vbNewLine & vbNewLine
Parts = SplitN(S, " ", 7)
Text1.SelText = "Ubound() = " & UBound(Parts) & vbNewLine
For P = 0 To UBound(Parts)
Text1.SelText = Parts(P) & vbNewLine
Next
End Sub

Resources