Variable declaration syntax error in the for next loop - vb.net

Following code is working properly.
Private Sub Button1_Click(sender As Object, e As RoutedEventArgs) Handles Button1.Click
Dim Dictionary1 As New Dictionary(Of String, Integer)
Dim Dictionary2 As New Dictionary(Of String, Integer)
Dim Dictionary3 As New Dictionary(Of String, Integer)
Dim Dictionary4 As New Dictionary(Of String, Integer)
Dim Dictionary5 As New Dictionary(Of String, Integer)
Dictionary1.Add("George", "36")
Dictionary2.Add("George", "36")
Dictionary3.Add("George", "36")
Dictionary4.Add("George", "36")
Dictionary5.Add("George", "36")
End Sub
I have tried to shorten the above code like the below code via for next loop.
But the below code gives me an error.
Private Sub Button2_Click(sender As Object, e As RoutedEventArgs) Handles Button2.Click
Dim Dictionary1 As New Dictionary(Of String, Integer)
Dim Dictionary2 As New Dictionary(Of String, Integer)
Dim Dictionary3 As New Dictionary(Of String, Integer)
Dim Dictionary4 As New Dictionary(Of String, Integer)
Dim Dictionary5 As New Dictionary(Of String, Integer)
For i As Integer = 1 To 5
("Dictionary" & i).Add("George", "36")
Next
End Sub
So, how can I solve the following error?

List of Dictionary
Dim Dictionary1 As New Dictionary(Of String, Integer)
Dim Dictionary2 As New Dictionary(Of String, Integer)
Dim Dictionary3 As New Dictionary(Of String, Integer)
Dim Dictionary4 As New Dictionary(Of String, Integer)
Dim Dictionary5 As New Dictionary(Of String, Integer)
Dim ds As New List(Of Dictionary(Of String, Integer))
ds.Add(Dictionary1)
ds.Add(Dictionary2)
ds.Add(Dictionary3)
ds.Add(Dictionary4)
ds.Add(Dictionary5)
For Each d As Dictionary(Of String, Integer) In ds
d.Add("George", 37)
Next

Related

Load multiple files by File-Type into ListView Control

How I can load files by File-Type?
I only wanna load the video files(.ts) from first the Loop and (.srt) files in the second Loop.
I tried, but I can't succeed in setting the Filter.
Here's the code by jmcilhinney
Private Sub tbnAddFiles_Click(sender As Object, e As EventArgs) Handles tbnAddFiles.Click
Dim vPaths As New List(Of String)
Dim vNames As New List(Of String)
Dim sPaths As New List(Of String)
Dim sNames As New List(Of String)
Dim x As New OpenFileDialog
x.Multiselect = True
x.Filter = "TS and SRT File|*.ts;*.srt"
x.RestoreDirectory = True
If x.ShowDialog = DialogResult.OK Then
'FOR VIDEO FILES (.ts)
For Each f In x.FileNames
vNames.Add(f)
vPaths.Add(Path.GetFileName(f))
Next
'FOR SRT FILES (.srt)
For Each f2 In x.FileNames
sNames.Add(f2)
sPaths.Add(Path.GetFileName(f2))
Next
AddToListView(vNames.ToArray, vPaths.ToArray, sNames.ToArray, sPaths.ToArray)
End If
End Sub
Sub AddToListView(vNames As String(), sNames As String(), vPaths As String(), sPaths As String())
LV.Items.Clear()
Dim items As New List(Of ListViewItem)
Dim upperBounds = {vNames.GetUpperBound(0), sNames.GetUpperBound(0), vPaths.GetUpperBound(0), sPaths.GetUpperBound(0)}
For i = 0 To upperBounds.Min
items.Add(New ListViewItem({vNames(i), sNames(i), vPaths(i), sPaths(i)}))
Next
LV.BeginUpdate()
LV.Items.AddRange(items.ToArray())
LV.EndUpdate()
End Sub
Filter the list using LINQ:
For Each f In x.FileNames.Where(Function(s) Path.GetExtension(s) = ".ts")
vNames.Add(f)
vPaths.Add(Path.GetFileName(f))
Next
'FOR SRT FILES (.srt)
For Each f In x.FileNames.Where(Function(s) Path.GetExtension(s) = ".srt")
sNames.Add(f)
sPaths.Add(Path.GetFileName(f))
Next
Here's the fixed and updated codes.
Big thanks to #jmcilhinney
Private Sub btnAddFiles_Click(sender As Object, e As EventArgs) Handles btnAddFiles.Click
Using ofd As New OpenFileDialog
With ofd
.Filter = ("video and srt files (*.ts;*.srt)|*.ts;*.srt")
.Multiselect = True
.RestoreDirectory = True
End With
If ofd.ShowDialog = DialogResult.OK Then
Dim vPaths As New List(Of String)
Dim sPaths As New List(Of String)
Dim vNames As New List(Of String)
Dim sNames As New List(Of String)
Dim oNames As New List(Of String)
txtinputFolder.Text = Path.GetDirectoryName(ofd.FileName)
For Each vid In ofd.FileNames.Where(Function(s) Path.GetExtension(s) = ".ts")
vNames.Add(vid) 'input paths
vPaths.Add(Path.GetFileName(vid)) 'intput filenames
oNames.Add(Path.GetFileNameWithoutExtension(vid) & ".mkv") 'output file name
Next
For Each srt In ofd.FileNames.Where(Function(s) Path.GetExtension(s) = ".srt")
sNames.Add(srt) 'input paths
sPaths.Add(Path.GetFileName(srt)) 'input filenames
Next
AddToListView(vPaths.ToArray, sPaths.ToArray, oNames.ToArray, vNames.ToArray, sNames.ToArray)
End If
End Using
End Sub
Sub AddToListView(vPaths As String(), sPaths As String(), vNames As String(), sNames As String(), oNames As String())
LV.Items.Clear()
Dim items As New List(Of ListViewItem)
Dim upperBounds = {vPaths.GetUpperBound(0), sPaths.GetUpperBound(0), vNames.GetUpperBound(0), sNames.GetUpperBound(0), oNames.GetUpperBound(0)}
For i = 0 To upperBounds.Min
items.Add(New ListViewItem({"Queued", vPaths(i), sPaths(i), vNames(i), sNames(i), oNames(i)}))
Next
LV.BeginUpdate()
LV.Items.AddRange(items.ToArray())
LV.EndUpdate()
updateParameter()
End Sub

Call a Sub in a Textbox VB.Net

Get all combinations which declares an established amount sum
I managed to convert the code written to C #, and I would need some help. I don't know how to let them display the appropriate values in my Textbox.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' Call (Main) - How to call in a Textbox1.Text
End Sub
Code:
Class SurroundingClass
Private Sub Main()
Dim numbers As Integer() = {3, 9, 8, 4, 5, 7, 10}
Dim target As Integer = 15
sum_up(New List(Of Integer)(numbers.ToList()), target)
End Sub
Private Shared Sub sum_up_recursive(ByVal numbers As List(Of Integer), ByVal target As Integer, ByVal part As List(Of Integer))
Dim s As Integer = 0
For Each x As Integer In part
s += x
Next
If s = target Then
Console.WriteLine("sum(" & String.Join(",", part.[Select](Function(n) n.ToString()).ToArray()) & ")=" + target)
End If
If s >= target Then
Return
End If
For i As Integer = 0 To numbers.Count - 1
Dim remaining = New List(Of Integer)()
Dim n As Integer = numbers(i)
For j As Integer = i + 1 To numbers.Count - 1
remaining.Add(numbers(j))
Next
Dim part_rec = New List(Of Integer)(part)
part_rec.Add(n)
sum_up_recursive(remaining, target, part_rec)
Next
End Sub
Private Shared Sub sum_up(ByVal numbers As List(Of Integer), ByVal target As Integer)
sum_up_recursive(numbers, target, New List(Of Integer)())
End Sub
End Class
When you run the code you will see that it outputs the sets of results one set at a time in the line Console.WriteLine("sum(" & String.Join(",", part.[Select](Function(n) n.ToString()).ToArray()) & ")=" + target).
As you want to get the result sets in one go, you will need to accumulate them at that point instead of writing to the console, and the Sub will need to be a Function with another parameter for the accumulator.
Making those modifications:
Public Class Form1
' Derived from the code at https://stackoverflow.com/questions/4632322/finding-all-possible-combinations-of-numbers-to-reach-a-given-sum
Function SumUpRecursive(numbers As List(Of Integer), target As Integer, part As List(Of Integer), solutions As List(Of List(Of Integer))) As List(Of List(Of Integer))
Dim s = part.Sum()
If s = target Then
' MsgBox("sum(" & String.Join(",", part.[Select](Function(n) n.ToString()).ToArray()) & ")=" & target)
solutions.Add(part)
End If
If s >= target Then
Return Nothing
End If
For i As Integer = 0 To numbers.Count - 1
Dim remaining = New List(Of Integer)()
Dim n As Integer = numbers(i)
For j As Integer = i + 1 To numbers.Count - 1
remaining.Add(numbers(j))
Next
Dim part_rec = New List(Of Integer)(part)
part_rec.Add(n)
SumUpRecursive(remaining, target, part_rec, solutions)
Next
Return solutions
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim numbers As Integer() = {3, 9, 8, 4, 5, 7, 10}
Dim target As Integer = 15
Dim nums = SumUpRecursive((numbers.ToList()), target, New List(Of Integer), New List(Of List(Of Integer)))
If nums Is Nothing Then
MsgBox("Failure :(")
Else
MsgBox(String.Join(vbCrLf, nums.Select(Function(b) String.Join(", ", b))))
' TextBox1.Lines = nums.Select(Function(b) String.Join(", ", b)).ToArray()
End If
End Sub
End Class
I'm sure there's a neater way of writing it.

Issue getting array from dictionary

I am having a little issue with the function Z to copy the data from dictionary into an array.. how can I fix this since dicValues is dimensional?
Dim dicValues(24) As Dictionary(Of String, Long)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Index = 1 To 24
dicValues(Index) = New Dictionary(Of String, Long)
Next
End Sub
issue here:
Dim arr As Array = dicValues.[Select](Function(z) z.Value).ToArray()
use dicValues(1) like this
Dim arr As Array = dicValues(1).[Select](Function(z) z.Value).ToArray()

VB.net Multiple StreamReader and FileStream in a single button

I want to create multiple of these readers but my program only reads the first filestream is there a way for it to read them all? or do i have to place them in different buttons? here is my current code, :
Public aRecp As String()
Public listRecp As New List(Of String)
Public aEmail As String()
Public listEmail As New List(Of String)
Public aName As String()
Public listName As New List(Of String)
Public sArray As String()
Public sList As New List(Of String)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim fStream As New System.IO.FileStream("messages.txt", IO.FileMode.Open)
Dim sReader As New System.IO.StreamReader(fStream)
Dim Index As Integer = 0
Do While sReader.Peek >= 0
sList.Add(sReader.ReadLine)
Loop
sArray = sList.ToArray
fStream.Close()
sReader.Close()
Dim StreamName As New System.IO.FileStream("sendername.txt", IO.FileMode.Open)
Dim ReaderName As New System.IO.StreamReader(StreamName)
Dim IndexName As Integer = 0
Do While ReaderName.Peek >= 0
listName.Add(sReader.ReadLine)
Loop
aName = listName.ToArray
StreamName.Close()
ReaderName.Close()
Dim StreamEmail As New System.IO.FileStream("senderemail.txt", IO.FileMode.Open)
Dim ReaderEmail As New System.IO.StreamReader(StreamEmail)
Dim IndexEmail As Integer = 0
Do While ReaderEmail.Peek >= 0
listEmail.Add(sReader.ReadLine)
Loop
aEmail = listEmail.ToArray
StreamEmail.Close()
ReaderEmail.Close()
Dim StreamRecp As New System.IO.FileStream("recpname.txt", IO.FileMode.Open)
Dim ReaderRecp As New System.IO.StreamReader(StreamRecp)
Dim IndexRecp As Integer = 0
Do While ReaderRecp.Peek >= 0
listRecp.Add(ReaderRecp.ReadLine)
Loop
aRecp = listRecp.ToArray
StreamRecp.Close()
ReaderRecp.Close()
End Sub
Not a direct answer to your question (and there's nothing obvious to me in your posted code as to why it's only executing the first reader), but since you're reading text files it'd be a lot less code to use File.ReadAllLines(fileName), like this:
Public aRecp As String()
Public aEmail As String()
Public aName As String()
Public sArray As String()
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
sArray = File.ReadAllLines("messages.txt")
aName = File.ReadAllLines("sendername.txt")
aEmail = File.ReadAllLines("senderemail.txt")
aRecp = File.ReadAllLines("recpname.txt")
End Sub
File.ReadAllLines(fileName) returns an array that has each line of the text file as an element. Much simpler than creating the stream, peeking your way through and reading each line into a list and then converting it to an array.

How pass more than one values in a Single variable to a Function in vb.net

I am using vb.net
I have a Function in my class(class1) file namely save. This Save function have 3 arguments. Now i am using the following approach to send arguments.
Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
dim p as new class1
p.save(txtempid.text,txtempname.text,txtage.text)
messagebox.show("Successfully Saved")
End sub
Class1:
imports system.data.sqlclient
imports system.configuration
public class class1
Dim constr As String
Dim con As SqlConnection
Dim cmd As SqlCommand
public function save(byval s as string,byval s1 as string,byval s2 as string)
constr = ConfigurationManager.AppSettings("con")
con = New SqlConnection(constr)
con.Open()
cmd=new sqlcommand("Emp_insert",con)
cmd.commandtype=commandtype.storedprocedure
cmd.Parameters.Add("#EmployeeID", SqlDbType.BigInt).Value = s
cmd.Parameters.Add("#EmployeeName", SqlDbType.VarChar).Value = s1
cmd.Parameters.Add("#Age", SqlDbType.BigInt).Value = s2
cmd.executenonquery()
cmd.dispose()
con.close()
return 0
End function
End Class
It's ok, but when i have more numerber of arguments it's hard. can any one suggest me better way?
Modification
public function save(parameters as Dictionary(Of string, object)) as int
for each item as KeyvaluePair(Of string, object) in parameters
cmd.Parameters.AddWithValue("#" & item.Key, item.Value)
next
return cmd.ExecuteNonQuery()
end function
You can use Tuple
public sub DoSomething(Tuple (Of string, string, integer) myparameter)
dim firstItem as string = myparameter.Item1
dim secondItem as string = myparameter.Item2
dim thirdItem as integer = myparameter.Item3
end sub
sub main()
Dim tuple as New Tuple(Of string, string, integer)("First","Second", 100)
DoSomething(tuple)
end sub