Solved

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

Posted on 2012-04-13
9
260 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
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 …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now