Solved

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

Posted on 2012-04-13
9
265 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

635 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