3 Replies - 892 Views - Last Post: 13 February 2012 - 03:54 PM Rate Topic: -----

#1 thecoswen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 13-February 12

Adding to an established Array with a Structure

Posted 13 February 2012 - 06:41 AM

Hello!,

I am trying to be able to add to an established Array that is read in from a CSV file. I'm using structures and I get the concept, but I just can't seem to get it to store. I am using Visual Basic 2010. Sorry for posting in here, but I did not see a category for it.

Thanks for any input in advance,

Scott

Public Class Form1
    Structure AddArray
        Dim slot As String
        Dim slot2 As String
    End Structure
    Dim Lines() As String = IO.File.ReadAllLines("C:\VB2010\Test.txt")
    Dim Add(Lines.Count - 1) As AddArray
    Dim parts() As String

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        ReDim Preserve Add(Lines.Count - 1)
        For j As Integer = 0 To Lines.Count - 1
            Dim test, test2 As AddArray
            test.slot = InputBox("test")
            test2.slot2 = InputBox("test2")
            Return
        Next


    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        For i As Integer = 0 To Lines.Count - 1
            parts = Lines(i).Split(","c)
            Add(i).slot = parts(0)
            Add(i).slot2 = parts(1)

        Next

    End Sub

  
    Private Sub btnDis_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDis.Click
        Dim query = From t In Add
                    Order By t.slot, t.slot2
                    Select t.slot, t.slot2

        lsBox.DataSource = query.ToList()

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        End

    End Sub


End Class



Is This A Good Question/Topic? 0
  • +

Replies To: Adding to an established Array with a Structure

#2 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1057
  • View blog
  • Posts: 4,097
  • Joined: 02-July 08

Re: Adding to an established Array with a Structure

Posted 13 February 2012 - 07:11 AM

Time to leave arrays in the dust. Move on to List(Of T) T being the type and it is like an array, but no ReDim'ing here.

'creation - class level
Dim AddList As New List(Of AddArray)


'add button
Dim ar As New AddArray
ar.slot = InputBox("slot")
ar.slot2 = InputBox("slot2")
AddList.Add(ar)



You where creating two structures and not filling both the slot and slot2 for both.

This post has been edited by _HAWK_: 13 February 2012 - 07:44 AM

Was This Post Helpful? 0
  • +
  • -

#3 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Adding to an established Array with a Structure

Posted 13 February 2012 - 07:24 AM

Also, if you're wanting an input box for each line the return will only allow that loop to work once. When the return is processed, it leaves the loop.

And it appears you need to move your Dim for the AddArray in the btnAdd event outside the loop.
Was This Post Helpful? 0
  • +
  • -

#4 thecoswen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 13-February 12

Re: Adding to an established Array with a Structure

Posted 13 February 2012 - 03:54 PM

Thank you so much for the quick reply and input.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1