vb2005 + -1 Dimensional String error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zubair1
    New Member
    • Sep 2008
    • 79

    #1

    vb2005 + -1 Dimensional String error

    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(my var as string)

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

    Function SearchFolder(By Val 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
  • zubair1
    New Member
    • Sep 2008
    • 79

    #2
    Bump :(

    Any help please :(

    Comment

    • PRR
      Recognized Expert Contributor
      • Dec 2007
      • 750

      #3
      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];

      Comment

      • zubair1
        New Member
        • Sep 2008
        • 79

        #4
        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

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          dim var as string(1)
          Would mean that there is only one element in your string array.

          Comment

          • zubair1
            New Member
            • Sep 2008
            • 79

            #6
            :(

            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 :)

            Comment

            • cloud255
              Recognized Expert Contributor
              • Jun 2008
              • 427

              #7
              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

              Comment

              • zubair1
                New Member
                • Sep 2008
                • 79

                #8
                Thanks

                i got that..

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

                Thanks

                Regards,

                Comment

                • Curtis Rutland
                  Recognized Expert Specialist
                  • Apr 2008
                  • 3264

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

                  Code:
                  Dim l As New Generic.List(Of String)
                  l.Add("string 1")
                  l.Add("string 2")
                  Dim s As New String
                  For Each s In l
                    Console.WriteLine(s)
                  Next
                  You can also access each element like you could an array.

                  Comment

                  • zubair1
                    New Member
                    • Sep 2008
                    • 79

                    #10
                    Originally posted by insertAlias
                    If you need a dynamically sized array, you should use a List.

                    Code:
                    Dim l As New Generic.List(Of String)
                    l.Add("string 1")
                    l.Add("string 2")
                    Dim s As New String
                    For Each s In l
                      Console.WriteLine(s)
                    Next
                    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,

                    Comment

                    • Curtis Rutland
                      Recognized Expert Specialist
                      • Apr 2008
                      • 3264

                      #11
                      Glad I could help =D

                      Comment

                      Working...