-2

I have a function that returns an object as Label for the sake of understanding let's call it "lblStatus".

public Label statusUpdater(int x)
{
    Label lblStatus = new Label();
    if (x==1)
    { 
        lblStatus.text = "I like Cheese!";
    }
    else
    {
        lblStatus.text = "And I don't care!";
    }        
    return lblStatus;
}
label1 = myclass.statusUpdater(1);

Would this be possible?

All I really need is to give all properties from a Label to another.
Not like this (label1 exists in designer)

zx485
  • 28,498
  • 28
  • 50
  • 59
PeterKima
  • 43
  • 1
  • 10
  • `x=1` will *assign*, it won't test logic. – tadman Mar 13 '17 at 16:53
  • @tadman thanks, corrected it. – PeterKima Mar 13 '17 at 16:55
  • 3
    why not test it with debugger and see? – Ousmane D. Mar 13 '17 at 16:55
  • So are you wanting to create another `Label` with text identical to an existing one, or are you trying to generate an exact copy in every regard? Or are you just trying to create an entirely new Label with some default values? – Abion47 Mar 13 '17 at 16:55
  • @OusmaneMahyDiaw i did it and didn't worked. No error but no output as well. That is why i came here – PeterKima Mar 13 '17 at 16:56
  • Is `label1` an existing label of which you want to change certain attributes? See [It is possible to copy all the properties of a certain control? (C# window forms)](http://stackoverflow.com/q/3473597/669576) – 001 Mar 13 '17 at 16:57
  • @Abion47 label1 exists in designer i want it to clone the label it is receiving. Not only text but all properties. – PeterKima Mar 13 '17 at 16:58
  • @JohnnyMopp yes I added that to the question now sorry – PeterKima Mar 13 '17 at 16:59
  • A control to be visible needs to be added to the Forms Controls collection. You are just creating a new Label and then? – Steve Mar 13 '17 at 17:00
  • @Steve but the label I wish to display Is visible, it exists in the designer. – PeterKima Mar 13 '17 at 17:01
  • You create a NEW label. Then assign some values from the existing label to the NEW one. This new label (an new instance of a Label class) is not visible – Steve Mar 13 '17 at 17:04
  • @zx485 I don't think I know – PeterKima Mar 13 '17 at 17:08
  • @zx485 I could state it is a deep copy that i was talking about but doesn't seem right because at the time I didn't knew that. Was the answer that told me – PeterKima Mar 13 '17 at 17:21

1 Answers1

3

What you are describing is known as a "Deep Copy". There are several ways to accomplish this that range in the amount of technical wizardry involved, but for your case, I would suggest keeping it simple and just using a helper method to duplicate all the properties you care about:

public static Label CopyLabel(Label label)
{
    Label l = new Label();
    l.Left = label.Left;
    l.Top = label.Top;
    l.Right = label.Right;
    l.Bottom = label.Bottom;
    l.Width = label.Width;
    l.Height = label.Height;
    l.Margin = label.Margin;
    l.Text = label.Text;
    // Add whatever other properties you deem important

    return l;
}

And call it like so:

Label newLabel = CopyLabel(label1);

(If you really do want to perform a true deep copy, then you can check out existing answers here and here.)

Community
  • 1
  • 1
Abion47
  • 22,211
  • 4
  • 65
  • 88
  • I am sorry could you explain the code? I fail to understand it – PeterKima Mar 13 '17 at 17:27
  • @PedroLima What it does is creates a new `Label` object and then copies the properties from the label that you pass (i.e. `label1`) into the new `Label` before returning it as a completely new object. – Abion47 Mar 13 '17 at 17:33
  • But doesn't Label newLabel = CopyLabel(label1); Read as label = label? – PeterKima Mar 13 '17 at 18:19
  • __No__, it doesn't! It assigns a __newly created__ label with all the same properties. – TaW Mar 13 '17 at 18:42
  • @PedroLima the variable "newLabel" is a reference to the returned object from the method "CopyLabel". If you still don't understand there are many free courses and books online that talk about objects. – Ousmane D. Mar 13 '17 at 18:44
  • @PedroLima `Label` is a **type**. `newLabel` is an **instance**. `label1` is an **instance**. `newLabel` and `label1` are the same **type** but are different **instances**. Would you say that a candy in your left hand and a candy in your right hand are *the same piece of candy?* Or would you say that they were *different pieces of candy that are the same type of candy?* – Abion47 Mar 13 '17 at 22:07
  • @Abion47 no but that analogy doesn't cover this case, maybe haven't made my question clear. CopyLabel returns a label right? So using logic how does this **Label newLabel = CopyLabel(label1)** differ from **Label newLabel = labelx** ? for the negative reputation i am getting this must be really easy I just can't seem to grasp... – PeterKima Mar 14 '17 at 09:23
  • @PedroLima Actually, the analogy holds just fine. It just seems like you don't understand what a reference type is and how it behaves. Look at what `CopyLabel` does. It creates an entirely new Label object, and the new object is what gets returned. With `Label newLabel = label1` you aren't copying anything. You are saying that `newLabel` and `label1` are the exact same object, and anything you do to one will be reflected on the other. For a more in-depth explanation, go find a tutorial on reference types. – Abion47 Mar 14 '17 at 13:16
  • @PedroLima To tie it to the analogy, `Label newLabel = CopyLabel(label1)` is like having two candies with the same traits, whereas `Label newLabel = label1` is like having a single piece of candy that you refer to using two different names. – Abion47 Mar 14 '17 at 13:20