|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
problem reading array data from structureI 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 I think you need to change in LoadListBoxes() the line:
Dim xfolder(i) As FolderID to Redim xfolder(i-1) -- Show quoteHide quoteCo wrote: > 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 Co wrote:
> Hi All, You declare and fill a local array in Sub LoadListBoxes. Use ReDim (without > Public xFolder() As FolderID > Private Sub LoadListBoxes() > Dim xfolder(i) As FolderID > What am I doing wrong? specifying a data type) instead. Armin On 20 mei, 22:40, "Armin Zingler" <az.nos***@freenet.de> wrote: Thanks guys,> 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 that was exactly the problem. Marco 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 On 21 mei, 05:58, "Cor Ligthert[MVP]" <Notmyfirstn***@planet.nl> What's your suggesting in this case then?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 Marco Cor Ligthert[MVP] wrote:
> Armin, I don't know. I don't see the problem. What does "redim on the main stack"> > Using a redim on the main stack. Is that not a little bit very much > from before 1989? > > Or do I miss something? 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 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
Show quote
Hide quote
On 21 mei, 15:06, "Cor Ligthert[MVP]" <Notmyfirstn***@planet.nl> So please come up with an alternative then.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 MArco Marco wrote, questining Cor Lightert:
[Cor Lightert] > > You are right that the old array, when a new one is created by the Redim If the only place where you change the array size is inside> > 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. 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. 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
When double precision isn't very precise
The process cannot access the file (in Windows2003 only) Windows 7 dll import problem unable to remove items from Listbox Controls not rendering Debugging a ClickOnce web-deployed app newbie setup question print as user? Can not find Microsoft.Jet.OLEDB.4.0 importing data from excel sheet to datagridView |
|||||||||||||||||||||||