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

T

Tony Johansson

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
 
P

PvdG42

Tony Johansson said:
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 ;-)
 
T

Tony Johansson

Hello!

Have you any good answer to definition and declaration questions ?

// Tony
 
T

Tom Dacon

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
 
P

PvdG42

Tony Johansson said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top