Solved

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

Posted on 2012-04-13
9
259 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
 

Author Comment

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

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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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
 

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
 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 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

19 Experts available now in Live!

Get 1:1 Help Now