Local Variable is Never Used misleading with Initializer

This generates a local variable is never used error which I find misleading because my_rtb is assigned to some_parent_object so it is added to that object and used.

RichTextBox my_rtb = new RichTextBox

                                             {

                                                 Parent = some_parent_object,

                                                 Dock = DockStyle.Fill,

                                                 Name = theGraphContext.sensorName

                                             };




If I do it this way
RichTextBox my_rtb = new RichTextBox
                                             {
                                              // Parent = some_parent_object,
                                                 Dock = DockStyle.Fill,
                                                 Name = theGraphContext.sensorName
                                             };
  my_rtb.Parent = some_parent_object;

I trade the local variable warning for the Use Initializer warning. I imagine this applies to all objects that support the Parent property in this manner.

2 comments

Hello,

Why do you need my_rtb local variable then? What if you write just the "new"
clause?


Serge Baltic
JetBrains, Inc — http://www.jetbrains.com
“Develop with pleasure!”


0

Serge,
Duh.  I just get so used to do things a certain way that  It never occured to me to write a new clause without assigning it. Just another new idiom resulting from initializer syntax.

Thank you very much

Tod

0

Please sign in to leave a comment.