468,463 Members | 2,046 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,463 developers. It's quick & easy.

Populate a string array dynamically

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
Nov 20 '05 #1
11 17276
"Zordiac" <zo*****@hotmail.com> schrieb
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

Nov 20 '05 #2
* zo*****@hotmail.com (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.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #3
Zordiac wrote:
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.
--
The email address listed is not my real address. Please do not send
valid email to that address.
Nov 20 '05 #4
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,
--
Marc Butenko
mb******@bresnan.net
"Zordiac" <zo*****@hotmail.com> wrote in message
news:77**************************@posting.google.c om...
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

Nov 20 '05 #5
Hi Zordiac,

Because you have so many answers, take the one from Marc

Cor
Nov 20 '05 #6
Hi Armin,
I get a different error: variable 'test' not declared.


You have an English VS now?

:-))

Cor
Nov 20 '05 #7
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.
"Marc Butenko" <mb******@state.mt.us> wrote in message news:<eT**************@tk2msftngp13.phx.gbl>...
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,
--
Marc Butenko
mb******@bresnan.net
"Zordiac" <zo*****@hotmail.com> wrote in message
news:77**************************@posting.google.c om...
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

Nov 20 '05 #8
"Cor Ligthert" <no**********@planet.nl> schrieb
Hi Armin,
I get a different error: variable 'test' not declared.


You have an English VS now?

:-))


:-)

--
Armin

Nov 20 '05 #9

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


It is not wrong however using the arraylist is much more proper in my
opinon.

http://msdn.microsoft.com/library/de...classtopic.asp

I hope this helps?

Cor
Nov 20 '05 #10
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
on an existing String (in C++ this would be a copy constructor). However! in
..NET there is no constructor on String that expects a String.

Instead of: 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

"Zordiac" <zo*****@hotmail.com> wrote in message
news:77**************************@posting.google.c om... 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.
"Marc Butenko" <mb******@state.mt.us> wrote in message

news:<eT**************@tk2msftngp13.phx.gbl>...
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,
--
Marc Butenko
mb******@bresnan.net
"Zordiac" <zo*****@hotmail.com> wrote in message
news:77**************************@posting.google.c om...
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

Nov 20 '05 #11
* zo*****@hotmail.com (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.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by George Hester | last post: by
4 posts views Thread by Kevin H | last post: by
2 posts views Thread by George | last post: by
5 posts views Thread by Arpan | last post: by
8 posts views Thread by Jess | last post: by
20 posts views Thread by silverburgh.meryl | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.