Populate a string array dynamically

Z

Zordiac

How do I dynamically populate a string array?
I hope there is something obvious that I'm missing here

Option Strict On
dim s() as string
dim sTmp as string = "test"
dim i as integer

s(i)=new string(test)
Above line gives - error implicit conversion string to 1-dim array of
char
Looked at stringbuilder and coverting to char array in contructor but
did not find an answer
 
A

Armin Zingler

Zordiac said:
How do I dynamically populate a string array?
I hope there is something obvious that I'm missing here

Option Strict On
dim s() as string
dim sTmp as string = "test"
dim i as integer

s(i)=new string(test)
Above line gives - error implicit conversion string to 1-dim array
of char
Looked at stringbuilder and coverting to char array in contructor
but did not find an answer

I get a different error: variable 'test' not declared.

Which values do you want to put in the array?


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
H

Herfried K. Wagner [MVP]

* (e-mail address removed) (Zordiac) scripsit:
How do I dynamically populate a string array?
I hope there is something obvious that I'm missing here

Option Strict On
dim s() as string
dim sTmp as string = "test"
dim i as integer

s(i)=new string(test)
Above line gives - error implicit conversion string to 1-dim array of
char
Looked at stringbuilder and coverting to char array in contructor but
did not find an answer

I feel sorry, but I am not able to understand what exactly you want to
do.
 
J

Jay Banks

Zordiac said:
How do I dynamically populate a string array?
I hope there is something obvious that I'm missing here

Option Strict On
dim s() as string
dim sTmp as string = "test"
dim i as integer

s(i)=new string(test)
Above line gives - error implicit conversion string to 1-dim array of
char
Looked at stringbuilder and coverting to char array in contructor but
did not find an answer

It has something to do with the fact that you're referencing the array
index [i.e.- s(i)] and also implicitly dimensioning your array by
specifying values for the indices [i.e. - (test) which is erroneous,
by-the-by]

Try this:

Option Strict On
Dim s() As String
Dim sTmp As String = "test"
Dim i As Integer

s(i) = "test"
--or--
s(i) = sTmp

-----
As a side note, if you wanted to create an array, and not dimension it
directly, you could say:
Dim s() As String = { "One", "Two", "Three" }

Hope this helped

J.
 
M

Marc Butenko

If I understand your question correctly, here is what you need to do:

Option Strict On
Dim S(-1) as String
Dim sTmp as String = "test"
Dim i as Integer = 0

Redim S(S.Length)
S(i) = sTmp

Redim S(S.Length)
i += 1
S(i) = "new String"

Hope that helps,
 
Z

Zordiac

Apologies, Apologies, Apologies, Apologies, Apologies, Apologies!!!!

There is nothing worse than some fool presenting the wrong question
and so wasting the time of those good people who come forward to help.
So here is the real problem -one line change to the original:

s(i)=new string(sTmp) 'and not as I put s(i)=new string(test)
Above line gives - error :
option strict on disallows implicit conversion string to 1-dim array
of char

Thanks for the solution Mark. There is a minor problem with it ie I
had to use Preserve to keep the existing array content. Btw why use -1
and not 0 in dim S(-1) as string as both create a first array entry ie
S(0) with value Nothing that can be populated.

So then (thanks to Mark) this the way to dynamically populate a string
array:

Option Strict On
Dim S() as String
Dim i as integer = -1
while we have got another new string in sTmp
i+=1
Redim Preserve S(i)
S(i) = sTmp
end while

Question : is this the "proper" way of doing this?


ReDim Preseve S(S.Length) should be used.
 
J

Jay B. Harlow [MVP - Outlook]

ZOrdiac,
s(i)=new string(sTmp) 'and not as I put s(i)=new string(test)
Above line gives - error :
option strict on disallows implicit conversion string to 1-dim array
of char
sTmp is already a String, you are attempting to construct a new String based
s(i)=new string(sTmp)
Simply:
s(i)=sTmp

Constructors of String, that are usable from VB.NET are:
Public Sub New(Char())
Public Sub New(Char(), Integer, Integer)
Public Sub New(Char, Integer)

Note that they all expect either a Char or a Char array. Allowing you to
create a new string from an array of char, a subset of an array of char, or
a char repeated a number of times...

Hope this helps
Jay
 
H

Herfried K. Wagner [MVP]

* (e-mail address removed) (Zordiac) scripsit:
array:

Option Strict On
Dim S() as String
Dim i as integer = -1
while we have got another new string in sTmp
i+=1
Redim Preserve S(i)
S(i) = sTmp
end while

'ReDim Preserve' will reduce performance. It's better to prepare a
larger array (you can determine the number of items using a heuristic,
depending on your "while we have dot another new string in sTmp" code,
and then perform a 'ReDim Preserve' once after all strings have been
read. Alternatively, you can store the strings in an arraylist or a
collection.
 

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

Top