Connecting Tech Pros Worldwide Forums | Help | Site Map

vb2005 + -1 Dimensional String error

Member
 
Join Date: Sep 2008
Posts: 74
#1: Sep 17 '08
Hi,

I pretty new to VB2005 i was just working on a project a problem arise which really got me frustrated.

Dim searchThese(1) As String
myvar(0) = "Hi"
myvar(1) = "Bye"

searchfolder(myvar as string)

after defining the above array i was making a function like this:

Function SearchFolder(ByVal myvar As String())
// code here
End Function

when i run the above could i get an error saying "Value of '-1 Dimensional array of string' cannot be converted to string"

i am really confused on why this is happening and how i can resolve this problem any help would be highly appreciated.

Regards

Member
 
Join Date: Sep 2008
Posts: 74
#2: Sep 17 '08

re: vb2005 + -1 Dimensional String error


Bump :(

Any help please :(
PRR PRR is offline
Moderator
 
Join Date: Dec 2007
Location: India
Posts: 702
#3: Sep 17 '08

re: vb2005 + -1 Dimensional String error


string[] ss=new string[1];
// here the size is one ; so you can only have ss[0] and not ss[1];
for that string[] ss=new string[2];
Member
 
Join Date: Sep 2008
Posts: 74
#4: Sep 17 '08

re: vb2005 + -1 Dimensional String error


Hello,

Thanks for your response DirtBag :)

i'm sorry :( but i still cant understand :( sorry n00b here :)

I started an array as
dim var as string(1)

so does that mean i only have one index?

i'm not quite sure, does the index of an array start from 0 or 1 in vb.net?

could you point out exactly how i can fix my code that presented :(

Thanks in advance :)
Regards
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#5: Sep 17 '08

re: vb2005 + -1 Dimensional String error


dim var as string(1)
Would mean that there is only one element in your string array.
Member
 
Join Date: Sep 2008
Posts: 74
#6: Sep 17 '08

re: vb2005 + -1 Dimensional String error


:(

sorry but I'm still confused :(

okay, can you just give me a small code

I have 3 checkboxes on my form

chkBox1 to chkBox 3

what i am trying to accomplish is that i check chkbox1 to see if it is checked.

if its checked i want to add a value to an array (myVar)

i try to do it like this

dim myVar(1) as string

IF chkBox1.checked = true then
myVar() = "Johnny Bravo"
End If

IF chkBox2.checked = true then
myVar() = "Hulk Hogan"
End If

i cant see what is wrong i'm sure its something with myVar()= part but i am not sure - i sort of want this array to be dynamic because i will not have 3 checkboxes i will have about 10 and i only want to insert values for only those which have been Checked.

Please can some one give me a working code for this :(

Thanks for the prompt response guys :)
Expert
 
Join Date: Jun 2008
Location: Pretoria, South Africa
Posts: 410
#7: Sep 17 '08

re: vb2005 + -1 Dimensional String error


An array cannot be addressed like a variable:

myVar() = whatever

is wrong.

you need to supply an index within the array:

myVar(0) = whatever.

I suggest you look at this

or get a text book to help you understand further
Member
 
Join Date: Sep 2008
Posts: 74
#8: Sep 17 '08

re: vb2005 + -1 Dimensional String error


Thanks

i got that..

but can you tell me howto do what i'm trying to accomplish in my last post please

Thanks

Regards,
insertAlias's Avatar
Forum Leader
 
Join Date: Apr 2008
Location: San Antonio, TX (USA)
Posts: 2,608
#9: Sep 17 '08

re: vb2005 + -1 Dimensional String error


If you need a dynamically sized array, you should use a List.

Expand|Select|Wrap|Line Numbers
  1. Dim l As New Generic.List(Of String)
  2. l.Add("string 1")
  3. l.Add("string 2")
  4. Dim s As New String
  5. For Each s In l
  6.   Console.WriteLine(s)
  7. Next
  8.  
You can also access each element like you could an array.
Member
 
Join Date: Sep 2008
Posts: 74
#10: Sep 17 '08

re: vb2005 + -1 Dimensional String error


Quote:

Originally Posted by insertAlias

If you need a dynamically sized array, you should use a List.

Expand|Select|Wrap|Line Numbers
  1. Dim l As New Generic.List(Of String)
  2. l.Add("string 1")
  3. l.Add("string 2")
  4. Dim s As New String
  5. For Each s In l
  6.   Console.WriteLine(s)
  7. Next
  8.  
You can also access each element like you could an array.

Sir, you are too good :)

SUPER HELP! :)

Thanks - i can't tell you how easy it made my work after using your method of List()

Thank you so much :)

Kind Regards,
insertAlias's Avatar
Forum Leader
 
Join Date: Apr 2008
Location: San Antonio, TX (USA)
Posts: 2,608
#11: Sep 17 '08

re: vb2005 + -1 Dimensional String error


Glad I could help =D
Reply