473,322 Members | 1,345 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Scope and Coding Style

I have never done this or seen this but thought it might be useful. What do
you think of it? This is a very simplified example but frequenly when
reusing an object, I find myself spending a lot of time insuring that I
don't carry values over from the previous use. This insures that each of
these objects are in their own scope. (I am not looking for a 'using'
statement here since my object doesn't implement IDispose.) I guess a good
alternative would be be to wrap each of these entities in a different sub
method.

public void SavePhone() {
{
PhoneEntity phone = new PhoneEntity();
phone.Number = "123";
phone.Save();
}

{
PhoneEntity phone = new PhoneEntity();
phone.Number = "234";
phone.Save();
}

{
PhoneEntity phone = new PhoneEntity();
phone.Number = "345";
phone.Save();
}

{
PhoneEntity phone = new PhoneEntity();
phone.Number = "456";
phone.Save();
}
}
let the opinions fly!

Apr 4 '07 #1
3 1237
"Andrew Robinson" <ne****@nospam.nospamwrote:
This is a very simplified example but frequenly when reusing
an object, I find myself spending a lot of time insuring that I
don't carry values over from the previous use. [...]
[using separate {} blocks to enforce "sub-local" scope]
let the opinions fly!
I don't think your example is a good example because each block
already does the equivalent of overwriting the previous value with a
new one. You're saying that you might forget to do this; however, you
might equally forget to put the { } braces around one of the blocks,
or make any number of other errors. Ultimately, there's no structure
that will save you from your own carelessness.

I am vaguely reminded of the C comparison idiom where you write (5==a)
rather than (a==5), because (a=5) would do an accidental assignment
whereas (5=a) would be a compile error. (This isn't an issue in C#
because conditions have to be Boolean values.) However, that's really
just a typo, whereas what you mention involves missing out a whole
step in the process, whether it is calling a Clear method or assigning
to a variable.
I guess a good alternative would be be to wrap each of these
entities in a different sub method.
Definitely for your example! If you duplicate the same few lines over
and over, like you've done here, then you're risking a different
problem: suppose the rules for saving a PhoneEntity change, such that
you also have to set the AreaCode property. If you have a method, you
can just change the code in one place. If not, you're going to have to
hunt down all of them and change it in a hundred places. This is very
error-prone and rather annoying.

If you're talking about a case with less repetitive actions, then post
a better example :)

Eq.
Apr 4 '07 #2
Hi Andrew,

Sorry that I may not understand your question exactly.
This is a very simplified example but frequenly when reusing an object, I
find myself spending a lot of time insuring that I don't carry values over
from the previous use.

I think you could define different variables for different usage, or define
a variable first and then every time you'd like to use a new instance, you
create a new instance and assign the new instance to the variable .

For example:

PhoneEntity phone1 = new PhoneEntity();
phone1.Number = "123";
phone1.Save();

PhoneEntity phone2 = new PhoneEntity();
phone2.Number = "234";
phone2.Save();

Or

PhoneEntity phone = null;

phone = new PhoneEntity();
phone.Number = "123";
phone.Save();

phone = new PhoneEntity();
phone.Number = "234";
phone.Save();

Hope this helps.

If you have any question, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Apr 5 '07 #3
Hi Andrew,

How about the problem now?

If you have any question, please feel free to let me know.

Thank you for using our MSDN Managed Newsgroup Support Service!

Sincerely,
Linda Liu
Microsoft Online Community Support

Apr 9 '07 #4

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

Similar topics

4
by: Marc Tanner | last post by:
Hello, I am currently working on a eventhandling system or something similar, and have the problem of loosing scope. I have read many interesting posts on this group and the faq article about...
8
by: lawrence | last post by:
I'm learning Javascript. I downloaded a script for study. Please tell me how the variable "loop" can have scope in the first function when it is altered in the second function? It is not defined...
12
by: Keith Chadwick | last post by:
I have a fairly hefty XSLT file that for the sake of debugging and clarity I wish to split into some separate sub-templates contained within the same file. The master template calls an...
18
by: craig | last post by:
I am curious about how many of you prefer style 1 vs. style 2, and why. Are there names for these style? style 1: method { }
144
by: Natt Serrasalmus | last post by:
After years of operating without any coding standards whatsoever, the company that I recently started working for has decided that it might be a good idea to have some. I'm involved in this...
4
by: Mike Labosh | last post by:
I realize that you people have not seen much of me other than some framework responses I have posted. I am primarily a VB guy (yes, you can laugh) But I have lurked here for several years,...
13
by: benben | last post by:
Is there an effort to unify the c++ coding standard? Especially identifier naming. Not a big issue but it would be annoying to have to incorporate different coding styles simultaneously when...
26
by: Patient Guy | last post by:
The code below shows the familiar way of restricting a function to be a method of a constructed object: function aConstructor(arg) { if (typeof(arg) == "undefined") return (null);...
2
by: chris.ritchie | last post by:
My question is very basic, but I can't find any material on it. Compare these: string x; // only used within the following loop for (int y = 0; y < 10; y++){ x = toString(y); use(x); } ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.