Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 275
  • Last Modified:

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

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
Alyanto
Asked:
Alyanto
  • 5
  • 2
1 Solution
 
PaulHewsCommented:
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
 
AlyantoAuthor Commented:
I just spotted it my self b ut for your troubles tyake the points
0
 
AlyantoAuthor Commented:
As I said I spotted the missing New as I flicked back to my code doh!   So for the trouble .....
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
AlyantoAuthor Commented:
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
 
AlyantoAuthor Commented:
And again I have attempted to give the points a second time to the best of my knowledge.
0
 
AlyantoAuthor Commented:
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
 
PaulHewsCommented:
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.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now