|
-
Nov 25th, 2007, 09:20 PM
#1
Thread Starter
Fanatic Member
Declaring Variable
HI guys! is there any best practice in declaring variable in C#.net? Is it more efficient to declare all the variables that you will be using in your functions just outside it or in a place where when the application starts all the variable declared will be executed/initialized? Thanks in advance!
-
Nov 25th, 2007, 09:23 PM
#2
Re: Declaring Variable
You declare them in the scope that they will be used in.
From widest to narrowset: You have app wide public variables, class/form level variables, procedural level vars and code block level.
You wouldnt have all vars as app wide public.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Nov 25th, 2007, 09:43 PM
#3
Thread Starter
Fanatic Member
Re: Declaring Variable
thanks a lot for that RobDog..But what if for example i have a function func and in that function i declare & Inialize several variables and that function wil be called several time s and so those variable will also be declare & Inialize several times..Isn't inefficient? Thanks in advance!
-
Nov 25th, 2007, 10:27 PM
#4
Re: Declaring Variable
Declaring a local variable is about the smallest amount of work you can do. If a variable is used only in a single method and its value is not supposed to persist between calls then the variable should be declared locally. If it's value must be persisted then it must be declared at the class level.
-
Nov 26th, 2007, 12:33 AM
#5
Re: Declaring Variable
Yes, it sounds like you have some procedural variables. So since the scope is only in the procedure its most efficient to initialize them within their scope. They dont retain their state between calls like John mentioned so there is no point in expanding their scope to persist outside the procedure.
Edit: 47,000 Posts!
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Nov 26th, 2007, 12:42 AM
#6
Re: Declaring Variable
 Originally Posted by RobDog888
Edit: 47,000 Posts!
I've already ordered a cake with 50,000 candles on it for you.
-
Nov 26th, 2007, 08:01 PM
#7
Thread Starter
Fanatic Member
Re: Declaring Variable
Thanks for that guys! It really helps..
-
Nov 27th, 2007, 04:09 AM
#8
Re: Declaring Variable
Declare variables only when you can give them a meaningful value, and in the narrowest possible scope.
-
Nov 27th, 2007, 07:45 AM
#9
Re: Declaring Variable
...Indeed.
Unless you are optimising a very tight loop in which case the normal rules don't always apply.
I don't live here any more.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|