472,971 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,971 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 17565
"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"}; //... }
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
3
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.