Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3
This is the forum to discuss the Wrox book Beginning C# 3.0 : An Introduction to Object Oriented Programming by Jack Purdum; ISBN: 9780470261293
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Display Modes
  #1 (permalink)  
Old November 30th, 2010, 01:48 PM
Registered User
 
Join Date: Nov 2010
Location: Noardburgum, Friesland, The Netherlands
Posts: 7
Thanks: 3
Thanked 0 Times in 0 Posts
Default Ch 13 Use of @ in front of variable name

I noticed that in listing 13-1, a @ is placed in front of buff and in front of txtStartingPath.Text.
Code:

string startingPath = @startingPathTextBox.Text;
 
var myDrive = newDriveInfo(@buff); // Get its info

I wondered why. The only info I can find is that you can use @ in order to use a variable like int for instance, but that's not the case here. If it were, the declaration of the variable would've had the @ in front of it too.
  #2 (permalink)  
Old November 30th, 2010, 02:22 PM
Friend of Wrox
Points: 1,166, Level: 13
Points: 1,166, Level: 13 Points: 1,166, Level: 13 Points: 1,166, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2008
Location: Indianapolis, IN, USA.
Posts: 234
Thanks: 0
Thanked 32 Times in 30 Posts
Default The @ operator

Hi Comecme:

If you look way back on page 122, you'll see that the amperand operator (@) is used when you wish to use string literals in a verbatim manner. As you know, the backslash can be used as a "lead-in" character for special escape sequences, like "\n" for a newline character. If you place the ampersand before the string variable on the righthand side of an assignment, that means you want a verbatim translation of the string. A common example is a directory path like: "C:\Windows\System". If you don't use the ampersand character, the same string would have to be written" "C:\\Windows\\System". Reread pages 122-123 and I think you'll see what I mean.
__________________
Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)
  #3 (permalink)  
Old November 30th, 2010, 04:09 PM
Registered User
 
Join Date: Nov 2010
Location: Noardburgum, Friesland, The Netherlands
Posts: 7
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Hi DrPurdum, thanks for the quick reply.

I know you can use the @ for verbatim string literals. But in this code you are using verbatim variable names. I've tried the code without the @ and it doesn't seem to make any difference.
Code:
// Verbatim string literal
string strVerbatim = @"C:\Temp";
 
// Verbatim variable named int
int @int = 1;
 
// Part of code listing 13-1
string startingPath = @startingPathTextBox.Text;
var myDrive = new DriveInfo(@buff);
In your code, if you'd be using string literals, both @'s would make sense.

PS: Isn't the ampersand operator the & and the at sign @?
  #4 (permalink)  
Old December 2nd, 2010, 08:52 AM
Friend of Wrox
Points: 1,166, Level: 13
Points: 1,166, Level: 13 Points: 1,166, Level: 13 Points: 1,166, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2008
Location: Indianapolis, IN, USA.
Posts: 234
Thanks: 0
Thanked 32 Times in 30 Posts
Default @ Operator

First, you're right. The "@" operator is the "at" sign while the "&" is the amperand.

As to your code, while your statement:

int @int = 1;

is legal, it's a really bad idea to use a keyword as the basis for a variable name, especially when using a legal operator as the first letter for the name. (Indeed, I think it should be an error to use the @ in this manner.) If you want to reflect the idea that you're using an integer, I would change this to:

int myInteger = 1;

or something like that. Even better would be a name that reflects the purpose of the variable.

The string literal operator (@) would have no effect on the variable you defined above since it only appears on the left side of the assignment operator. In that case, it is simply viewed as another "letter" in the identifier's name (e.g., @int). This means it is used in the expression in such a way that the compiler does not interpret it as operator.

In the case of the first statement:

string strVerbatim = @"C:\Temp";

the compiler sees the operator on the righthand side of the assignment expression and says: "Aha! The backslash is a literal and means I should NOT interpret the "\T" as the tab character."

Finally, the @buff used as the argument to DriveInfo() causes the compiler to alter the attribute list for buff in the symbol table to accept any string data as a string literal. Remember that arguments passed to a method are passed on the stack and "popped" off into a temporary variable in the method. (Reference variables pop the lvalue instead of the rvalue.) This means that the method arguments are "assigned" to the temporary variables created by the method. Therefore, the @buff argument can be viewed as executing the statement:

string strTemp = @buff;

inside the DriveInfo() method.

I hope this helps.
__________________
Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)
  #5 (permalink)  
Old December 2nd, 2010, 10:58 AM
Registered User
 
Join Date: Nov 2010
Location: Noardburgum, Friesland, The Netherlands
Posts: 7
Thanks: 3
Thanked 0 Times in 0 Posts
Default

I think I'm not making myself clear. Maybe that's because I'm Dutch?

First of all, in the example with @int, the at sign is not just another character in the variable name. The @int is a verbatim identifier. This means it makes it possible to create a variable named int, even though int is a keyword in C#. Since the @ isn't part of the variable name itself, buff and @buff in your code would refer to the same variable (the same lvalue as well as the same rvalue).

But verbatim identifiers are not what my question's about. My question is why you use the at sign in the @buff and @startingPath.Text on the righthand side of the operator. Like I mentioned before, I see no difference in the process step of the program if I leave out the at sign. If you type a "\" in the textbox, the Text property will show you "\\". If you'd type in a tab character, the Text property would show "\t", but if you type "\t" it would show "\\t".

If you try this
Code:
string startingPath = startingPathTextBox.Text;
string startingPath2 = @startingPathTextBox.Text;
if (startingPath.Equals(startingPath2))
{
  MessageBox.Show("They are the same!");
}
you'll see the strings are the same.

Either with or without the @, the variable startingpath will contain "C:\\Temp" after I've typed in C:\Temp in the textbox. For the @buff I see no difference either. And by not seeing any difference, I mean I've checked the value of startingPath in the debugger.
  #6 (permalink)  
Old December 2nd, 2010, 03:37 PM
Friend of Wrox
Points: 1,166, Level: 13
Points: 1,166, Level: 13 Points: 1,166, Level: 13 Points: 1,166, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2008
Location: Indianapolis, IN, USA.
Posts: 234
Thanks: 0
Thanked 32 Times in 30 Posts
Default @ Operator

We're probably talking at cross purposes.

First, I was just wondering why you would use the statement

int @int = 1;

What did you have in mind? I can't imagine a good reason for such code.

Second, the @ operator is really used for string literals, or quoted strings, in the code. Look at the following statements in the debugger and it may become clear what the operator does:

string startPath1 = @"C:\\temp";
string startPath2 = "C:\\temp"; ;

The result is that startPath1 ends up looking like "C:\\\\temp" while startPath2 has "C:\\temp". In the first case, the literals causes the compiler to view the backslashes as escape sequences, so it translates the literal. The @ operator simply saves the programmer some typing when path names are hard coded. Without the @ operator, what you see is what you get.
__________________
Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)
  #7 (permalink)  
Old December 2nd, 2010, 05:42 PM
Registered User
 
Join Date: Nov 2010
Location: Noardburgum, Friesland, The Netherlands
Posts: 7
Thanks: 3
Thanked 0 Times in 0 Posts
Default

I have no idea why someone might want to use @int as a variable name, I was just showing that the at sign in front of a variable name is used to make that possible. Let's not talk about @int any more.

The use of the at sign in front of a string literal is clear to me. @"C:\Temp" is the same as "C:\\Temp".

But why do you use the at sign in method btnList_Click of listing 13-1 in the book?
Code:
startingPath = @txtStartingPath.Text;
and in ShowDriveInfo?
Code:
DriveInfo myDrive = new DriveInfo(@buff);
You are the one who started using @ in front of a variable name, not me.
In these lines of code, we are not talking about a verbatim string literal. There are no string literals in this code!

Again, if I remove both @'s in listing 13-1, I get the exact same results. So, is there any reason to use the @ in these two lines of code?

Last edited by comecme; December 2nd, 2010 at 05:51 PM.
  #8 (permalink)  
Old December 2nd, 2010, 06:23 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,652
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Dr.Purdum wrote:
Quote:
Finally, the @buff used as the argument to DriveInfo() causes the compiler to alter the attribute list for buff in the symbol table to accept any string data as a string literal.
*IF* I understand what Dr.Purdum was saying, it would mean that doing
Code:
DriveInfo myDrive = new DriveInfo(@buff);
would imply that if buff had "c:\temp" assigned to it then it would be taken literally, and \t would not mean TAB.

But that's NUTSO!!!

What if you had
Code:
String foo = "c:\temp";
in some completely separate module, some separately compiled code. You and I (and Dr. Purdum) know that OF COURSE that will be seen as the string
Code:
    c:[tab]temp
And then suppose you did
Code:
String buff = foo;
DriveInfo myDrive = new DriveInfo(@buff);
Would that imply that somehow the runtime would REINTERPRET the string from foo so that the [tab] magically is reconverted to two characters "\" and "t"????

I don't believe it.

comecme, I'm with you. None of this usage makes sense to me.
The Following User Says Thank You to Old Pedant For This Useful Post:
comecme (December 16th, 2010)
 


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Ch 13-cms_articles_compose nuhbutt BOOK: Beginning PHP 6, Apache, MySQL 6 Web Development ISBN: 9780470391143 1 June 1st, 2010 12:28 PM
Simple Pagination for Ch 13 djlfreak BOOK: Beginning PHP 6, Apache, MySQL 6 Web Development ISBN: 9780470391143 0 June 1st, 2010 08:29 AM
Search ch 13, ch 16 sporik BOOK: Beginning PHP 6, Apache, MySQL 6 Web Development ISBN: 9780470391143 0 October 27th, 2009 04:44 PM



All times are GMT -4. The time now is 08:42 PM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.