Solved

500pts: Variable already defined in this scope, can't i null var out?

Posted on 2006-06-28
6
272 Views
Last Modified: 2010-04-16
Hi there,

I am using some tablestyles in a form_load event of a winform.... And i wish to use ts, style1, style2 etc etc which variables over and over again for each Datagrid..

Basically I want to know if i can null out the variables? .... so i can use them again... here is my code. Can anybody suggest  a way to null out these variable so i can redeclare them further down ... Thanks in advance..

i.e.

                        DataGridTextBoxColumn style1=new DataGridTextBoxColumn();
                  style1.MappingName="TEST";
                  style1.HeaderText="FIELD1";
                  ts.GridColumnStyles.Add(style1);

                  DataGridTextBoxColumn style2=new DataGridTextBoxColumn();
                  style2.MappingName="TEST2";
                  style2.HeaderText="FIELDS2";
                  ts.GridColumnStyles.Add(style2);

                  uxFirstDataGrid.TableStyles.Add(ts);


//errrors below because TS, STYLE1 etc have already been declared...
                  DataGridTableStyle ts=new DataGridTableStyle();
                  ts.MappingName="Record";
                  
                  DataGridTextBoxColumn style1=new DataGridTextBoxColumn();
                  style1.MappingName="FLIGHT";
                  style1.HeaderText="flight";
                  ts.GridColumnStyles.Add(style1);

                  DataGridTextBoxColumn style2=new DataGridTextBoxColumn();
                  style2.MappingName="NAME";
                  style2.HeaderText="Name";
                  ts.GridColumnStyles.Add(style2);

                  uxIntDataGrid.TableStyles.Add(ts);

0
Comment
Question by:ianinspain
[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
  • 3
  • 3
6 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 16999446
You can reuse the same variables:

               DataGridTextBoxColumn style1=new DataGridTextBoxColumn();
               style1.MappingName="TEST";
               style1.HeaderText="FIELD1";
               ts.GridColumnStyles.Add(style1);

               DataGridTextBoxColumn style2=new DataGridTextBoxColumn();
               style2.MappingName="TEST2";
               style2.HeaderText="FIELDS2";
               ts.GridColumnStyles.Add(style2);

               uxFirstDataGrid.TableStyles.Add(ts);


               DataGridTableStyle ts=new DataGridTableStyle();
               ts.MappingName="Record";
               
               style1=new DataGridTextBoxColumn();
               style1.MappingName="FLIGHT";
               style1.HeaderText="flight";
               ts.GridColumnStyles.Add(style1);

               style2=new DataGridTextBoxColumn();
               style2.MappingName="NAME";
               style2.HeaderText="Name";
               ts.GridColumnStyles.Add(style2);

               uxIntDataGrid.TableStyles.Add(ts);
0
 

Author Comment

by:ianinspain
ID: 16999634
Yes this is true..... but in some situations i have a lot of properties i have set in there... and sometimes it might be better to clear them and start again....

I just was wondering if this is possible?

Thanks in advance

Ian
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 500 total points
ID: 16999730
style1=new DataGridTextBoxColumn();

This line creates new instance of DataGridTextBoxColumn in it's default state. Properties set to previous instance are not applied to this new instance. It works exactly as you want.

If you wonder how it works, .NET reference types have reference counters. When instance is created, it's reference counter is set to 1. When reference is assigned to another variable, counter is incremented. When variable goes out of scope or set to null, cointer is decremented. When counter is set to 0, object is not available in code and will be destroyed later by Garbage Collector.
Specifically, in your code:

DataGridTextBoxColumn style1=new DataGridTextBoxColumn();   // instance A created, counter = 1
style1.MappingName="TEST";              // set properties of instance A
style1.HeaderText="FIELD1";
ts.GridColumnStyles.Add(style1);          // instance A is passed to Add method which keeps it, counter = 2
...

// Instance B created, counter = 1. Since variable is reused, instance A counter is decremented. Now it is 1
// and kept somewhere in GridColumnStyles collection. We continue to work with completely new instance B.
style1=new DataGridTextBoxColumn();  

style1.MappingName="FLIGHT";             // set properties of instance B
style1.HeaderText="flight";
ts.GridColumnStyles.Add(style1);          // instance B is passed to Add method which keeps it, counter = 2
...
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ianinspain
ID: 17000023
Cool!! thanks alot!! Thats really helped.. Assigning points now..

p.s. at the beginning of my code, could i do

                  DataGridTextBoxColumn test1,test2,test3,test4,test5,test6,test7;
I reserve the right to use test1 - test7 ... only declared not assigned... then i can assign them in my code

But do you know what effect this would have with memory by delcaring variables..   because i may not use them...

cheers

ian
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 17000080
Compiler shows warning for every unused variable, so you can remove them.
When procedure is called, it allocates space for every variable. Variable that is not used takes some memory, but unassigned reference is just pointer (32 bits for Win32 OS), there is no problem.
0
 

Author Comment

by:ianinspain
ID: 17000367
thanks :-)

Ian
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

696 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