Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2006-06-28
6
Medium Priority
?
276 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

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!

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

618 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