How do you save an array or an ArrayList in VB.NET using My.Settings? I cannot find the array type anywhere, even in the browse window.
I know I can convert the array to a string, but I do not know how to convert a string to an array. I know that if I were to break it at a delimiter then I could convert a string to an array, but my problem is that any text at all could be stored within the array as a single value, so I cannot pick a delimiter that is unlikely to be used.
I was also facing the same problem and I came up with a solution to this.
Here are the steps:
Open up the properties of your app and select settings
select the setting name and then where it says type click on the
arrow and select browse.
in the browse window type in system.collections.arraylist and hit enter!
there you have your array!
You can use array like this:
your_array_name(here_comes_the_item_no.) = whatever
What kind of array? I've had luck using StringCollection for strings. ArrayList works for most anything else (and that's about the only place I'd use arraylist).
I would either use the StringCollection type, and just convert your elements to/from strings when storing them in my.settings, or use XML Serialization to turn the array in to an xml string, and store that in my.settings.
Related
I want to shorten the length of a string to 40 Elements. I did try it with length(), last(), first()... but in order to convert it into an array I need to know the content of the string. The String I want to shorten could have any value.
Would be cool if you can share similiar problems or have a solution.
I tried to convert the string into an Array, with the split() function but I dont know the 40th Element.
By using the length function I could find out the length of the String I want to shorten.
Maybe there is something to delete the last few Elements of the string but I dont know it.
substring(variables('String'),0,39)
This should solve your problem
Test-OnlineFast is a (great) custom function which creates an array with the following output incredibly quickly:
I can use this output on its own, but if I want to list just the addresses using the commands:
$Pinged = Test-OnlineFast $MyIPList.ipaddress
$Pinged.Address
I receive the output:
Even though the array is a standard type:
This prevents me from doing things like comparing the array to another and matching the addresses.
Is there any way to 'convert' the array, so I can use it in this way? I've tried exporting it to a CSV or text file, and importing, but it's the same.
You could convert the array to a List<psobject>:
$list = [System.Linq.Enumerable]::ToList([psobject[]]$Array)
Since List<psobject> doesn't have a property called Address, you can now rely on property enumeration:
$list.Address
someone knows if it's possible create a fixed size ArrayList? Or I have to use necessarily an array?
I try with this
Dim array As ArrayList
array = New ArrayList(10)
and
array.Capacity = 10
But I can add more than 10 items, and it doesn 't show me any kind of error how I expected.
Thanks
Just use an Array this size will not change unless you explicitly code it to.
Dim myArray(9) As String 'or whatever object you need Integer, etc.
Note that specifying 9 will create 0-9 i.e. 10 items in your array
(ArrayLists are bad in many many ways so don't use them)
Capacity of ArrayList tells the maximum number of items ArrayList can currently hold. Capacity will be increased automatically at run time when more items are added in the ArrayList.
For fixed size, use Array as mentioned below:
Dim intArray(9) As Integer
If you are wanting to store different types in your collection you can use;
Dim myArray(5) As Object
If you wanted to read them back as the type you put them in as you would have to convert their type back what they were originally.
I do not recommend this as an approach. If you want to do this then I suggest you create a custom object such as a class or structures that will contain properties for each of the values you wish to set.
I have a 2-dimensional jagged array at the moment. I have it filled with everything i need. My issue now is that I cannot pull from it. I need to be able to take information from the array and store the values as a primitive type. Instead I keep receiving errors about how it cannot convert a System.Object[] to a System.Int32. I initialized the array as:
$values = New-Object system.Array[][] 1000,1000
Essentially what I need do to is get something by this logic work.
$pageNum = $values[$i][1]
Can someone help me understand how to properly pull just the value of the object so I can cast it to an int? Thanks in advance!
What i ended up finding out is using the .Get(0) worked for me as below.
[int]$pageNum = $values[$i][2].get(0)
This successfully took the value out of the object alone and it did not try and copy the rest of the object over like before
I basically want to write logic such that if certain element is already present in array i dont want to again put it into it. My array is one dimensional. I am not able to understand how filter function works. Please help. Thanks.
I'm not sure I exactly get what you are trying to do.
But what I understand makes me recommend you to use Dictionary (it is basically a Hashmap)
It works with keys and values.
Each element of this Dictionary contains a unique key (a String that is used to access specifically the data you want) and a value attached to it (can be a String, a Number...)
Here is how to use it :
in your VBA page, go to Tools -> References -> add "Microsoft Scripting Runtime" from the list of available references.
Then in your code :
Dim myDico as Dictionary
set myDico = new Dictionary
myDico.add "myKey", 5
msgbox myDico.Item("myKey") '5