Home All Groups Group Topic Archive Search About

problem reading array data from structure

Author
20 May 2009 6:46 PM
Co
Hi All,

I created a Structure

Public Structure FolderID

        Private FolderID As Integer
        Private FolderName As String

        Public Sub New(ByVal myFolderID As Integer, ByVal myFolderName
As Integer)
            FolderID = myFolderID
            FolderName = myFolderName
        End Sub

        Public Property FolderIDValue() As Integer
            Get
                Return FolderID
            End Get
            Set(ByVal Value As Integer)
                FolderID = Value
            End Set
        End Property

        Public Property FolderNameValue() As String
            Get
                Return FolderName
            End Get
            Set(ByVal Value As String)
                FolderName = Value
            End Set
        End Property

        Public Overrides Function ToString() As String
            Return [String].Format("{0}, {1}", FolderID, FolderName)
        End Function
    End Structure

Now I want to fill this with data from a Listbox.

Public xFolder() As FolderID

Private Sub LoadListBoxes()

        Dim sql As String = "SELECT * FROM Kabinet"
        Dim strTable As String = "Kabinet"
        Dim da As New OleDb.OleDbDataAdapter(sql, conn)
        Dim ds As New DataSet
        conn.Open()
        Try
            da.SelectCommand = New OleDb.OleDbCommand(sql, conn)
            da.Fill(ds, strTable)

            Dim dr As DataRow
            Dim i As Integer = ds.Tables(0).Rows.Count - 1
            Dim j As Integer = 0
            Dim xfolder(i) As FolderID
            For Each dr In ds.Tables(0).Rows
                ListBox1.Items.Add(dr.Item("foldername"))
                xFolder(j).FolderIDValue = dr.Item("Id")
                xfolder(j).FolderNameValue = dr.Item("foldername")
                j += 1
            Next

        Catch oException As OleDbException
            MessageBox.Show(oException.Message)

        Catch oException As Exception
            MessageBox.Show(oException.Message)

        End Try
        conn.Close()

    End Sub

Now from a function I want to read data from the array but it's empty:

Private Function GetRightFolderID(ByVal sListValue As String) As
String

        Dim e As FolderID
        For Each e In xFolder
            If sListValue = e.FolderNameValue Then
                Return e.FolderIDValue
            End If
        Next e
        Return Nothing

    End Function

What am I doing wrong?

Regards
Marco
The Netherlands

Author
20 May 2009 8:16 PM
Mike
I think you need to change in LoadListBoxes() the line:

        Dim xfolder(i) As FolderID

to

        Redim xfolder(i-1)

--


Co wrote:
Show quoteHide quote
> Hi All,
>
> I created a Structure
>
> Public Structure FolderID
>
>         Private FolderID As Integer
>         Private FolderName As String
>
>         Public Sub New(ByVal myFolderID As Integer, ByVal myFolderName
> As Integer)
>             FolderID = myFolderID
>             FolderName = myFolderName
>         End Sub
>
>         Public Property FolderIDValue() As Integer
>             Get
>                 Return FolderID
>             End Get
>             Set(ByVal Value As Integer)
>                 FolderID = Value
>             End Set
>         End Property
>
>         Public Property FolderNameValue() As String
>             Get
>                 Return FolderName
>             End Get
>             Set(ByVal Value As String)
>                 FolderName = Value
>             End Set
>         End Property
>
>         Public Overrides Function ToString() As String
>             Return [String].Format("{0}, {1}", FolderID, FolderName)
>         End Function
>     End Structure
>
> Now I want to fill this with data from a Listbox.
>
> Public xFolder() As FolderID
>
> Private Sub LoadListBoxes()
>
>         Dim sql As String = "SELECT * FROM Kabinet"
>         Dim strTable As String = "Kabinet"
>         Dim da As New OleDb.OleDbDataAdapter(sql, conn)
>         Dim ds As New DataSet
>         conn.Open()
>         Try
>             da.SelectCommand = New OleDb.OleDbCommand(sql, conn)
>             da.Fill(ds, strTable)
>
>             Dim dr As DataRow
>             Dim i As Integer = ds.Tables(0).Rows.Count - 1
>             Dim j As Integer = 0
>             Dim xfolder(i) As FolderID
>             For Each dr In ds.Tables(0).Rows
>                 ListBox1.Items.Add(dr.Item("foldername"))
>                 xFolder(j).FolderIDValue = dr.Item("Id")
>                 xfolder(j).FolderNameValue = dr.Item("foldername")
>                 j += 1
>             Next
>
>         Catch oException As OleDbException
>             MessageBox.Show(oException.Message)
>
>         Catch oException As Exception
>             MessageBox.Show(oException.Message)
>
>         End Try
>         conn.Close()
>
>     End Sub
>
> Now from a function I want to read data from the array but it's empty:
>
> Private Function GetRightFolderID(ByVal sListValue As String) As
> String
>
>         Dim e As FolderID
>         For Each e In xFolder
>             If sListValue = e.FolderNameValue Then
>                 Return e.FolderIDValue
>             End If
>         Next e
>         Return Nothing
>
>     End Function
>
> What am I doing wrong?
>
> Regards
> Marco
> The Netherlands
Author
20 May 2009 8:40 PM
Armin Zingler
Co wrote:
> Hi All,

> Public xFolder() As FolderID


> Private Sub LoadListBoxes()


>            Dim xfolder(i) As FolderID


> What am I doing wrong?

You declare and fill a local array in Sub LoadListBoxes. Use ReDim (without
specifying a data type) instead.


Armin
Author
20 May 2009 10:34 PM
Co
On 20 mei, 22:40, "Armin Zingler" <az.nos***@freenet.de> wrote:
> Co wrote:
> > Hi All,
> > Public xFolder() As FolderID
> > Private Sub LoadListBoxes()
> >            Dim xfolder(i) As FolderID
> > What am I doing wrong?
>
> You declare and fill a local array in Sub LoadListBoxes. Use ReDim (without
> specifying a data type) instead.
>
> Armin

Thanks guys,

that was exactly the problem.

Marco
Author
21 May 2009 3:58 AM
Cor Ligthert[MVP]
Armin,

Using a redim on the main stack. Is that not a little bit very much from
before 1989?

Or do I miss something?

Cor
Author
21 May 2009 7:05 AM
Co
On 21 mei, 05:58, "Cor Ligthert[MVP]" <Notmyfirstn***@planet.nl>
wrote:
> Armin,
>
> Using a redim on the main stack. Is that not a little bit very much from
> before 1989?
>
> Or do I miss something?
>
> Cor

What's your suggesting in this case then?

Marco
Author
21 May 2009 10:08 AM
Armin Zingler
Cor Ligthert[MVP] wrote:
> Armin,
>
> Using a redim on the main stack. Is that not a little bit very much
> from before 1989?
>
> Or do I miss something?

I don't know. I don't see the problem. What does "redim on the main stack"
mean? I only know one stack (per Thread), not a main stack. Redim creates an
array on the GC heap but not on the stack because it's a reference type
(as you know). In addition, a class field is holding the reference, and the
class object is also on the heap. Maybe I miss something now.


Armin
Author
21 May 2009 1:06 PM
Cor Ligthert[MVP]
Armin,

You are right that the old array, when a new one is created by the Redim
created goes every time out of scope.

I was a little bit confused because a struct was used and thought that the
array was situated in the struct itself.

However even then I don't like the solution of dynamic arrays which are
every time new created.

Cor
Author
21 May 2009 1:13 PM
Co
Show quote Hide quote
On 21 mei, 15:06, "Cor Ligthert[MVP]" <Notmyfirstn***@planet.nl>
wrote:
> Armin,
>
> You are right that the old array, when a new one is created by the Redim
> created goes every time out of scope.
>
> I was a little bit confused because a struct was used and thought that the
> array was situated in the struct itself.
>
> However even then I don't like the solution of dynamic arrays which are
> every time new created.
>
> Cor

So please come up with an alternative then.

MArco
Author
21 May 2009 3:49 PM
Branco
Marco wrote, questining Cor Lightert:
[Cor Lightert]
> > You are right that the old array, when a new one is created by the Redim
> > created goes every time out of scope.
>
> > I was a little bit confused because a struct was used and thought that the
> > array was situated in the struct itself.
>
> > However even then I don't like the solution of dynamic arrays which are
> > every time new created.
[Marco]
> So please come up with an alternative then.

If the only place where you change the array size is inside
LoadListBoxes (where the array is just recreated to the appropriate
size using Redim), then using an array seems to be a perfect choice,
to me.

But if you resize the array (adding or removing items) between calls
to LoadListBoxes, then maybe a generic list -- List(Of ForderID) --
would be more appropriate.

Best regards,

Branco.
Author
21 May 2009 7:01 PM
Cor Ligthert[MVP]
I think you can better use like Bronco said  a List(Of FolderID)

Although I probably would not use that either, but simply use two listboxes
with as datasource two dataviews with different filters.

Cor





Show quoteHide quote
"Co" <vonclausow***@gmail.com> wrote in message
news:51508420-0470-44d5-8e83-86c80f87b203@l32g2000vba.googlegroups.com...
> On 21 mei, 15:06, "Cor Ligthert[MVP]" <Notmyfirstn***@planet.nl>
> wrote:
>> Armin,
>>
>> You are right that the old array, when a new one is created by the Redim
>> created goes every time out of scope.
>>
>> I was a little bit confused because a struct was used and thought that
>> the
>> array was situated in the struct itself.
>>
>> However even then I don't like the solution of dynamic arrays which are
>> every time new created.
>>
>> Cor
>
> So please come up with an alternative then.
>
> MArco