Is there a way to make an empty array and growing it as it gets data in vb.net? - arrays

I have been working on a project, and am attempting to make a new array for data. I have tried making an empty array with Dim Name() As String = {}. I am using a ListView, and the way I have done it there are blank spots where I have gotten rid of data. This is my current code:
Sub English(ByVal Country() As String, ByVal Language() As String)
rbDisplayallData.Checked = False
lstResults.Visible = True
lstResults.Items.Clear()
lstResults.Columns.Clear()
With lstResults
.View = View.Details
.Columns.Add("English Speaking Countries", 200, HorizontalAlignment.Left)
End With
For i = 0 To 181
Dim EnglishSpeakingCountries(i) As String
If Language(i) = "English" Then
EnglishSpeakingCountries(i) = Country(i)
End If
lstResults.Items.Add(New ListViewItem({EnglishSpeakingCountries(i)}))
Next
End Sub
I am trying to get rid of these spaces.
I Was thinking if I were to compact the array or make a new one with the same data going into a new array it would fix the issue.
If you have a solution please let me know.

There are two things that could be considered an empty array
An array with no elements, i.e. a Length of zero.
An array where every element is Nothing.
All arrays are fixed-length. Once you create an array with a particular number of elements, it always has that number of elements. You can use ReDim Preserve or Array.Resize but, in both those cases, what actually happens is that a new array is created and the elements copied from the old array. The new array is assigned to the same variable but anywhere the old array is referenced, it will still have that same number of elements. Try running this code to see that in action:
Dim a1 As String() = {}
Dim a2 As String() = {"First", "Second", "Third"}
Dim b1 = a1
Dim b2 = a2
Console.WriteLine(a1.Length)
Console.WriteLine(a2.Length)
Console.WriteLine(b1.Length)
Console.WriteLine(b2.Length)
Console.WriteLine()
ReDim Preserve a1(2)
Array.Resize(a2, 6)
Console.WriteLine(a1.Length)
Console.WriteLine(a2.Length)
Console.WriteLine(b1.Length)
Console.WriteLine(b2.Length)
Console.ReadLine()
Output:
0
3
0
3
3
6
0
3
As you'll be able to see, a1 and a2 end up referring to new arrays with the specified lengths but the original arrays with the original lengths still exist and are still accessible via b1 and b2.
If you start with an array with no elements then you can use ReDim Preserve or Array.Resize to give the appearance of resizing the array but that's not really what's happening and that should generally be avoided. If you know how many elements you'll end up with then you could create an array of that size and then set each element in turn. You'd need to keep track of the next element index though, so that's still a bit tedious.
Generally speaking, if you want an array-like data structure but you want it to be able to grow and shrink as required, you should use a collection. The most common collection is the List(Of T), where T is any type you care to specify in your code. If you want to store String objects then use a List(Of String). You can call Add to append a new item to the end of the list, as well as Insert, Remove and RemoveAt methods. You can also get or set an item by index, just as you can do for array elements.
Note that a List(Of T) actually uses an array internally and uses the aforementioned method of "resizing" that array. It optimises the process somewhat though, which makes the code easier for you to write and large collections more efficient to use.
It's worth noting that, in your own code, the Columns and Items properties of your ListView are both collections, although they are slightly different to the List(Of T) class.
Looking at your original code, this:
For i = 0 To 181
Dim EnglishSpeakingCountries(i) As String
If Language(i) = "English" Then
EnglishSpeakingCountries(i) = Country(i)
End If
lstResults.Items.Add(New ListViewItem({EnglishSpeakingCountries(i)}))
Next
could be changed to this:
Dim englishSpeakingCountries As New List(Of String)
For i = 0 To 181
If Language(i) = "English" Then
englishSpeakingCountries.Add(Country(i))
lstResults.Items.Add(Countries(i))
End If
Next
Note that you're just adding items to two collections. I guess the question is whether you actually need this extra collection at all. If you do want to use it later then you need to assign it to a member variable rather than a local variable. If you don't need it later then don't create it at all. As I said, you're already adding items to a collection in the ListView. Maybe that's all you need, but you haven't provided enough info for us to know.

Related

Visio VBA - How can I distribute shapes with a known, fixed distance

I'd like to place all currently selected shapes into an array. I'd then like to sort that array so I can find either the top most or left most shape in the array. I'd then like to use that shape as my starting point, and then from there align the other shapes a fixed, known distance apart. I've tried to place the shapes into an array like so:
Dim numShapes As Integer, i As Integer
Dim arrShapes As Visio.Selection
numShapes = Visio.ActiveWindow.Selection.Count
For i = 1 To numShapes
arrShapes(i) = Visio.ActiveWindow.Selection(i)
Next i
I have tried to create the array with no type specification, specifying as variant, and as in this example as selection. I don't know if I can put them into a list of some kind either? Obviously I can't get to the point of sorting the array and then distributing my shapes until I can get the array to populate. I'm placing a break point in the code and I have the "Locals" window open and I can see that the array is not being populated.
Update:
Why does this work,
Dim Sel As Visio.Selection
Dim Shp As Visio.Shape
Set Sel = Visio.ActiveWindow.Selection
For Each Shp in Sel
Debug.Print Shp.Name
Next
And this does not?
Dim i As Integer
Dim Shp As Visio.Shape
For i = 1 To Visio.ActiveWindow.Selection.Count
Set Shp = Visio.ActiveWindow.Selection(i)
Debug.Print Shp.Name
Next i
Regards,
Scott
There was a couple of problems in your code - fixing only one would not have got you any further in understanding if you had actually fixed anything.
Your arrShapes is declared as a general object - the Selection
Object is one of those objects that is the Jack of all trades, and
master of none.
You didn't "Set" when assigning to the array.
I don't have Visio on this machine, so cannot directly test the code below. I am also assuming that all items selected are shapes (usually a safe assumption in Visio).
Dim numShapes As Integer, i As Integer
Dim arrShapes() As Shape ' Set this up as an array of shape
If Visio.ActiveWindow.Selection.Count > 0 then ' don't want to cause a problem by setting the array to 0!
ReDim arrShapes(Visio.ActiveWindow.Selection.Count)
numShapes = Visio.ActiveWindow.Selection.Count ' while not really necessary it does help explain the code.
For i = 1 To numShapes
' must Set as we want the reference to the shape, not the default value of the shape.
Set arrShapes(i) = Visio.ActiveWindow.Selection(i)
Next i
Else
MsgBox "No shapes selected. Nothing done." ' soft fail
End If

String array not accepting a string

I'm trying to make a simple login program, but I'm having troubles with assigning a value to my two arrays "User()" and "Pass()". I have the following code on a form titled "frmCreate". This is the form I will be using to create the accounts.
Public Class frmCreate
Dim passs() As String
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnCreate.Click
If Not (txtUser.Text = "") And Not (txtPass.Text = "") Then
userCount = userCount + 1
User(userCount) = txtUser.Text
Pass(userCount) = txtPass.Text
Else
MsgBox("Please enter a username or password")
End If
End Sub
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
passs(0) = "hi"
End Sub
End Class
The issue I'm getting is as follows:
Error in array
The txtPass and txtUser are both text boxes, which can be edited by the user.
Any help would be much appreciated! (Also, I tried mucking around with the variables and all, the strings after the = sign aren't the problem, and when I set the "userCount" inside the brackets to "0" for example, it still returned the same error)
EDIT Added code as text, rather then image (image still there). Note the extra few lines at the end where I set a new array which I named passs() to "hi". The dim is also further up. If I am declaring my variables incorrectly, please let me know.
EDIT2 Ok, I changed my declaration of "User()" and "Pass()" to = {}. Now my problem is that I'm getting the error that the value is out of the bounds of the array. I understand that this happens when you try to call on a non existent value which is outside the arrays boundaries, but the array I set has no boundaries, and I'm just trying to give it a value, not call on one.
EDIT3 Ugh... Ok tweaked a bit, I've found that if I add an unused value to "User()" then it is able to replace it. So I can get the program to replace already existent values in arrays, but I can not get it to create new values in arrays.
It would appear your User variable is NULL. I presume this is intended to be a list of strings or something similar?
You should check the place you are initializing this variable to make sure it is being created...
You could add an
If User Is Nothing
line at the start of the function to check this, and if the list is null at this point create it.
I have discovered what it is I was doing wrong. See, I thought when I declared a variable with empty parenthesis, it created a dynamic variable. This is obviously not the case. Since setting the size of my variable to the ludicrous size of 50,000, my program now works. Thanks to all who tried to help, and sorry for asking the wrong thing :/
Your problem here is that you only defined the variable array. You never reserved a spot for the array values in memory. Therefore your variable has a value of Nothing.
Before assigning the first item, you must assign room for it...
'We say we want an array
Dim passs() As String
'We ask the memory to reserve six spots for our array
passs = New String(5) {}
'We assign the first spot to the String "hi"
passs(0) = "hi"
However, if you don't know how many items you are going to add in your array, I strongly suggest you to use a List(of T) instead :
Dim passs As New List(of String)
passs.Add("hi")

Excel VBA - How to search array and return value?

I have some data that looks like this (starting in A1):
Batman
Carl Reiner
Ford
Sushi
But I have a two dimensional array, storing those names, and their type:
Dim Names()
ReDim Names(4,1)
Names(0,0) = "Batman"
Names(0,1) = "Superhero"
Names(1,0) = "Carl Reiner"
Names(1,1) = "Comedian"
Names(2,0) = "Ford"
Names(2,1) = "Car Manufacturer"
Names(3,0) = "Sushi"
Names(3,1) = "Food"
Now, I would like to loop through column A and replace those data points with their equivalent type. I thought there would be a relatively simple way to essentially search Names(x,0) for "Batman" then just replace it with Names(x,1). What's the best way to do so?
I found somewhere that you could possibly use Vlookup with this, so I tried this, to no avail:
Evaluate(WorksheetFunction.Vlookup("A1",Names,1,False)
With my thought being it would look for "Batman" and return the value in Names in the "1" dimension...But that keeps giving me an error (Type 13 Mismatch).
Thanks for any ideas/help!
edit: I found this post and seem to be getting somewhere with Application.Match("A1",Application.Index(Names,0,1),0) which returns the position of "A1" in the array. Now I just need to return the offset and get the "1" position of that...
Edit 2: I think I got it. Using MATCH to find the position of "A1" then just use the array to find it:
Names(Application.Match("A1",Application.Index(Names,0,1),0),1)
So, when A1 is Batman, it finds that position in the index, then it finds the equivalent return in the second dimension.
(Sorry for the lack of semantics, I don't have a very good idea of how to refer to this, so please let me know if I can clarify).
It seems like this would be easier using a Dictionary.
Sub testing()
Dim dic As Object
Set dic = CreateObject("scripting.dictionary")
With dic
.Add "Batman", "Superhero"
.Add "Carl Reiner", "Comedian"
.Add "Ford", "Car Manufacturer"
End With
MsgBox "Batman is a " & dic.Item("Batman")
End Sub
I think if you want to use worksheet functions, you have to put the data into a worksheet (e.g. a column or pair of columns) instead of an array. This is pretty easy to do, though, and you can use a different worksheet, or even another workbook, and there are solutions to hide these, so your users don't have to see that data cluttering the other worksheets.

Array already dimensioned - Class module

For certain, technical, reasons, we cannot use styles in word. In an effort to speed up applying global properties over and over, I've created a class that can read from a simple xml style-sheet. The sheet contains different "paragraphs." Each paragraph simply stores the paragraph properties that we use the most.
I'm used to C++ where I can use dynamic memory and I'm trying to replicate the behavior of a dynamically allocated array. However, when I attempt to re-dim I get the error message "Array arleady dimensioned."
My research on the MSDN suggests that in order to ReDim the array has to be Global or in the "general declaration context" This makes me think it might simply not be possible to do it in a class.
Excerpt from MSDN:
"You can use ReDim only at procedure level. Therefore, the declaration
context for the variable must be a procedure; it can't be a source
file, a namespace, an interface, a class, a structure, a module, or a
block."
I have attempted to search stack overflow for "Word VBA Array already dimensioned" and went through all 3 pages of results with no avail.
private type pStyle 'Definition removed because it's not needed
private Paragraphs(0) As pStyle 'Initially an empty array of paragraphs
later I have the following function
Public Function AddEmpty()
'Create space
count = count + 1
ReDim Preserve Paragraphs(count)
AddEmpty = count
End Function
Please let me know if any ideas. I would prefer to not have to "estimate" the number of paragraph styles we will need for each style sheet as every file is different.
Private Paragraphs(0) As ...
This is not an empty array, rather it is a fixed length array with 1 element.
For a dynamic array - one you will later redimension - just declare it as:
Private Paragraphs() As ...
Dim numbers(10) As Integer
MsgBox (UBound(numbers))
ReDim numbers(4)
MsgBox (UBound(numbers))
Above code will throw array-already-dimensioned. we can do like
ReDim numbers(10) As Integer
MsgBox (UBound(numbers))
ReDim numbers(4)
MsgBox (UBound(numbers))

How to fill-up cells within a Excel worksheet from a VBA function?

I simply want to fill-up cells in my spreadsheet from a VBA function. By example, I would like to type =FillHere() in a cell, and in result I will have a few cells filled-up with some data.
I tried with such a function:
Function FillHere()
Dim rngCaller As Range
Set rngCaller = Application.Caller
rngCaller.Cells(1, 1) = "HELLO"
rngCaller.Cells(1, 2) = "WORLD"
End Function
It breaks as soon as I try to modify the range. Then I tried this (even it's not really the behavior I'm looking for):
Function FillHere()
Dim rngCaller As Range
Cells(1, 1) = "HELLO"
Cells(1, 2) = "WORLD"
End Function
This is not working neither. But it works if I start this function from VBA using F5! It seems it's not possible to modify anything on the spreadsheet while calling a function... some libraries do that though...
I also tried (in fact it was my first idea) to return a array from the function. The problem is that I only get the first element in the array (there is a trick that implies to select a whole area with the formula at the top left corner + F2 + CTRL-SHIFT-ENTER, but that means the user needs to know by advance the size of the array).
I'm really stuck with this problem. I'm not the final end-user so I need something very easy to use, with, preferably, no argument at all.
PS: I'm sorry I asked this question already, but I wasn't registered at that time and it seems that I can't participate to the other thread anymore.
You will need to do this in two steps:
Change your module to be something like:
Dim lastCall As Variant
Dim lastOutput() As Variant
Function FillHere()
Dim outputArray() As Variant
ReDim outputArray(1 To 1, 1 To 2)
outputArray(1, 1) = "HELLO"
outputArray(1, 2) = "WORLD"
lastOutput = outputArray
Set lastCall = Application.Caller
FillHere = outputArray(1, 1)
End Function
Public Sub WriteBack()
If IsEmpty(lastCall) Then Exit Sub
If lastCall Is Nothing Then Exit Sub
For i = 1 To UBound(lastOutput, 1)
For j = 1 To UBound(lastOutput, 2)
If (i <> 1 Or j <> 1) Then
lastCall.Cells(i, j).Value = lastOutput(i, j)
End If
Next
Next
Set lastCall = Nothing
End Sub
Then in order to call the Sub go into the ThisWorkbook area in VBA and add something like:
Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
Call WriteBack
End Sub
What this does is return the value of the topleft cell and then after calculation completes populates the rest. The way I wrote this it assumes only one FillHere function will be called at a time. If you want to have multiple ones which recalculate at the same time then you will need a more complicated set of global variables.
One word of warning is that this will not care what it overwrites when it populates the other cells.
Edit:
If you want to do this on a Application wide basis in an XLA. The code for the ThisWorkbook area should be something like:
Private WithEvents App As Application
Private Sub App_SheetCalculate(ByVal Sh As Object)
Call WriteBack
End Sub
Private Sub Workbook_Open()
Set App = Application
End Sub
This will wire up the Application Level calculation.
What you're trying to do won't work in Excel - this is by design.
You can do this, though:
Function FillHere()
Redim outputArray(1 To 1, 1 To 2)
outputArray(1, 1) = "HELLO"
outputArray(1, 2) = "WORLD"
FillHere = outputArray
End Function
If you then select two adjacent cells in your worksheet, enter =FillHere() and press Control+Shift+Enter (to apply as an array formula) then you should see your desired output.
Fundamentally, a function can only affect the cell it is called from. It sounds like you may need to look at using the Worksheet_Change or Worksheet_SelectionChange events to trigger the modification of cells in the intended range.
You can do this indirectly using a 2-stage process:
Write your UDF so that it stores data in a sufficiently persistent way (for example global arrrays).
then have an Addin that contains application events that fire after each calculation event, looks at any data stored by the UDFs and then rewrites the neccessary cells (with warning messages about overwrite if appropriate) and reset the stored data.
This way the user does not need to have any code in their workbook.
I think (but do not know for sure) that this is the technique used by Bloomberg etc.

Resources