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
Solved

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

Posted on 2006-06-28
6
270 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
  • 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

860 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