This is probably quite simple to do but I am very new to visual basic so please bear with me.
I know similar questions have been asked, but none of them seem to answer my question. I have an array created earlier, and want to randomly choose one value and change it.
I have two random number generators ('GeneratorP1' and 'GeneratorP2'), which are added to some text a create a variable name which is stored in Loc1:
Dim Loc1 As String
Loc1 = "th" & GenerateP1() & "(" & GenerateP2() & ")"
eg: th5(4) is stored in Loc1
How would I go about changing the value of th5(4)?
EDIT: I have 5 arrays (th1, th2, th3, th4, th5), with indexes up to 4
If I'm understanding your question correctly (which I'm not sure I am), you're wanting to use the string stored in variable Loc1 as a variable name?
You said you created an array earlier, so why do you need to randomly generate the variable (GenerateP1) and the index (GenerateP2)? If you have a single array, can't you use randomly pickthe index, then assign whatever value you want to it?
th5(GenerateP2()) = "my value"
If you had multiple arrays (th1, th2, th3, etc.) I would suggest using a Dictionary to randomly pick which array to use. Alternatively you could use a multidimensional array and generate both indexes
th(GenerateP1(), GenerateP2()) = "my value"
If possible, you should consider using a List instead of an Array.
Instead of having multiple arrays, use a 2-dimensional array
Dim th(5,4)
So instead of trying to reference th1(3), you could use th(1,3) or th5(2) would be th(5,2)
Related
If I affect a full array to a ListBox, using ListBox1.List = [{1;2;3;4}] for example, the Listbox's .List items keep their correct type (here numbers), but if I use ListBox1.AddItem 5 or Listbox1.List(5) = 6 to set an individual item the type is automatically changed to String.
Sample Code:
Private Sub UserForm_Initialize()
ListBox1.List = [{1;2;3;4}]
ListBox1.AddItem 5
ListBox1.AddItem
ListBox1.List(5) = 6
End Sub
Later on, when comparing values, I get wrong results because numbers are not equals to text (5 <>"5").
Is there any easy (1) way to ensure the type of the list items is not converted to String?
(1) I know I can explicitly make the conversion to String, but I rather keep my values as numbers instead of "numbers-strored-as-text" in the listbox
I guess that will be impossible when using AddItem. According to https://learn.microsoft.com/en-us/office/vba/api/access.listbox.additem, the first parameter is a string, so everything you pass will be converted to a string.
Probably your best bet is to collect all items in an array and assign the array using ListBox1.List. Or you have to live with numbers stored as string...
Update
I mixed the pages up - link to the Access page was wrong.
Anyhow, Documentation is rather poor. It says "valid object" but doesn't define what that means. At least it is not possible to add a object - that throws a type mismatch.
Also, the documentation states that a Variant is returned, but it seems thatis not true - when I try to get the result, the compiler throws an error.
As a conclusion, I assume that AddItem converts everything to a string (and throws an error if that fails). So I still assume that you have to build up an array and assign it to List if you want to have real numbers.
I'm developing a macro using extra attachmate. we have a 3rd party tool called CSS from where macro fetches required data. Now I'm trying to arrange dates from old to new.
Firstly I have changed date(10/01/14) to number(140110) and then extracted all values into array. From array is there any Function to sort the numbers or should i compare each value manually in loop?
After comparing the records from array, I have to write this data in different columns of single row.
IF my approach is incorrect then please suggest me correct way.
Thanks,
Yaswanth
Your question talks about a lot of different things. I assume that you want to sort an array of integer. To do that you can use the Sort function.
Dim arr(2) As Integer
arr(0) = 140110
arr(1) = 130110
arr(2) = 150110
Array.Sort(arr)
I have an array variable (string type). It contains certain no. of items, that I donot know how many they are.
I need to run a loop for that many nos. that the array contains. I tried LBound and UBound loop but it says my array is not a system array.
How can I know how many items my array contains?
Thanks
Furqan
You can use the Length property of the array object.
From MSDN (Array.Length Property):
Gets a 32-bit integer that represents the total number of elements in all the dimensions of the Array.
Read about arrays in VB.NET and the Array class for better understanding of arrays in VB.NET and the .NET framework.
Update:
However, for looping over an array you should simply use a For Each loop (as an array is treated like any other collection in .NET) - this way you will not make any silly mistakes with array bounds and off by ones:
For Each item As arrayItemType in MyArray
' do stuff with item
Next
See the example on this page.
You look at the length:
To get the number of items in the first dimension: arrayName.GetLength(0)
If you need the index, use GetUpperBound(0)
Some helpful examples here.
Like Oded said, you can use the Length-propery of the Array. This would look something like this:
Dim data As String() = {"one", "two", "three", "four"}
For i = 0 To data.Length - 1
Console.WriteLine(data(i))
Next
If you just want to loop all strings in your array, you can use For Each as well:
For Each s As String In data
Console.WriteLine(s)
Next
If the compiler is telling you that your variable is not a system array, then chances are, it's not an array. If it's not an array, you won't be able to get its bounds through any means.
Inspect the variable in the locals window and verify that your variable is of the type that you think it is. It's probably not an array after all.
Still having trouble can you please help?
This needs to be written in Visual Basic
Here is a statement from the Main part of the program...
mylist.ForEach(AddressOf ProcessLink)
What this statement says is the following.... "For each item in the ArrayList "mylist" send item to the sub program "ProcessLink"
Note that ProcessLink is going to receive multiple groups of data from the ArraList "mylist"
ProcessLink then takes each value sent to it and turns it into "P.myName" and P.myValue"
I need ProcessLink to then add these values to an array. And each time it receives a batch of data from the ArrayList "mylist" it will add those values to the same Array. ProcessLink will then sort the array based on "P.Value"
I then need ProcessLink to output the name value pairs in the array and output the result as...
Response.Write("<tr><td>" & P.myName & "</td><td>" & P.myValue & "</td></tr>")
What should the code in ProcessLink look like?
I really reccomend using generic object lists rather than Arrays. You will get all of the functionality you need + 10x more. Sorting, adding, etc are way easier. With generic lists you don't have to worry about declaring the Array size, or any of the difficulties when working with Arrays containing objects. Take a look to the following for more information including the code sample:
.NET Object Collections Using Generics 101:
http://allen-conway-dotnet.blogspot.com/2009/11/net-object-collections-using-generics.html
List(Of T) Class:
http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx
I have an array Newstr(20) When I fill it, I need to know how many of its indexes has been filled ? and find out the values.
I need to know how many of its indexes has been filled ?
Arrays don't keep that information. They only know how many spots you allocated. You have to track how many you assigned yourself. More than that, if you're working with a collection where you don't know how many items there will be, arrays are really the wrong choice in the first place. You should use a List(Of T) instead.
You could populate the array with a known string, then test for that string to see how many elements in your array are filled.
I would - however - suggest using an array list. You can get the number of elements added to the list from the Count property. This is the MSDN entry for Array Lists.
In order to find which of the elements have been filled, you can use a LINQ construction like this:
Dim input() = New String() {"abc", "def", "ghi", "", Nothing}
Dim output = input.Where(Function(i) Not String.IsNullOrEmpty(i)).ToArray
When you run this code, the output array will contain "abc", "def" and "ghi".
You can modify the selector of the Where to suit your preference if you're coding for a different type of array.
For instance the selector for Integer? will be:
input.Where(Function(i) (Not i Is Nothing) Or (i <> 0)).ToArray
Of course, you'll have to be coding in .NET 3.5+ in order to get access to LINQ.