problem reading array data from structure


C

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
 
Ad

Advertisements

M

Mike

I think you need to change in LoadListBoxes() the line:

Dim xfolder(i) As FolderID

to

Redim xfolder(i-1)
 
M

Mike

I think you need to change in LoadListBoxes() the line:

Dim xfolder(i) As FolderID

to

Redim xfolder(i-1)
 
A

Armin Zingler

Co said:
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
 
A

Armin Zingler

Co said:
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
 
C

Co

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
 
Ad

Advertisements

C

Co

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
 
C

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
 
C

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
 
C

Co

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
 
C

Co

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
 
Ad

Advertisements

A

Armin Zingler

Cor said:
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
 
A

Armin Zingler

Cor said:
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
 
C

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
 
C

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
 
C

Co

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
 
Ad

Advertisements

C

Co

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
 
B

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.
 
B

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.
 
Ad

Advertisements

C

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
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top