468,765 Members | 1,413 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,765 developers. It's quick & easy.

Is these actually any kind of definition between a definition and declaration

Hello!

I'm reading in a book and sometimes they use the term declaration and
sometimes they use the term definition.

I just wonder if there exist any kind of definition about what exactly a
definition resp a declaration is?

For example here is an extract.
Notice that the StreamReader declaration was moved outside the Try block.

StreamReader sr = new StreamReader("text.txt");
try
{
Console.writeLine(sr.readToEnd());
}
catch(Exception ex)
{
....
}

One more thing in the book they say the following "Typically, all code
except for simple variable decaration should occur
within Try blocks."

If I for example have a method that just add "" to a string so string 1
become "1" doesn't need to be put into a Try catch block
I assume. Do you agree ?

//Tony
Sep 21 '08 #1
5 1048
"Tony Johansson" <jo*****************@telia.comwrote in message
news:WI****************@newsb.telia.net...
Hello!

I'm reading in a book and sometimes they use the term declaration and
sometimes they use the term definition.

I just wonder if there exist any kind of definition about what exactly a
definition resp a declaration is?

For example here is an extract.
Notice that the StreamReader declaration was moved outside the Try block.

StreamReader sr = new StreamReader("text.txt");
try
{
Console.writeLine(sr.readToEnd());
}
catch(Exception ex)
{
...
}

One more thing in the book they say the following "Typically, all code
except for simple variable decaration should occur
within Try blocks."

If I for example have a method that just add "" to a string so string 1
become "1" doesn't need to be put into a Try catch block
I assume. Do you agree ?

//Tony
I'd love to know the title and ISBN of a book that actually advises the
reader to put all code except simple variable declarations in try blocks.
Please tell us?

FWIW, I agree with you that there is a lot of code that doesn't to be in try
blocks ;-)

Sep 21 '08 #2
Hello!

Have you any good answer to definition and declaration questions ?

// Tony

"PvdG42" <pv**@toadstool.eduskrev i meddelandet
news:On**************@TK2MSFTNGP06.phx.gbl...
"Tony Johansson" <jo*****************@telia.comwrote in message
news:WI****************@newsb.telia.net...
>Hello!

I'm reading in a book and sometimes they use the term declaration and
sometimes they use the term definition.

I just wonder if there exist any kind of definition about what exactly a
definition resp a declaration is?

For example here is an extract.
Notice that the StreamReader declaration was moved outside the Try block.

StreamReader sr = new StreamReader("text.txt");
try
{
Console.writeLine(sr.readToEnd());
}
catch(Exception ex)
{
...
}

One more thing in the book they say the following "Typically, all code
except for simple variable decaration should occur
within Try blocks."

If I for example have a method that just add "" to a string so string 1
become "1" doesn't need to be put into a Try catch block
I assume. Do you agree ?

//Tony

I'd love to know the title and ISBN of a book that actually advises the
reader to put all code except simple variable declarations in try blocks.
Please tell us?

FWIW, I agree with you that there is a lot of code that doesn't to be in
try blocks ;-)

Sep 21 '08 #3
Tony, when you simply declare a variable, you're creating a new variable
with a default value, but not assigning any specific value to it. You're
reserving space, giving it a data type, and letting the compiler or runtime
code assign a default value. The default value depends on both the data type
and the language - the rules are not necessarily the same for all languages:

String myString;

When you define a variable, you're assigning a value to it:

myString = "abc";

Both C# and VB, and many other languages as well, offer syntax in which you
can both declare and define a variable in a single statement:

String myString = "abc";
As to when and how to use Try blocks, there are as many opinions as there
are programmers. Many believe that these two rules apply:

1. Your application should never crash, consequently no exceptions should
occur that are not intercepted and somehow handled by an exception handler
(i.e., a Try/Catch block);

2. You should not use Try blocks in sections of code where you do not have
something useful to do when an exception occurs. The corollary is that you
should if you do.

Beyond these, all else is interpretation.
Tom Dacon
Dacon Software Consulting

"Tony Johansson" <jo*****************@telia.comwrote in message
news:WI****************@newsb.telia.net...
Hello!

I'm reading in a book and sometimes they use the term declaration and
sometimes they use the term definition.

I just wonder if there exist any kind of definition about what exactly a
definition resp a declaration is?

For example here is an extract.
Notice that the StreamReader declaration was moved outside the Try block.

StreamReader sr = new StreamReader("text.txt");
try
{
Console.writeLine(sr.readToEnd());
}
catch(Exception ex)
{
...
}

One more thing in the book they say the following "Typically, all code
except for simple variable decaration should occur
within Try blocks."

If I for example have a method that just add "" to a string so string 1
become "1" doesn't need to be put into a Try catch block
I assume. Do you agree ?

//Tony

Sep 21 '08 #4
"Tony Johansson" <jo*****************@telia.comwrote in message
news:JY****************@newsb.telia.net...
Hello!

Have you any good answer to definition and declaration questions ?

// Tony
When you write something like this: int x;
you are declaring a variable. When you write a class definition, you are
defining a new type.
Perhaps if you were to quote some statements (presumably from the unnamed
book) that are confusing you, others could better address your concerns.

Sep 22 '08 #5
OD
I'm reading in a book and sometimes they use the term declaration and
sometimes they use the term definition.
I'm not sure there's an academic way to answer.. the author is just
using synonyms to avoid repetition :-)
Of course wa can speak hours about the "real" differences between both
words, you "declare" a variable and you "define" a class for example.
But in both cases your writing/creating code... one a variable has been
"declared" one can say it is "defined" ! and once a class has been
"defined", we can also say it has been "declared" !
it's splitting hairs :-)
One more thing in the book they say the following "Typically, all code except
for simple variable decaration should occur
within Try blocks."
it's hard to say and always hard to judge a sentence extracted from its
context, but as you write it, it's something stupid. Not all pieces of
code must be in a try/catch. Sometimes it must, sometimes it's useless.

--
OD___
www.e-naxos.com
Sep 22 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Razvan | last post: by
39 posts views Thread by Holly | last post: by
8 posts views Thread by newmans | last post: by
19 posts views Thread by J. J. Farrell | last post: by
9 posts views Thread by Curious Student | last post: by
10 posts views Thread by Kobu | last post: by
2 posts views Thread by Laurent Deniau | last post: by
15 posts views Thread by vaib | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.