By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,187 Members | 1,070 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,187 IT Pros & Developers. It's quick & easy.

convert VB.Net code to C# code, the results are different in debug mode

P: n/a
The piece of code is for a Web Form Page. Who can tell me why? Thanks a lot!
------------------------------------------------
VB.Net Code:
Protected Overrides Sub AddParsedSubObject(ByVal obj As Object)

If (TypeOf obj Is LiteralControl) Then

Dim lc As LiteralControl = obj

Dim text As String = lc.Text.ToLower()

Result:

text: text "
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title>home</title>
<meta name="generator" content="microsoft visual studio .net 7.1">
<meta name="code_language" content="visual basic .net 7.1">
<meta name="vs_defaultclientscript" content="javascript">
<meta name="vs_targetschema"
content="http://schemas.microsoft.com/intellisense/ie5">
</head>
<body ms_positioning="gridlayout">

--------------------------

C# Code:

protected override void AddParsedSubObject(object obj)

{

if(obj is LiteralControl){

LiteralControl lc = (LiteralControl) obj;

String text = lc.Text.ToLower();

Result:

text "\r\n<!doctype html public \"-//w3c//dtd html 4.0 transitional//en\"
\r\n<html>\r\n\t<head>\r\n\t\t<title>commerce</title>\r\n\t\t<meta

name=\"generator\" content=\"microsoft visual studio .net
7.1\">\r\n\t\t<meta name=\"code_language\" content=\"c#\">\r\n\t\t<meta
name=\"vs_defaultclientscript\" content=\"javascript\">\r\n\t\t<meta
name=\"vs_targetschema\"
content=\"http://schemas.microsoft.com/intellisense/ie5\">\r\n\t</head>\r\n\t<body
ms_positioning=\"gridlayout\">
Nov 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
The reason for this is that VB.NET *protects* the developer from literal
strings by automagically parsing it and changing things like line feeds,
tabs, double-quotes, etc to their respective ASCII codes and then stores
them that way (AFAIK).

C# does not do this. (In C (Java, C++, C#, etc) based languages, things like
double quotes have to be escaped, much as you have seen in your code
output: --><meta name=\"generator\" content=\"microsoft visual studio
..net\"><--)

For instance, when you create a string using the line:
"The "quick" brown dog
jumped over the lazy fox."

VB.NET will parse it and convert all the \r\n (typical windows linefeed) and
double quotes for you. C# on the other hand will parse it as "The \"quick\"
brown dog\r\njumped over the lazy fox". This is because C# reads the string
literally.

HTH,
~d

"name" <li********@sympatico.ca> wrote in message
news:un********************@news20.bellglobal.com. ..
The piece of code is for a Web Form Page. Who can tell me why? Thanks a
lot!
------------------------------------------------
VB.Net Code:
Protected Overrides Sub AddParsedSubObject(ByVal obj As Object)

If (TypeOf obj Is LiteralControl) Then

Dim lc As LiteralControl = obj

Dim text As String = lc.Text.ToLower()

Result:

text: text "
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title>home</title>
<meta name="generator" content="microsoft visual studio .net 7.1">
<meta name="code_language" content="visual basic .net 7.1">
<meta name="vs_defaultclientscript" content="javascript">
<meta name="vs_targetschema"
content="http://schemas.microsoft.com/intellisense/ie5">
</head>
<body ms_positioning="gridlayout">

--------------------------

C# Code:

protected override void AddParsedSubObject(object obj)

{

if(obj is LiteralControl){

LiteralControl lc = (LiteralControl) obj;

String text = lc.Text.ToLower();

Result:

text "\r\n<!doctype html public \"-//w3c//dtd html 4.0 transitional//en\"
\r\n<html>\r\n\t<head>\r\n\t\t<title>commerce</title>\r\n\t\t<meta

name=\"generator\" content=\"microsoft visual studio .net
7.1\">\r\n\t\t<meta name=\"code_language\" content=\"c#\">\r\n\t\t<meta
name=\"vs_defaultclientscript\" content=\"javascript\">\r\n\t\t<meta
name=\"vs_targetschema\"
content=\"http://schemas.microsoft.com/intellisense/ie5\">\r\n\t</head>\r\n\t<body
ms_positioning=\"gridlayout\">

Nov 16 '05 #2

P: n/a
Great, Thanks!
"DotNet Coder" <d0*********@yahoo.com> wrote in message
news:eH**************@TK2MSFTNGP12.phx.gbl...
The reason for this is that VB.NET *protects* the developer from literal
strings by automagically parsing it and changing things like line feeds,
tabs, double-quotes, etc to their respective ASCII codes and then stores
them that way (AFAIK).

C# does not do this. (In C (Java, C++, C#, etc) based languages, things
like double quotes have to be escaped, much as you have seen in your code
output: --><meta name=\"generator\" content=\"microsoft visual studio
.net\"><--)

For instance, when you create a string using the line:
"The "quick" brown dog
jumped over the lazy fox."

VB.NET will parse it and convert all the \r\n (typical windows linefeed)
and double quotes for you. C# on the other hand will parse it as "The
\"quick\" brown dog\r\njumped over the lazy fox". This is because C# reads
the string literally.

HTH,
~d

"name" <li********@sympatico.ca> wrote in message
news:un********************@news20.bellglobal.com. ..
The piece of code is for a Web Form Page. Who can tell me why? Thanks a
lot!
------------------------------------------------
VB.Net Code:
Protected Overrides Sub AddParsedSubObject(ByVal obj As Object)

If (TypeOf obj Is LiteralControl) Then

Dim lc As LiteralControl = obj

Dim text As String = lc.Text.ToLower()

Result:

text: text "
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title>home</title>
<meta name="generator" content="microsoft visual studio .net 7.1">
<meta name="code_language" content="visual basic .net 7.1">
<meta name="vs_defaultclientscript" content="javascript">
<meta name="vs_targetschema"
content="http://schemas.microsoft.com/intellisense/ie5">
</head>
<body ms_positioning="gridlayout">

--------------------------

C# Code:

protected override void AddParsedSubObject(object obj)

{

if(obj is LiteralControl){

LiteralControl lc = (LiteralControl) obj;

String text = lc.Text.ToLower();

Result:

text "\r\n<!doctype html public \"-//w3c//dtd html 4.0 transitional//en\"
>\r\n<html>\r\n\t<head>\r\n\t\t<title>commerce</title>\r\n\t\t<meta

name=\"generator\" content=\"microsoft visual studio .net
7.1\">\r\n\t\t<meta name=\"code_language\" content=\"c#\">\r\n\t\t<meta
name=\"vs_defaultclientscript\" content=\"javascript\">\r\n\t\t<meta
name=\"vs_targetschema\"
content=\"http://schemas.microsoft.com/intellisense/ie5\">\r\n\t</head>\r\n\t<body
ms_positioning=\"gridlayout\">


Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.