?
Solved

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

Posted on 2006-06-28
6
Medium Priority
?
274 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 2000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Industry Leaders: 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!

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

762 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