Solved

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

Posted on 2006-06-28
6
267 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:ianinspain
Comment Utility
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
Comment Utility
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
Comment Utility
thanks :-)

Ian
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now