Solved

Working with clonable objects in a generic.list(Of T)

Posted on 2012-04-13
9
261 Views
Last Modified: 2012-04-17
I am working with an object that I have implemented the ICloneable interface to and that works just fine.  However I would like to add it to a collection, normally I use the generic.list(Of T).  However in this case it is not a a happy bunny.  I suspect I am using the wrong collection type for the task.

I have provided an example-ish bit of code.  I am currently doing a brief proof of concept and then what I have learnt will be applied to a change to a live application.  A sort of measure twice and cut once approach to the problem.
_______________________________________________________________
'This is a sample class not the actual
Public Class Address
    Implements ICloneable
    Public Property CompanyName As String = String.Empty
    Public Property PropertyName As String = String.Empty
    Public Property AddressLn1 As String = String.Empty
    Public Property AddressLn2 As String = String.Empty
    Public Property AddressLn3 As String = String.Empty
    Public Property City As String = String.Empty
    Public Property County As String = String.Empty
    Public Property Postcode As String = String.Empty
    Public Property country As String = String.Empty
    Public Event Cloned(ByVal Success as boolean)

    Public Function Clone() As Object Implements System.ICloneable.Clone
        Return Me.MemberwiseClone
        RaiseEvent Cloned(True)
    End Function
End Class

_______________________________________________________________

public Class Form1
   Private Addresslist As Generic.List(Of Address)

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
  Addresslist.Add(New Address{.CompanyName =".......})
End sub
0
Comment
Question by:Alyanto
  • 5
  • 2
9 Comments
 
LVL 38

Accepted Solution

by:
PaulHews earned 500 total points
ID: 37842611
There is no problem adding such an object to a generic list.  You have to instantiate the list and properly initialize the object however:

Public Class Form1
    Private Addresslist As New Generic.List(Of Address)

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Addresslist.Add(New Address With {.CompanyName = "......."})
    End Sub

End Class

Open in new window

0
 
LVL 1

Author Comment

by:Alyanto
ID: 37842621
I just spotted it my self b ut for your troubles tyake the points
0
 
LVL 1

Author Comment

by:Alyanto
ID: 37842634
As I said I spotted the missing New as I flicked back to my code doh!   So for the trouble .....
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 1

Author Comment

by:Alyanto
ID: 37852218
Err excuse me but I believe that I have given the points, which is why I ended the sentence with for your trouble indicating I was giving them anyway.  An apology I believe is ion order from the administrator.
0
 
LVL 1

Author Comment

by:Alyanto
ID: 37852230
And again I have attempted to give the points a second time to the best of my knowledge.
0
 
LVL 1

Author Comment

by:Alyanto
ID: 37852277
Dear Paul
I wish to thank you for a very quick response to an immensely stupid oversight on my part.  I do believe that you are a skilled professional which is why I would not quibble over the 500 points especially with the speed of your response.  But ultimately it was a minor correction and that is how I see it.  I can't justify a higher mark and be fair to the next problem I assign a I mark to.  It is not a negative reflection of you, rather of the trivial nature of the question which I hope I have been clear on is wholly my fault.

Kind regards


Alyanto
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 37852766
Alyanto,

You seem to think that you are doing me a favor, begrudging me the points.  Let's invert that perspective.   As a volunteer expert, I did you a favor by answering your question, quickly, correctly and without knowing you were solving it yourself.  For my efforts I received a slap in the face: C grade, the lowest possible grade, indicating that my efforts are severely unappreciated.  

Do I care about the points?  Not very much.  Vee_Mod can PAQ the question with zero points for me, and we will be done with it.  The issue for me was the discourtesy.  While that's probably unintentional, if I didn't mention it, you probably would never know about it.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to remove double quotes from a string in VB.Net 5 47
Need a starter for ETL protocol? 4 57
Regex validation 2 25
Write to a printer using vb.net 9 30
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question