String redimming

  • Thread starter Thread starter pmclinn
  • Start date Start date
P

pmclinn

I have a string(10) array.

I want to redim it so that I only have the last 9 strings in the array.
What is the best way to do this?


Example:
String(0) = Dog
String(1) = Cat
String(2) = Bird

I want the cat, and bird to remain and drop the dog from the array....


-Peter
 
Something like this might help, although it's hardcoded:

Dim str(5)
str(0) = "Dog"
str(1) = "Bird"
str(2) = "Cat"
str(3) = "Horse"
str(4) = "Lion"

Dim x
For x = 0 To 3
str(x) = str(x + 1)
Next
str(4) = ""
ReDim Preserve str(4)
 
pmclinn said:
I have a string(10) array.

I want to redim it so that I only have the last 9 strings in the array.
What is the best way to do this?


Example:
String(0) = Dog
String(1) = Cat
String(2) = Bird

I want the cat, and bird to remain and drop the dog from the array....

Using 'ReDim Preserve' you can only remove items from the end of the array.
However, you can use 'Array.Copy' to create a copy of the array and then
discard the original array.
 
pmclinn,
In addition to the other comments.

Rather then use an Array, consider using an ArrayList (.NET 1.0 & 1.1) or
List(Of T) (.NET 2.0).

Then you can use ArrayList.RemoveAt(0) or List(Of String).RemoveAt(0) to
remove the first element in the "array".

http://msdn.microsoft.com/library/d...temCollectionsArrayListClassRemoveAtTopic.asp

http://msdn2.microsoft.com/en-us/library/5cw9x18z(en-US,VS.80).aspx

Something like:

' VB 2003 syntax
Dim list As New ArrayList
list.Add("Dog")
list.Add("Cat")
list.Add("Bird")
list.RemoveAt(0)

' VB 2005 syntax
Dim list As New List(Of String)
list.Add("Dog")
list.Add("Cat")
list.Add("Bird")
list.RemoveAt(0)

--
Hope this helps
Jay [MVP - Outlook]
T.S. Bradley - http://www.tsbradley.net


|I have a string(10) array.
|
| I want to redim it so that I only have the last 9 strings in the array.
| What is the best way to do this?
|
|
| Example:
| String(0) = Dog
| String(1) = Cat
| String(2) = Bird
|
| I want the cat, and bird to remain and drop the dog from the array....
|
|
| -Peter
|
 
Jay B. Harlow said:
pmclinn,
In addition to the other comments.

Rather then use an Array, consider using an ArrayList (.NET 1.0 & 1.1) or
List(Of T) (.NET 2.0).

And since you are only using strings, try StringCollection (under the
System.Collections.Specialized namespace) :)

HTH,
Mythran
 
Mythran,
Yes!

StringCollection is a very good option in this case, as it covers .NET 1.0,
1.1 & 2.0!

--
Hope this helps
Jay [MVP - Outlook]
T.S. Bradley - http://www.tsbradley.net


|
| message | > pmclinn,
| > In addition to the other comments.
| >
| > Rather then use an Array, consider using an ArrayList (.NET 1.0 & 1.1)
or
| > List(Of T) (.NET 2.0).
| >
|
| And since you are only using strings, try StringCollection (under the
| System.Collections.Specialized namespace) :)
|
| HTH,
| Mythran
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top