Really basic C# and Visual Studio 2008 question

M

Mitch

I'm brand new, and just started C# for Dummies.

I've also put together a list of websites with code snippets and
tutorials.


If I cut-and-paste the following line from an example into Visual
Studio, I get errors.

Console.WriteLine(“Enter your name, please:”);

If I manually type the " and ( ) characters, it gets rid of the
errors.

Why is this? Can it be avoided? I had planned on being able to do a
lot of cut-and-pasting as I learn code.
 
M

Mitch

Interesting, it still doesn't work.
In fact, if i paste into Notepad, select all, copy, and paste into VS,
i get all the same errors.

Is there some setting in Visual Studio to tell it to stop trying to be
so smart?
 
M

Mitch

Can you tell us a little more about the errors? Is it something to do with the quotation marks?

The quotation marks and the parentheses.
"Unexpected character."

If I highlight each one and retype the character over it, the error
disappears from the list.
 
M

Mitch

Can you tell us a little more about the errors? Is it something to do with the quotation marks?

Here's the whole program which results in 33 errors:

using System;
namespace HelloWorld
{
public class Program
{
// This is where the program starts
static void Main(string[] args)
{
// prompt user to enter a name
Console.WriteLine(“Enter your name, please:”);
// now read the name entered
string sName = Console.ReadLine();
// greet the user with the entered name
Console.WriteLine(“Hello, “ + sName);
// wait for user to acknowledge the results
Console.WriteLine(“Press Enter to terminate...”);
Console.Read();
}
}
}
 
M

Mitch

I guess after all these decades using the computer, I'm going to have
to learn how to type.

Where's my Mavis Beacon? :)
 
J

John Smith

Mitch@_._ said:
I guess after all these decades using the computer, I'm going to have
to learn how to type.

Where's my Mavis Beacon? :)


You can use Ctrl-H to do a quick search and replace of incorrect
quotation marks.
 
R

Rudy Velthuis

Mitch@_._ said:
Can you tell us a little more about the errors? Is it something to
do with the quotation marks?

Here's the whole program which results in 33 errors:

using System;
namespace HelloWorld
{
public class Program
{
// This is where the program starts
static void Main(string[] args)
{
// prompt user to enter a name
Console.WriteLine(“Enter your name, please:”);
// now read the name entered
string sName = Console.ReadLine();
// greet the user with the entered name
Console.WriteLine(“Hello, “ + sName);

The “ and ” are the wrong characters to delimit strings. You should use
" instead. I have no idea what formatted your code, but the quote
characters you show are typographic ones, they are not the simple ASCII
character with ASCII code hex 22 the compiler expects.

Try the following code instead. Copy and paste it into your editor and
run it. It works for me (and I'm not even using VS).

using System;

namespace HelloWorld
{
public class Program
{
// This is where the program starts
static void Main(string[] args)
{
// prompt user to enter a name
Console.WriteLine("Enter your name, please:");
// now read the name entered
string sName = Console.ReadLine();
// greet the user with the entered name
Console.WriteLine("Hello, " + sName);
// wait for user to acknowledge the results
Console.WriteLine("Press Enter to terminate...");
Console.Read();
}
}
}

--
Rudy Velthuis http://rvelthuis.de

"The great thing about a computer notebook is that no matter how
much you stuff into it, it doesn't get bigger or heavier."
-- Bill Gates.
 
R

Rudy Velthuis

Peter said:
Alternatively, don't waste time visiting sites that post uncompilable
code. :)

Just a guess, but I assume the code was pasted into a tool for
designing websites or perhaps even a word processor like Word, and this
added the fancy formatting. Could well be that the author of the
website is not even aware of it, especially if the author of the
website is not the author of the code.
 
M

Mitch

Take that back. Its useless.

Why do you say that?
I was just composing an email to someone, and used PT to strip down a
bunch of mismatched text that I had cut-n-pasted from various sources.
Worked great.
 
J

John Smith

NMitch@_._ said:
Why do you say that?
I was just composing an email to someone, and used PT to strip down a
bunch of mismatched text that I had cut-n-pasted from various sources.
Worked great.


Glad to hear it. However it is useless when it comes to removing smart
(curly) quotes, which is an issue when you are trying to copy and paste
from html or document sources.

Removing formatted quotes is the issue that you had when trying to
compile formatted copying code. :)
 
M

Mitch

Okay, here's another one. if i copy and paste a code example that
doesn't use indenting, is there a way to make VS indent it?
 
M

Mitch

If the code is compilable code (and sometimes even if it's not) and you
already have auto-formatting turned on, VS will reformat the pasted code,
indentation and all.

Okay, thanks. I was trying to get it to autoformat while those
incorrect quotes were still there.
 
J

John Smith

Mitch@_._ said:
Okay, here's another one. if i copy and paste a code example that
doesn't use indenting, is there a way to make VS indent it?

Go to To:

View->Advance->Format Document
OR
View->Advance->Format Selection

Personally I dragged them onto the toolbar. (Tools-Customize).
 

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