473,509 Members | 7,333 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

declaring strings inside vs. outside of functions

Often, I want to use a string in multiple functions on a page. I can declare
and re-set the string in each function, or I can just delcare the string
outside of the individual functions so that they can all see it.

This works fine.

My question is if there is any reason NOT to go ahead and declare ALL
strings at the top? Is there a performance/memory hit in doing that? Or is
it simply a matter of clean code syntax/layout?

-Darrel
Nov 18 '05 #1
3 1257
Go for clear code that is also easy to read and maintainable. If the
string serves different purposes in the different methods, than you
should keep the declaration inside the methods as this can reduce
confusion tremendously.

--
Scott
http://www.OdeToCode.com

On Fri, 13 Aug 2004 10:06:40 -0500, "darrel" <no*****@hotmail.com>
wrote:
Often, I want to use a string in multiple functions on a page. I can declare
and re-set the string in each function, or I can just delcare the string
outside of the individual functions so that they can all see it.

This works fine.

My question is if there is any reason NOT to go ahead and declare ALL
strings at the top? Is there a performance/memory hit in doing that? Or is
it simply a matter of clean code syntax/layout?

-Darrel


Nov 18 '05 #2
Strings are immutable in .NET. What that means is every operation, every
change, any time you change a string, and new string is created and the old
one is marked for GC.

string s = "hello";
s = s.Replace( "hell", "heaven" );

Two strings were created. I think there is actually more because the string
literals must be loaded.

IMHO, it is more a style issue. I like to have strings declared inside my
methods, they are marked for GC faster when the methods exits, rather than
when the Page is done processing a request.

HTH,

bill
"darrel" <no*****@hotmail.com> wrote in message
news:uJ**************@TK2MSFTNGP11.phx.gbl...
Often, I want to use a string in multiple functions on a page. I can declare and re-set the string in each function, or I can just delcare the string
outside of the individual functions so that they can all see it.

This works fine.

My question is if there is any reason NOT to go ahead and declare ALL
strings at the top? Is there a performance/memory hit in doing that? Or is
it simply a matter of clean code syntax/layout?

-Darrel

Nov 18 '05 #3
You're talking about the issue of scope. There is a reason why variables
declared inside functions aree not accessible from outside those functions.
They are scoped to function scope, and are protected from any other code
modifying them unintentionally. When you want to use a variable in more than
one function, it is good to declare it global to the class (as a field or
property). When you don't, it is dangerous to declare it global, as it may
be unintentionally modified by some other code than the function for which
you intended it.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"darrel" <no*****@hotmail.com> wrote in message
news:uJ**************@TK2MSFTNGP11.phx.gbl...
Often, I want to use a string in multiple functions on a page. I can declare and re-set the string in each function, or I can just delcare the string
outside of the individual functions so that they can all see it.

This works fine.

My question is if there is any reason NOT to go ahead and declare ALL
strings at the top? Is there a performance/memory hit in doing that? Or is
it simply a matter of clean code syntax/layout?

-Darrel

Nov 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
1308
by: DJ | last post by:
I am currently having trouble with strings. I have a private string variable in my class and i assign a value to it in the creation function as follows. #include <string> using std::string; ...
13
2100
by: Thomas Zhu | last post by:
Hello, I know the difference between the two definations. But I do not know where are they in the memory. would someone tell me ? char s={"good", "morning"}; // at stack? char *t = {"good",...
2
1452
by: Rob Meade | last post by:
Hi all, New to .Net - still finding my feet...quick question... In one of my functions I have about a dozen variables being declared at the top - the first thing within the function, about 2...
9
1760
by: Bilgehan.Balban | last post by:
Hi, For a declaration such as: char * mystring = "ABCDabcd123"; Is it a linker issue where such strings are stored in C, or is it defined as part of the language definition? Is there any...
1
4361
by: ypjofficial | last post by:
Hi all, what's difference does it make when we define the member function inside and outside a class? I am using vc7. when i define the member functions outside a class with the scope...
8
5838
by: rendle | last post by:
I have a MSIL/performance question: Is there any difference between declaring a variable once and assigning to it multiple times, and declaring and assigning multiple times? For example: //...
95
4958
by: hstagni | last post by:
Where can I find a library to created text-based windows applications? Im looking for a library that can make windows and buttons inside console.. Many old apps were make like this, i guess ...
8
7496
by: SM | last post by:
I've always wonder if there is diference when declaring and initializing a varible inside/outside a loop. What's a better practice? Declaring and initializing variables inside a loop routine,...
5
8799
by: Rahul B | last post by:
Hi, I have very little knowledge about creating Procedures/functions in DB2. When i tried to create the test function like CREATE FUNCTION GET_TEST (P_TEST_ID INTEGER, P_SEL_OR_SORT...
0
7347
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7073
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7506
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5062
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4732
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3218
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1571
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
779
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
443
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.