Context: See below Code 1 C# (and compilation error), Code 2 C# (one possible Work-around, and in this specific work-around, why should I make a President a VicePresident? I don't have that authority :-D )
Questions: Is this thread's question relates to documentation reading? or to designing aspect? -If documentation reading related, then point me to a link. -If designing concerned, then: 1. Were there any scenario or reason or code pattern that make the
C# Designer from design a valid inner/outer scope for the same variable name? Note: C++ and java do not have a scoping problem with the inner block (for loop) and the out block (main itself). See down below Code 3.A (C++), Code 3.B (java). Code 3.C (C#). 2.
If *you are* a code generator software designer from C++ or Java to C#, how would you handle Code 1? I mean how would you handle the President? What would be your strategy? make a VicePresident? If you can. :-D 3. If *your team* plan to migrate from C++ or
Java yourself, how would you handle the President? make a VicePresident? That would be a revolution! :-D --------------------------------------------------------------------------------------------------------------
Code 1. c#, C# compiler version 1.0.3705 : It's not compiled. See below Compilation Error Output.
using System;
using System.Xml;
public class DomXpath
{
public static void Main(string[] args)
{
try
{
string xmlfile = "WorldPresidents.xml";
XmlDocument doc = new XmlDocument();
doc.Load(xmlfile);
// Example 1: Using XPath to list all Presidents
string xPath = "//President";
XmlNodeList Presidents = doc.SelectNodes(xPath);
if (Presidents !=null)
{
foreach (XmlNode President in Presidents) // line 17
{
for (int j=0; j<President.ChildNodes.Count; j++)
{
Console.WriteLine(President.ChildNodes[j].InnerText);
}
}
}
// Example 2: Using XPath to update a single President
string Country = "FRANCE";
xPath = "//President[Country='" + Country.ToUpper() + "']/Name";
XmlNode President = doc.SelectSingleNode (xPath); // line 28
if (President !=null) // line 29
{
President.InnerText = "Your Name"; // line 31 // Too good to be true? :-D
}
else
Console.WriteLine (xPath);
doc.Save(xmlfile);
}
catch (XmlException exp)
{
Console.WriteLine("XmlException: " +exp.ToString());
}
catch (Exception exp)
{
Console.WriteLine("Exception: " +exp.ToString());
}
}
}
/*
Microsoft (R) Visual C# .NET Compiler version 7.00.9466
for Microsoft (R) .NET Framework version 1.0.3705
Copyright (C) Microsoft Corporation 2001. All rights reserved.
DomXpath.cs(28,12): error CS0136: A local variable named 'President' cannot be
declared in this scope because it would give a different meaning to
'President', which is already used in a 'child' scope to denote
something else
DomXpath.cs(28,12): error CS0103: The name 'President' does not exist in the
class or namespace 'DomXpath'
DomXpath.cs(29,8): error CS0103: The name 'President' does not exist in the
class or namespace 'DomXpath'
DomXpath.cs(31,5): error CS0246: The type or namespace name 'President' could
not be found (are you missing a using directive or an assembly
reference?)
>>>Please see Source code: line 17, 28,29, 31
*/
Note
Instead of
foreach (XmlNode President in Presidents) // line 17
{
for (int j=0; j<President.ChildNodes.Count; j++)
{
Console.WriteLine(President.ChildNodes[j].InnerText);
}
}
we have. And still same error.
for (int i=0; i < Presidents.Count; i++)
{
XmlNode President = Presidents[i];
for (int j=0; j<President.ChildNodes.Count; j++)
{
Console.WriteLine(President.ChildNodes[j].InnerText);
}
}
--------------------------------------------------------------------------------------------------------------
Code 2. c#, one possible Work-around, and make a President a VicePresident.
using System;
using System.Xml;
public class DomXpath
{
public static void Main(string[] args)
{
try
{
string xmlfile = "WorldPresidents.xml";
XmlDocument doc = new XmlDocument();
doc.Load(xmlfile);
// Example 1: Using XPath to list all Presidents
string xPath = "//President";
XmlNodeList Presidents = doc.SelectNodes(xPath);
if (Presidents !=null)
{
foreach (XmlNode President in Presidents)
{
for (int j=0; j //XmlNode President = doc.SelectSingleNode (xPath);
XmlNode VicePresident = doc.SelectSingleNode (xPath);
//if (President !=null)
if (VicePresident !=null)
{
//President.InnerText = "Your Name"; // Too good to be true? :-D
VicePresident.InnerText = "Your Name"; // Too good to be true? :-D
}
else
Console.WriteLine (xPath);
doc.Save(xmlfile);
}
catch (XmlException exp)
{
Console.WriteLine("XmlException: " +exp.ToString());
}
catch (Exception exp)
{
Console.WriteLine("Exception: " +exp.ToString());
}
}
}
---------------------------- Code 3.A (C++), Code 3.B (java). Code 3.C (C# and compilation error) -------------------
Code 3.A. C++, Microsoft complier VS 6.0 : Compiled and Result executed as expected.
#include
void main()
{
for (int i=0;i<2;i++)
{
int k =i;
cout<Code 3.B. Java, Sun compiler JDK1.4.x : Compiled and Result executed as expected.
public class BlockScope
{
public static void main(String[] args)
{
for (int i=0;i<2;i++)
{
int k =i;
System.out.println (k);
}
int k=1;
System.out.println (k);
}
}
Code 3.C. c#, C# compiler version 1.0.3705 : It's not compiled. See below Compilation Error Output.
public class BlockScope
{
public static void Main(string[] args)
{
for (int i=0;i<2;i++)
{
int k =i;
System.Console.WriteLine (k);
}
int k=1; // line 10
System.Console.WriteLine (k); // line 11
}
}
--Compilation Error:
Microsoft (R) Visual C# .NET Compiler version 7.00.9466
for Microsoft (R) .NET Framework version 1.0.3705
Copyright (C) Microsoft Corporation 2001. All rights reserved.
BlockScope.cs(10,7): error CS0136: A local variable named 'k' cannot be declared
in this scope because it would give a different meaning to 'k', which is
already used in a 'child' scope to denote something else
BlockScope.cs(10,7): error CS0103: The name 'k' does not exist in the class or
namespace 'BlockScope'
BlockScope.cs(11,29): error CS0103: The name 'k' does not exist in the class or
namespace 'BlockScope'
-------------------------------------------------------------------------------------------------
Where there is a will, there is a way.
and where there is a team, there is more than one way.
Hi, I guess you would have to ask Anders to get real and profound reasons why he designed it this way. My two cents: Codes 3 are harder to read than if you name both k variables the same. In C#, the scope of a variable is larger than in some other languages,
it does not start at the line where the variable is declared but covers the whole "indentation level". It's a design choice. As you figured, this is easily worked around by renaming one of the variables. But you don't have to make it a vice-president (this
would be misleading to anyone who reads your code), you can name the one in the loop localPresident, for example. As for code designers, I guess they control the code they generate, so they should be able to work around this as easily as you can. And for Java
migration, I don't know if this is handled by the tools that are provided with J#. You would have to check this one. Would be interesting to know. I don't know how J# handles variable scope either. Anyone? Hope this helps.
Bertrand
----
This posting is provided "AS IS" with no warranties, and confers no rights.
bleroy, 1. ::My two cents: Codes 3 are harder to read than if you name both k variables the same See if I give you a 'k' variable example alone, you would probably say that or rather "that does not make sense" :-D And that's the reason I came with the President.
:-D :-D 2. ::But you don't have to make it a vice-president (this would be misleading to anyone who reads your code), Yes. We don't have to. But during the code review session, the boss would say, "So who is the vice-president? Are you? and since when?". Should
I say "Since I know C#. :-D :-D " ::you can name the one in the loop localPresident, for example. Yes. But this is *not* natural and gracious. You lost the core 'Block scope' that every programmer know since Pascal. Any C++ or Java or Pascal will say "If you
have to change name variable to adjust this blocking scope, then I would have think hard for a name, or just append a prefix local and for all the source code for a project. And who know how big is a project and we have to do a code inspection to change these
variable 'manually' ::I guess you would have to ask Anders to get real and profound reasons why he designed it this way. This is *exactly* what C++ and Java Designer or any future Language Designer like *you* would like to know for sure? ::You would have to
check this one. Would be interesting to know. This is expressed by my questions about the designing concerned. Thanks for replying.
Where there is a will, there is a way.
and where there is a team, there is more than one way.
Phuoc
Participant
1150 Points
230 Posts
How to handle the President? Explain me if you can? by Block scope.
Aug 04, 2003 09:12 PM|LINK
using System; using System.Xml; public class DomXpath { public static void Main(string[] args) { try { string xmlfile = "WorldPresidents.xml"; XmlDocument doc = new XmlDocument(); doc.Load(xmlfile); // Example 1: Using XPath to list all Presidents string xPath = "//President"; XmlNodeList Presidents = doc.SelectNodes(xPath); if (Presidents !=null) { foreach (XmlNode President in Presidents) // line 17 { for (int j=0; j<President.ChildNodes.Count; j++) { Console.WriteLine(President.ChildNodes[j].InnerText); } } } // Example 2: Using XPath to update a single President string Country = "FRANCE"; xPath = "//President[Country='" + Country.ToUpper() + "']/Name"; XmlNode President = doc.SelectSingleNode (xPath); // line 28 if (President !=null) // line 29 { President.InnerText = "Your Name"; // line 31 // Too good to be true? :-D } else Console.WriteLine (xPath); doc.Save(xmlfile); } catch (XmlException exp) { Console.WriteLine("XmlException: " +exp.ToString()); } catch (Exception exp) { Console.WriteLine("Exception: " +exp.ToString()); } } } /* Microsoft (R) Visual C# .NET Compiler version 7.00.9466 for Microsoft (R) .NET Framework version 1.0.3705 Copyright (C) Microsoft Corporation 2001. All rights reserved. DomXpath.cs(28,12): error CS0136: A local variable named 'President' cannot be declared in this scope because it would give a different meaning to 'President', which is already used in a 'child' scope to denote something else DomXpath.cs(28,12): error CS0103: The name 'President' does not exist in the class or namespace 'DomXpath' DomXpath.cs(29,8): error CS0103: The name 'President' does not exist in the class or namespace 'DomXpath' DomXpath.cs(31,5): error CS0246: The type or namespace name 'President' could not be found (are you missing a using directive or an assembly reference?) >>>Please see Source code: line 17, 28,29, 31 */ Note Instead of foreach (XmlNode President in Presidents) // line 17 { for (int j=0; j<President.ChildNodes.Count; j++) { Console.WriteLine(President.ChildNodes[j].InnerText); } } we have. And still same error. for (int i=0; i < Presidents.Count; i++) { XmlNode President = Presidents[i]; for (int j=0; j<President.ChildNodes.Count; j++) { Console.WriteLine(President.ChildNodes[j].InnerText); } }-------------------------------------------------------------------------------------------------------------- Code 2. c#, one possible Work-around, and make a President a VicePresident.using System; using System.Xml; public class DomXpath { public static void Main(string[] args) { try { string xmlfile = "WorldPresidents.xml"; XmlDocument doc = new XmlDocument(); doc.Load(xmlfile); // Example 1: Using XPath to list all Presidents string xPath = "//President"; XmlNodeList Presidents = doc.SelectNodes(xPath); if (Presidents !=null) { foreach (XmlNode President in Presidents) { for (int j=0; j //XmlNode President = doc.SelectSingleNode (xPath);
XmlNode VicePresident = doc.SelectSingleNode (xPath);
//if (President !=null)
if (VicePresident !=null)
{
//President.InnerText = "Your Name"; // Too good to be true? :-D
VicePresident.InnerText = "Your Name"; // Too good to be true? :-D
}
else
Console.WriteLine (xPath);
doc.Save(xmlfile);
}
catch (XmlException exp)
{
Console.WriteLine("XmlException: " +exp.ToString());
}
catch (Exception exp)
{
Console.WriteLine("Exception: " +exp.ToString());
}
}
}
---------------------------- Code 3.A (C++), Code 3.B (java). Code 3.C (C# and compilation error) ------------------- Code 3.A. C++, Microsoft complier VS 6.0 : Compiled and Result executed as expected.and where there is a team, there is more than one way.
bleroy
All-Star
15617 Points
2302 Posts
Re: How to handle the President? Explain me if you can? by Block scope.
Aug 04, 2003 09:30 PM|LINK
----
This posting is provided "AS IS" with no warranties, and confers no rights.
Phuoc
Participant
1150 Points
230 Posts
Re: How to handle the President? Explain me if you can? by Block scope.
Aug 04, 2003 10:03 PM|LINK
and where there is a team, there is more than one way.