473,385 Members | 1,292 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 17610
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: George Hester | last post by:
What's this? IE has a simple way using an ActiveX object so that I can load a text file into the <OPTION>s like a database. It is called Tabular Control. But I doubt this is going to work with...
4
by: Kevin H | last post by:
Apologies in advance if this sounds slow-witted, but I didn't find it here. Need to populate some textboxes on a form. While I could hard code it (the number of options aren't that high), it...
2
by: George | last post by:
Is there a fast way to transfer an Excel range to an array? Example: Excel range is E2:E300 Dim person() as string Thanks, George
5
by: Arpan | last post by:
In order to populate any server control with data dynamically, is it ALWAYS NECESSARY to either BIND the DataSource to that server control or call the DataBind method of that server control? For...
1
by: vj | last post by:
How i can populate all fileds dynamically in jsp page based on contents found in xml file? I have written jsp servlets and java class file. i transferred automatic data from jsp to servlet then to...
0
by: vijendra | last post by:
How i can populate all fileds dynamically in jsp page based on contents found in xml file?I have written jsp servlets and java class file. i transferred automatic data from jsp to servlet then to...
3
by: rn5a | last post by:
A SqlDataReader is populated with the records from a SQL Server 2005 DB table. The records retrieved depends upon 2 conditions (the conditions depend on what a user selects in an ASPX page). If...
8
by: Jess | last post by:
Hello, I'm doing an exercise that defines a new abstract class "Str" that has the same functions as "string" class, which holds a vector of chars. But since I'm also trying to define a "c_str()"...
20
by: silverburgh.meryl | last post by:
In my code, I have an array of char* pointer which is populated statically: void function1() { char *ppsz_argv2 = { "abc" , "def", "dummy"}; //... }
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.