basdewaard Posted August 10, 2004 Posted August 10, 2004 Hi This really has me stumped! I'm dynamically adding two labels to my page (or form). I set the properties for the first label and then copy the properties of the frist label to the second by setting the first equal to the second. I don't get the expected results though! dim l1 as new label dim l2 as new label l1.text = "Test1" l2 = l1 me.controls.add(l1) me.controls.add(l2) Why doesn't l2's text property equal that of l1? I thought I had a better understanding of OOP than this! :confused: Bas Quote
Administrators PlausiblyDamp Posted August 10, 2004 Administrators Posted August 10, 2004 l2's text property will equal that of l1 - check in the debugger, however what you are really doing is setting l2 to reference l1 (both variables point to the same area of memory). This means they will also ocupy the same location on the form - it will only ever appear to be a single control (because it is a single control with 2 variables that point to it). you could just assign the test property (also remember to position them differently as well) Dim l1 As New Label Dim l2 As New Label l1.Text = "Test1" l2.Text = l1.Text l1.Location = New Point(100, 100) l2.Location = New Point(150, 150) Me.Controls.Add(l1) Me.Controls.Add(l2) Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
basdewaard Posted August 10, 2004 Author Posted August 10, 2004 Ah of course! I hadn't thought about the fact that it's a reference to the same area of memory. [discussion mode: ON] Since an area of memory is reserved for l2 (from the 'new' instantiation), is it possible to copy that area of memory from l1 into l2's area of memory and then set l2's properties (such as ID) such that l2 is still unique? What I'm trying to achieve is to be able to copy a whole heap of properties from one label and replicate those properties for multiple labels (but in the most efficient way possible). Rather than setting each new label's properties individually? Thanks for your reply, Bas Quote
Administrators PlausiblyDamp Posted August 10, 2004 Administrators Posted August 10, 2004 In your code the original allocation of memory for l2 is never actually used - although allocated it is no longer required after the line l2=l1. Could you not write a method that accepts a label as a parameter and have that method do all the property setup rather than trying to copy the properties from another instance? Would probably be easier in the long run. Failing that you may want to investigate reflection - although this will do what you want there could well be an associated performance hit if you are doing this with many controls / properties. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
basdewaard Posted August 10, 2004 Author Posted August 10, 2004 I'd say that's the way to go. Thanks again for your help (and tips). Bas Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.