I have several arrays in VB.net. These arrays have different lengths and I need the combination between them. The simple solution is using nested loop as in the example:
Dim Array1= {10, 11, 12}
Dim Array2= {15}
Dim Array3= {1,2,3}
Dim array(2) As Object
array(0) = Array1
array(1) = Array2
array(2) = Array3
for (a = 1 to < Array1.Length - 1)
for (b = 1 to < Array2.Length - 1)
for (c = 1 to < Array3.Length - 1)
'Get combination
Next
Next
Next
Output: {10,15,1},{10,15,2},{10,15,3},{11,15,1},{11,15,2},...
But the real problem is that the number of arrays is not a fixed parameter (could be 3 as in the example or any other number) and therefore nested loops is not a solution.
Any idea?
Here's a quick rewrite of some code I wrote almost a decade ago while still using VB.Net 2003...what great memories lying around in my hard drive!
Hope you find its output useful:
10,15,1
10,15,2
10,15,3
11,15,1
11,15,2
11,15,3
12,15,1
12,15,2
12,15,3
The code that generated it:
Public Class Form1
Private WithEvents FC As ArrayCombinations = Nothing
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
If IsNothing(FC) Then
FC = New ArrayCombinations
FC.AddArray(New Integer() {10, 11, 12})
FC.AddArray(New Integer() {15})
FC.AddArray(New Integer() {1, 2, 3})
FC.GenerateCombos()
End If
End Sub
Private Sub FC_combinations(combos As System.Collections.Generic.List(Of System.Collections.Generic.List(Of Integer))) Handles FC.Combinations
For Each combo As List(Of Integer) In combos
Debug.Print(String.Join(",", combo.Select(Function(i) i.ToString())))
Next
FC = Nothing
End Sub
End Class
Public Class ArrayCombinations
Private Arrays As New List(Of Integer())
Private WithEvents BGW As New System.ComponentModel.BackgroundWorker
Public Event Combinations(ByVal combos As List(Of List(Of Integer)))
Public Sub New()
BGW.WorkerReportsProgress = True
End Sub
Public Sub AddArray(ByVal values() As Integer)
If Not BGW.IsBusy Then
Arrays.Add(values)
End If
End Sub
Public Sub GenerateCombos()
If Not BGW.IsBusy Then
BGW.RunWorkerAsync()
End If
End Sub
Private Sub BGW_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BGW.DoWork
Dim sizes As New List(Of Integer)
Dim combinations As Integer
Dim remainder As Integer
Dim quotient As Integer
Dim combination As List(Of Integer)
Dim combos As New List(Of List(Of Integer))
If Arrays.Count > 0 Then
combinations = 1
For Each factor() As Integer In Arrays
sizes.Add(factor.Count)
combinations = combinations * factor.Count
Next
For i As Integer = 0 To combinations - 1
combination = New List(Of Integer)
For j As Integer = 1 To Arrays.Count
combination.Add(Nothing)
Next
quotient = i \ sizes.Item(sizes.Count - 1) ' Integer Division
remainder = i Mod sizes.Item(sizes.Count - 1)
combination(Arrays.Count - 1) = Arrays.Item(Arrays.Count - 1)(remainder)
For j As Integer = (sizes.Count - 2) To 0 Step -1
combination.Item(j) = Arrays.Item(j)(quotient Mod sizes.Item(j))
quotient = quotient \ sizes.Item(j) ' Integer Division
Next
combos.Add(combination)
Next
e.Result = combos
End If
End Sub
Private Sub BGW_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BGW.RunWorkerCompleted
RaiseEvent Combinations(e.Result)
End Sub
End Class
You can create a list to hold the combined values from all the arrays and then push those values back into an array, like this:
Dim listCombined = New List(Of Integer)
listCombined.AddRange(array1)
listCombined.AddRange(array2)
listCombined.AddRange(array3)
Dim arrayCombined As Integer() = listCombined.ToArray()
Note: This example assumes you have integer arrays, but you can change the type to whatever the arrays actually hold. You can also loop through the arrays instead of listing them one by one for the AddRange calls.
Related
I have several arrays named : Array1, Array2, Array3, ... and also some comboboxes named: cboArray1, cboArray2, cboArray3, ....
How can i write a GENERAL code to add elements of each array to corresponding combox. I know following code works, but its not GENERAL and ABSTRACT.
For i = 0 To Array1.Length - 1
cboArray1.Items.Add(Array1(i))
Next
For i = 0 To Array2.Length - 1
cboArray2.Items.Add(Array2(i))
Next
...
Working procedure maybe as follows: 1. Find all comboboxes in form (easy) 2. extract name of a combox (easy) 3. find similar-named array from code (difficult) 4. ....
I can use other sets like List, ... if it makes sense.
Here's what you're asking for using Reflection...though I'm not sure how useful this really is:
Public Class Form1
Private Array1 As String() = {"cat", "dog", "fish"}
Private Array2 As String() = {"alpha", "beta", "gamma"}
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
WireComboBoxes(Me)
End Sub
Private Sub WireComboBoxes(ByVal container As Control)
For Each ctl As Control In container.Controls
If TypeOf ctl Is ComboBox AndAlso ctl.Name.ToUpper.StartsWith("CBO") Then
Dim cb As ComboBox = DirectCast(ctl, ComboBox)
Dim arrName As String = cb.Name.Substring(3)
Dim fi As System.Reflection.FieldInfo = Me.GetType.GetField(arrName, Reflection.BindingFlags.Instance Or Reflection.BindingFlags.Public Or Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.IgnoreCase)
If Not IsNothing(fi) Then
cb.DataSource = fi.GetValue(Me)
End If
ElseIf ctl.HasChildren Then
WireComboBoxes(ctl)
End If
Next
End Sub
End Class
Hello im having trouble with my code !
we are asked to organize a list of names from a text.txt file the make them show up into a lits box (got that part down :) ) . then from the list box we are asked to create an array and sort that array (using our own sorting method) and organize the names using a button in assending order and another button organizing the array in decending order. the results from the orders names should appear in another list box .
i have gotten only the last name in the list to show up in the second list box but my code has no errors it just wont order the names properly! Help!!!!!
here is my code :)
Public Class FileSort
Dim sr As IO.StreamReader = IO.File.OpenText("C:\Users\Inspiron 15\documents\visual studio 2010\Projects\assigment4 EL\assigment4 EL\names.txt")
Structure names
Dim c As Integer
Dim fullname As String
End Structure
Dim allNames(99) As names
Private Sub btnName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnName.Click
Do While sr.Peek <> -1
Name = sr.ReadLine
LstNames.Items.Add(Name & " ")
Loop
sr.Close()
End Sub
Private Sub bubbelsort(ByRef names() As System.String, ByVal c As Integer)
c = 0
names(c) = sr.ReadLine()
c = c * 1
For c = 1 To 99 Step +1 '~~~ Addding (Z to A) to the the Listbox
lstOrderedNames.Items.Add(Name & "")'
Next
End Sub
Private Sub BtnAssend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAssend.Click
Dim names(99) As String
Dim c As Integer
c = 0
Dim A As Integer
A = 99
names(c) = sr.ToString
c = c + 1
For c = 1 To 99 Step +1 '~~~ Addding (Z to A) to the the Listbox
lstOrderedNames.Items.Add(Name & "")
Next
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDessend.Click
Dim names(99) As String
Dim c As Integer
c = 0
Dim A As Integer
A = 99
names(c) = sr.ToString
names(A) = sr.ToString
A = A - 1
For A = 99 To 0 Step -1 '~~~ Addding (Z to A) to the the Listbox
lstOrderedNames.Items.Add(Name & "")
Next
End Sub
enter image description here
Since your problem is the sorting algorithm (if I understand this correctly).
At first we need an array.
Dim arr(ListBox1.Items.Count - 1) As String
For i As Integer = 0 To arr.Length - 1
arr(i) = CStr(ListBox1.Items(i))
Next
Next the sorting algorithm. Since you wanted to go with BubbleSort:
Private Sub StringBubbleSort(arr As String)
For i As Integer = 0 To arr.Length - 1
For j As Integer = 0 To arr.Length - 2 - i
If String.Compare(arr(j), arr(j + 1)) > 0 Then
Dim temp As String = arr(j)
arr(j) = arr(i)
arr(i) = temp
End If
Next
Next
End Sub
Then you use this function and copy the array to your second ListBox.
StringBubbleSort(arr)
ListBox2.Items.AddRange(arr)
String.Compare: https://msdn.microsoft.com/de-de/library/84787k22(v=vs.110).aspx
you could use linq
ListBox1.Items.Add("Battle")
ListBox1.Items.Add("Cattle")
ListBox1.Items.Add("apple")
ListBox2.DataSource = (From l In ListBox1.Items
Select l Order By l Ascending).ToList
I have a code to display a non-repeating random numbers, but I stuck by not sort in ascending order.
Here my code:
Public Class Form1
Dim intNumber As Integer
Dim arrNumber(0 To 5) As Integer
Dim i, x, y As Integer
Private Sub mostrar_resultados_sorteo(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
repetido()
For i = 0 To 5
ListBox1.Items.Add(arrNumber(i).ToString)
Next
End Sub
Private Sub repetido()
For x = 0 To 5
Start:
Randomize()
intNumber = Int((49 * Rnd()) + 1)
For y = 0 To 5
If intNumber = arrNumber(y) Then
GoTo Start
End If
Next y
arrNumber(x) = intNumber
Next x
ordenar()
End Sub
Private Sub ordenar()
End Sub
End Class
Your question is not tagged with programming language, but I guess it is VB.
Please see this link on how to sort an array.
Say I have two files Form1.frm and Module1.bas in a Visual Basic 6 project.
Form1.frm:
Public myArray As Variant
Private Sub Form_Load()
ReDim myArray(2)
Call PopulateArrays
End Sub
Module1.bas:
Public Sub PopulateArrays()
Form1.myArray(0) = Array(1, 2, 3)
Form1.myArray(1) = Array(4, 5, 6)
Form1.myArray(2) = Array(7, 8, 9)
End Sub
The issues is that once the code runs, 'myArray' remains empty.
If I put the PopulateArrays Sub within the main Form1.frm the arrays do populate.
Any thoughts?
There are certain things you can't declare in an object module (userforms are just special object modules) and one of them is arrays. If you had declared
Public myArray() As Variant
Then you would have got a compile error that said as much and would have known. But since you declared as Variant, the compiler didn't complain, but the assignment just doesn't work.
One option is to make the Variant array private and use property statements (this is VBA, but should be the same for VB6). In the userform
Private mmyArray() As Variant
Public Property Get myArray() As Variant
myArray = mmyArray
End Property
Public Property Let myArray(lmyArray As Variant)
mmyArray = lmyArray
End Property
Private Sub UserForm_Click()
MsgBox Join(Me.myArray(0), "_")
End Sub
Private Sub UserForm_Initialize()
ReDim mmyArray(2)
PopulateArrays
End Sub
And in a standard module
Public Sub PopulateArrays()
Dim ar(0 To 2) As Variant
ar(0) = Array(1, 2, 3)
UserForm1.myArray = ar
End Sub
I see you've used a workaround to let your Form1 hold a public array. However, I think a better solution would be: (A) move the array to the module and make it public (B) create accessor/mutator functions for it.
If you aren't keeping multiple instances of the form at the same time, then (A) is the simplest solution.
Here is the code I tested for (A),
Form:
Private Sub UserForm_Initialize()
Call PopulateArrays
Call displayArray
End Sub
Private Sub displayArray()
Dim v1 As Variant
Dim s As String
For Each v1 In myArray
s = s + Join(v1, ", ") + vbNewLine
Next v1
MsgBox s
End Sub
Module:
Dim form1 As UserForm1
Public myArray As Variant
Public Sub start()
Set form1 = New UserForm1
End Sub
Public Sub PopulateArrays()
ReDim myArray(2)
myArray(0) = Array(1, 2, 3)
myArray(1) = Array(4, 5, 6)
myArray(2) = Array(7, 8, 9)
End Sub
I'm trying to make an array of string, but can't get it to run correctly. Here is what I have.
Public Class Form1
Dim wordArray() As String
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'increase the size of string array by one, by setting the new upperBound at the current Length
'use Preserve so that string currently in the array are not overwritten with Nothing
ReDim Preserve wordArray(wordArray.Length)
'use an TextBox to get the name of the new string from the user
'assign this name (which is a String) to the last element of the string array
wordArray(wordArray.GetUpperBound(0)) = TextBox2.Text
End Sub
End Class
Any help will be appreciated, thank you.
How about a List(Of String)? No ReDim required here.
Private wordList As New List(Of String)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
wordList.Add(textbox2.Text)
End Sub
For Fixed-size array:
Dim strCDRack(0 to 2) As String
strCDRack(0) = "Deftones"
strCDRack(1) = "Tool"
strCDRack(2) = "Disturbed"
For Dynamic array:
Dim strCDRack() As String
ReDim strCDRack(0 to 2) As String
strCDRack(0) = "Deftones"
strCDRack(1) = "Tool"
strCDRack(2) = "Disturbed"
For expanding the Dynamic array:
Dim strCDRack() As String
ReDim strCDRack(0 to 2) As String
strCDRack(0) = "Deftones"
strCDRack(1) = "Tool"
strCDRack(2) = "Disturbed"
ReDim Preserve strCDRack(0 to 3) As String
strCDRack(3) = "Charlotte Church"
For more info about VB arrays checkout this link..
Use a list!
Dim StringArray As New List(Of [String])()
And in the click handler:
StringArray.Add(TextBox1.Text)
You should use Collections here like List(Of String), it grows as the number of element grows. No need to maintain size yourself. Also the problem might at your end is, when you ReDim try to increase your array size.
ReDim Preserve wordArray(wordArray.Length + 1)