Issue getting array from dictionary - vb.net

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()

Related

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.

Vb.net List within a list

So far I have the following code below. How to display each list value to textboxes?
Dim list As New List(Of String)
list.Add(dgvData.SelectedCells(0).Value.ToString)
list.Add(dgvData.SelectedCells(1).Value.ToString)
list.Add(dgvData.SelectedCells(2).Value.ToString)
list.Add(dgvData.SelectedCells(4).Value.ToString)
Dim val As String
For Each val In list
' MsgBox(val)
Next
Add your code in the CellEnter Event. You don't need to declare the list.
Private Sub YourDataGrid_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles YourDataGrid.CellEnter
txtbox1.Text = YourDataGrid.Item(col, row).Value.ToString
txtbox2.Text = YourDataGrid.Item(col, row).Value.ToString
txtbox3.Text = YourDataGrid.Item(col, row).Value.ToString
txtbox4.Text = YourDataGrid.Item(col, row).Value.ToString
end sub

Insert Arrays into Combobox

Private Sub Form2_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim window As New System.IO.StreamReader("Pfad", True)
Dim lines As String
Dim Element As String()
Dim SplitArray As String()
lines = window.ReadToEnd
Element = lines.Split("!")
Element.Count
For i = 0 To Element.Count - 1
SplitArray = Element(i).Split(Chr(13))
Next
ComboBox1.Items.Add(SplitArray)
End Sub
Hello I'm a Newbie. I want to fill my Combobox with the results of the split.
Use addrange to fill combobox items with array of strings
ComboBox1.Items.AddRange(SplitArray)

VB Syntax: return type of a function, returning a dynamic array of generic lists

I would like to convert a few lines of VB code into a function call for the sake of compactness and clarity. Currently the code populates a dynamic array of generic lists:
Dim itemCount as Integer = 5
Dim myarr(itemCount - 1) As List(Of DataRow)
' some code that populates myarr
What would be the syntax of return type for a function that would return myarr?
Here is a sample code. The type is List(Of DataRow)(), this can also be verified via TypeName.
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim m() As List(Of DataRow)
m = getTest()
End Sub
Private Function getTest() As List(Of DataRow)()
Dim m(10) As List(Of DataRow)
MessageBox.Show(TypeName(m))
For i = 0 To 10
m(i) = New List(Of DataRow)
Next
Return m
End Function

New Random in a For-Next-Statement Repeats the Same Value

I am creating 3 random bytes in a for-next-statement.
However, they are always the same for each for-next, for example "333", or "777".
I wonder why this is so and what I am doing wrong.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim s As String = String.Empty
For i As Integer = 0 To 2
s += GetRandomByte().ToString
Next
Me.TextBox1.Text = s
End Sub
Public Function GetRandomByte(Optional ByVal uLow As Byte = 1, Optional ByVal uHigh As Byte = 9) As Byte
Dim r As New Random
Dim b As Integer = r.Next(CInt(uLow), CInt(uHigh + 1))
Return b
End Function
In your constructor for the new Random, give it a new seed each time. I like to use the current timestamp's hashcode value
New Random(Now.GetHashCode)
keep same instance for all calls:
Dim r As New Random()
Public Function GetRandomByte(Optional ByVal uLow As Byte = 1, Optional ByVal uHigh As Byte = 9) As Byte
Return r.Next(CInt(uLow), CInt(uHigh + 1))
End Function
or, pass to Random constructor a seed:
Dim r As New Random(CInt(Date.Now.Ticks And &h0000FFFF))