473,289 Members | 1,729 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

Using Classes as List Items

I have written a serialized class that has several properties that are
typed as a list of type class. When I deserialize an XML file, the list
is populated just fine. But I am having trouble manually loading the
class for serialization when the class has not been initialized by a
deserialize. If I add three instances of a class to the list, each with
different values for their properties, the XML file created by
serialization has three items for that property, but the values in all
three are the same values, the values of the last class added to the
list. I have tried a test of this that merely loads up a list of
classes in a class that is not being serialized and dump the property
values with the same result. Any ideas?

Sample code:

My custom class to be listed:

Public Class clsItem

Private _Part1 As String
Private _Part2 As String

Public Property Part1() As String
Get
Return _Part1
End Get
Set(ByVal value As String)
_part2 = value
End Set
End Property

Public Property Part2() As String
Get
Return _Part2
End Get
Set(ByVal value As String)
_Part2 = value
End Set
End Property

End Class

Code to load up the list and dump it:

Dim myItem As New clsItem
Dim myItems As New List(Of clsItem)
Dim i As Integer = 1

myItem.Part1 = "This is Part 1 the First Item in the List"
myItem.Part2 = "This is Part 2 the First Item in the List"
myItems.Add(myItem)
myItem.Part1 = "This is Part 1 the Second Item in the List"
myItem.Part2 = "This is Part 2 the Second Item in the List"
myItems.Add(myItem)
myItem.Part1 = "This is Part 1 the Third Item in the List"
myItem.Part2 = "This is Part 2 the Third Item in the List"
myItems.Add(myItem)
For Each myItem In myItems
Debug.WriteLine("Item Number " & CStr(i))
Debug.WriteLine("Part1 = " & myItem.Part1)
Debug.WriteLine("Part2 = " & myItem.Part2)
i += 1
Next

The output:

Item Number 1
Part1 =
Part2 = This is Part 2 the Third Item in the List
Item Number 2
Part1 =
Part2 = This is Part 2 the Third Item in the List
Item Number 3
Part1 =
Part2 = This is Part 2 the Third Item in the List

Notice that the first property called Part1 never gets any value.

Jan 25 '06 #1
9 1503

za***@construction-imaging.com wrote:
I have written a serialized class that has several properties that are
typed as a list of type class. When I deserialize an XML file, the list
is populated just fine. But I am having trouble manually loading the
class for serialization when the class has not been initialized by a
deserialize. If I add three instances of a class to the list, each with
different values for their properties [snip]
Dim myItem As New clsItem
Dim myItems As New List(Of clsItem)
Dim i As Integer = 1

myItem.Part1 = "This is Part 1 the First Item in the List"
myItem.Part2 = "This is Part 2 the First Item in the List"
myItems.Add(myItem)
myItem.Part1 = "This is Part 1 the Second Item in the List"
myItem.Part2 = "This is Part 2 the Second Item in the List"
myItems.Add(myItem)
myItem.Part1 = "This is Part 1 the Third Item in the List"
myItem.Part2 = "This is Part 2 the Third Item in the List"
myItems.Add(myItem)


I only see one instance. You want a myItem to be a New clsItem each
time, before you set its properties and add it to the list.

--
Larry Lard
Replies to group please

Jan 25 '06 #2
Zacks,
As Larry suggests, you only have a single instance of clsItem.

I would define a constructor on clsItem, something like:

Public Sub New(part1 As String, part2 As String)
_part1 = part1
_part2 = part2
End Sub

Then the code to create the list can be reduced to:

Dim myItem As clsItem

myItem = New clsItem("This is Part 1 the First Item in the List",
"This is Part 2 the First Item in the List")
myItems.Add(myItem)
myItem = New clsItem("This is Part 1 the Second Item in the List",
"This is Part 2 the Second Item in the List")
myItems.Add(myItem)
myItem = New clsItem("This is Part 1 the Third Item in the List",
"This is Part 2 the Third Item in the List")
myItems.Add(myItem)
Or even:

myItems.Add(New clsItem("This is Part 1 the First Item in the List",
"This is Part 2 the First Item in the List"))
myItems.Add(New clsItem("This is Part 1 the Second Item in the
List", "This is Part 2 the Second Item in the List"))
myItems.Add(New clsItem("This is Part 1 the Third Item in the List",
"This is Part 2 the Third Item in the List"))
For Each myItem As clsItem In myItems
I would also consider creating a clsItemCollection that inherits from
System.Collections.ObjectModel.Collection(Of T), allowing a parameterized
Add statement

Public Class clsItemCollection
Inherits System.Collections.ObjectModel.Collection(Of clsItem)

Public Overloads Sub Add(ByVal part1 As String, ByVal part2 As
String)
Add(New clsItem(part1, part2))
End Sub

End Class

Giving:

Dim myItems As New clsItemCollection

myItems.Add("This is Part 1 the First Item in the List", "This is
Part 2 the First Item in the List")
myItems.Add("This is Part 1 the Second Item in the List", "This is
Part 2 the Second Item in the List")
myItems.Add("This is Part 1 the Third Item in the List", "This is
Part 2 the Third Item in the List")

Which limits (encapsulates) the multiple calls to the constructor to the
overload Add method, note clsItemCollection also has an Add(clsItem) method.

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
<za***@construction-imaging.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
|I have written a serialized class that has several properties that are
| typed as a list of type class. When I deserialize an XML file, the list
| is populated just fine. But I am having trouble manually loading the
| class for serialization when the class has not been initialized by a
| deserialize. If I add three instances of a class to the list, each with
| different values for their properties, the XML file created by
| serialization has three items for that property, but the values in all
| three are the same values, the values of the last class added to the
| list. I have tried a test of this that merely loads up a list of
| classes in a class that is not being serialized and dump the property
| values with the same result. Any ideas?
|
| Sample code:
|
| My custom class to be listed:
|
| Public Class clsItem
|
| Private _Part1 As String
| Private _Part2 As String
|
| Public Property Part1() As String
| Get
| Return _Part1
| End Get
| Set(ByVal value As String)
| _part2 = value
| End Set
| End Property
|
| Public Property Part2() As String
| Get
| Return _Part2
| End Get
| Set(ByVal value As String)
| _Part2 = value
| End Set
| End Property
|
| End Class
|
| Code to load up the list and dump it:
|
| Dim myItem As New clsItem
| Dim myItems As New List(Of clsItem)
| Dim i As Integer = 1
|
| myItem.Part1 = "This is Part 1 the First Item in the List"
| myItem.Part2 = "This is Part 2 the First Item in the List"
| myItems.Add(myItem)
| myItem.Part1 = "This is Part 1 the Second Item in the List"
| myItem.Part2 = "This is Part 2 the Second Item in the List"
| myItems.Add(myItem)
| myItem.Part1 = "This is Part 1 the Third Item in the List"
| myItem.Part2 = "This is Part 2 the Third Item in the List"
| myItems.Add(myItem)
| For Each myItem In myItems
| Debug.WriteLine("Item Number " & CStr(i))
| Debug.WriteLine("Part1 = " & myItem.Part1)
| Debug.WriteLine("Part2 = " & myItem.Part2)
| i += 1
| Next
|
| The output:
|
| Item Number 1
| Part1 =
| Part2 = This is Part 2 the Third Item in the List
| Item Number 2
| Part1 =
| Part2 = This is Part 2 the Third Item in the List
| Item Number 3
| Part1 =
| Part2 = This is Part 2 the Third Item in the List
|
| Notice that the first property called Part1 never gets any value.
|
Jan 25 '06 #3
Zack,

I am curious, why are you building your own most simple small version of the
inbuilt great class the DataTable? I can assure you that they did think
about that one by Microsoft it seems for me that it has endless
possibilities..

Cor
Jan 25 '06 #4
Zacks,
BTW: I just noticed this in your code...

Debug.WriteLine is overloaded with a Category, you can "simplify" this:

| Debug.WriteLine("Item Number " & CStr(i))
| Debug.WriteLine("Part1 = " & myItem.Part1)
| Debug.WriteLine("Part2 = " & myItem.Part2)

to:

Debug.WriteLine(i, "Item Number")
Debug.WriteLine(myItem.Part1, "Part1")
Debug.WriteLine(myItem.Part2, "Part2")
--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
<za***@construction-imaging.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
|I have written a serialized class that has several properties that are
| typed as a list of type class. When I deserialize an XML file, the list
| is populated just fine. But I am having trouble manually loading the
| class for serialization when the class has not been initialized by a
| deserialize. If I add three instances of a class to the list, each with
| different values for their properties, the XML file created by
| serialization has three items for that property, but the values in all
| three are the same values, the values of the last class added to the
| list. I have tried a test of this that merely loads up a list of
| classes in a class that is not being serialized and dump the property
| values with the same result. Any ideas?
|
| Sample code:
|
| My custom class to be listed:
|
| Public Class clsItem
|
| Private _Part1 As String
| Private _Part2 As String
|
| Public Property Part1() As String
| Get
| Return _Part1
| End Get
| Set(ByVal value As String)
| _part2 = value
| End Set
| End Property
|
| Public Property Part2() As String
| Get
| Return _Part2
| End Get
| Set(ByVal value As String)
| _Part2 = value
| End Set
| End Property
|
| End Class
|
| Code to load up the list and dump it:
|
| Dim myItem As New clsItem
| Dim myItems As New List(Of clsItem)
| Dim i As Integer = 1
|
| myItem.Part1 = "This is Part 1 the First Item in the List"
| myItem.Part2 = "This is Part 2 the First Item in the List"
| myItems.Add(myItem)
| myItem.Part1 = "This is Part 1 the Second Item in the List"
| myItem.Part2 = "This is Part 2 the Second Item in the List"
| myItems.Add(myItem)
| myItem.Part1 = "This is Part 1 the Third Item in the List"
| myItem.Part2 = "This is Part 2 the Third Item in the List"
| myItems.Add(myItem)
| For Each myItem In myItems
| Debug.WriteLine("Item Number " & CStr(i))
| Debug.WriteLine("Part1 = " & myItem.Part1)
| Debug.WriteLine("Part2 = " & myItem.Part2)
| i += 1
| Next
|
| The output:
|
| Item Number 1
| Part1 =
| Part2 = This is Part 2 the Third Item in the List
| Item Number 2
| Part1 =
| Part2 = This is Part 2 the Third Item in the List
| Item Number 3
| Part1 =
| Part2 = This is Part 2 the Third Item in the List
|
| Notice that the first property called Part1 never gets any value.
|
Jan 25 '06 #5
You message came across a little garbled. Try again?

Jan 25 '06 #6
Thanks for all of the great feedback, all if it was useful.

Jan 25 '06 #7
Jay,

Is there a reason why, in the constructor, you assign the parameter values
directly to the private data elements instead of going through the property's
accessor method?

I know that in this particular example the class does not enforce any
business rules in the Set procedures. But I like the habit of always going
through the Set procedures. For example:

Public Sub New(part1 As String, part2 As String)
Me.Part1 = part1
Me.Part2 = part2
End Sub

That way I don't accidentally bypass code that might be added later in the
Set procedures.

What criteria do you use to decide how to assign values from the constructor
to the data elements of the class?

Kerry Moorman
"Jay B. Harlow [MVP - Outlook]" wrote:
I would define a constructor on clsItem, something like:

Public Sub New(part1 As String, part2 As String)
_part1 = part1
_part2 = part2
End Sub


Jan 25 '06 #8
| Is there a reason why, in the constructor, you assign the parameter values
| directly to the private data elements instead of going through the
property's
| accessor method?
Its the convention I've pretty much always used.

| ... But I like the habit of always going
| through the Set procedures. ...
Its a good habit to have, it really depends on the project/solution if I use
the fields directly or use the properties, as some projects/solutions don't
really call for special validation logic in the set methods...

| What criteria do you use to decide how to assign values from the
constructor
| to the data elements of the class?
If the property set routines include special validation logic. Of course if
special validation logic is added as an after thought, then its possible the
constructor will be missed. One would hope Unit Testing would catch these
cases ;-)

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Kerry Moorman" <Ke**********@discussions.microsoft.com> wrote in message
news:95**********************************@microsof t.com...
| Jay,
|
| Is there a reason why, in the constructor, you assign the parameter values
| directly to the private data elements instead of going through the
property's
| accessor method?
|
| I know that in this particular example the class does not enforce any
| business rules in the Set procedures. But I like the habit of always going
| through the Set procedures. For example:
|
| Public Sub New(part1 As String, part2 As String)
| Me.Part1 = part1
| Me.Part2 = part2
| End Sub
|
| That way I don't accidentally bypass code that might be added later in the
| Set procedures.
|
| What criteria do you use to decide how to assign values from the
constructor
| to the data elements of the class?
|
| Kerry Moorman
|
|
| "Jay B. Harlow [MVP - Outlook]" wrote:
|
| > I would define a constructor on clsItem, something like:
| >
| > Public Sub New(part1 As String, part2 As String)
| > _part1 = part1
| > _part2 = part2
| > End Sub
| >
|
Jan 25 '06 #9
I think your Part1 Set Property has a typo it should be writting to _Part1
not _Part2
<za***@construction-imaging.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
I have written a serialized class that has several properties that are
typed as a list of type class. When I deserialize an XML file, the list
is populated just fine. But I am having trouble manually loading the
class for serialization when the class has not been initialized by a
deserialize. If I add three instances of a class to the list, each with
different values for their properties, the XML file created by
serialization has three items for that property, but the values in all
three are the same values, the values of the last class added to the
list. I have tried a test of this that merely loads up a list of
classes in a class that is not being serialized and dump the property
values with the same result. Any ideas?

Sample code:

My custom class to be listed:

Public Class clsItem

Private _Part1 As String
Private _Part2 As String

Public Property Part1() As String
Get
Return _Part1
End Get
Set(ByVal value As String)
_part2 = value
End Set
End Property

Public Property Part2() As String
Get
Return _Part2
End Get
Set(ByVal value As String)
_Part2 = value
End Set
End Property

End Class

Code to load up the list and dump it:

Dim myItem As New clsItem
Dim myItems As New List(Of clsItem)
Dim i As Integer = 1

myItem.Part1 = "This is Part 1 the First Item in the List"
myItem.Part2 = "This is Part 2 the First Item in the List"
myItems.Add(myItem)
myItem.Part1 = "This is Part 1 the Second Item in the List"
myItem.Part2 = "This is Part 2 the Second Item in the List"
myItems.Add(myItem)
myItem.Part1 = "This is Part 1 the Third Item in the List"
myItem.Part2 = "This is Part 2 the Third Item in the List"
myItems.Add(myItem)
For Each myItem In myItems
Debug.WriteLine("Item Number " & CStr(i))
Debug.WriteLine("Part1 = " & myItem.Part1)
Debug.WriteLine("Part2 = " & myItem.Part2)
i += 1
Next

The output:

Item Number 1
Part1 =
Part2 = This is Part 2 the Third Item in the List
Item Number 2
Part1 =
Part2 = This is Part 2 the Third Item in the List
Item Number 3
Part1 =
Part2 = This is Part 2 the Third Item in the List

Notice that the first property called Part1 never gets any value.

Feb 2 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Carlos Ribeiro | last post by:
Hello all, I'm posting this to the list with the intention to form a group of people interested in this type of solution. I'm not going to spam the list with it, unless for occasional and...
2
by: Steve | last post by:
Hi all If there is a more relevant NG for my question about Reflection I would be grateful if some-one could point me in the right direction! I am using the following code to get the Public...
1
by: barnesc | last post by:
Hi again, Since my linear algebra library appears not to serve any practical need (I found cgkit, and that works better for me), I've gotten bored and went back to one of my other projects:...
4
by: JohnR | last post by:
Assume you have a grocery store application. An order consists of an order header with the "customer" info, and multiple "item" records (one for each thing they ordered). Now, in a regular DB...
9
by: friendhouston | last post by:
Hi, Have a requirement where new items may be added later to an existing list of items. These new items may have new features but will need to contain minimum basic features like description and...
2
by: BBM | last post by:
I have the following base class that uses Generics in its definition. MyList(of T, C) - T is a BindingList(Of C), C is the type in the list I have many implemented classes...
4
by: Mani | last post by:
Hi All, Can anyone please tell me how interface is going to help me in real time senarios. As interface contains only skeleton of methos and it has to be inherited to a class. Is there...
4
by: Zerofury | last post by:
Okay here's what i'm trying to do. I want to create an array and fill it with objects, so that when i create a method to alphabetize them, all of the pieces (the price, name, ect) don't get all out...
10
by: AZRebelCowgirl73 | last post by:
This is what I have so far: My program! import java.util.*; import java.lang.*; import java.io.*; import ch06.lists.*; public class UIandDB {
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.