OneNation
OneNation

Reputation: 417

Wrongly use Arrays in VB - Not sure how to solve

It's been such a long time since i touch vb.net and I am having a problem.. it should be a simple one but I am lost. I want to create a loop string but before that I am trying to learn how to use the string array.

The following code is what I have but there is always error at the line g(1,0)=t It is not an object instance. How can this be done?

Code:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim g(,) As String
        Dim t As String = "ok"
        g(1, 0) = t
        MsgBox(g(1, 0))
    End Sub

Upvotes: 0

Views: 96

Answers (3)

Zeddy
Zeddy

Reputation: 2089

Use the REDIM statement (re-dimension) before you attempt to re-dimension (change the size) of your array.

something like

REDIM g(10, 10) 

Upvotes: 1

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67207

Try this,

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim g(,) As String= {{"Hai","Hello"},{"ok","then"}}
        MsgBox(g(1, 0))
    End Sub

For more information refer this.

EDIT:

You can traverse your array like this

   Dim g(1,1) As String

   for i as integer=0 to 1
      for j as integer=0 to 1
         g(i,j)="Your text"
      next
   next

Upvotes: 1

Inisheer
Inisheer

Reputation: 20794

You need to give your array a size. For example:

Dim g(100, 100) As String

http://msdn.microsoft.com/en-us/library/vstudio/wak0wfyt.aspx#BKMK_CreatingAnArray

Upvotes: 1

Related Questions