Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 15th, 2005, 09:03 AM
Authorized User
 
Join Date: Nov 2005
Posts: 15
Thanks: 5
Thanked 0 Times in 0 Posts
Default simple error

Hi
Here this is my program to access xml file , and i am getting an error "not all code return a value".

Anybody plz hlp ....

namespace XMLMIntro
{
class Exercise
{
public int ShowContinents(string xmlFileName, string Name)
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(xmlFileName);

XmlElement elem = xmlDocument.DocumentElement;
XmlNode Node = elem.FirstChild;
XmlNodeList lstadd = elem.GetElementsByTagName("add");

if (Node.HasChildNodes)
{
for (int i = 0; i < lstadd.Count; i++)
{

if (addList[i].Attributes["name"].InnerText == Name)
{
int returnValue = (System.Convert.ToInt32(addAttributes["value"].InnerText));
return returnValue;
}
}
}
}
}
}
 
Old November 15th, 2005, 09:08 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Read the error message. If you evaluate you prorgam you will notice that you only return a value when Node.HasChildNodes and addList[i].Attributes["name"].InnerText == Name. What integer value will be returned if this is not the case?

Jacob.
 
Old November 15th, 2005, 09:15 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

BTW... I believe that your error message is something like not all paths return a value, and therefore a Google search would render the following...

http://www.google.dk/search?hl=da&q=...%22+.net&meta=

Hope it helps, Jacob ;)
 
Old November 15th, 2005, 10:21 AM
Authorized User
 
Join Date: Nov 2005
Posts: 15
Thanks: 5
Thanked 0 Times in 0 Posts
Default

Hi Jacob,
Thanq for the reply. But what value I have to return
eg:return 1;
is correct?

 
Old November 15th, 2005, 10:56 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It depends heavily on the flow of the program you are designing. I am not too sure what you are trying to accomplish, but if you specify in the method signature that you want to return an integer, then you have to return an integer; i.e. no matter what path is chosen.

E.g. if you know that your method always returns a possitive integer when the method returns as you would like, then you could return -1 for the case where if fails to return what you would like.
Code:
public int ShowContinents(string xmlFileName, string Name)
{
    ...
    if(Node.HasChildNodes)
    {
        for(int i = 0; i < lstadd.Count; i++)
        {
            if(addList[i].Attributes["name"].InnerText == Name)
            {
                ...
                return returnValue; // returnValue always possitive.
            }
        }
    }
    return -1;
}
When you call your method ShowContinents you can ask if it is -1 or not. This is just an example, however, remember to return from all paths.

HtH, Jacob.
 
Old November 16th, 2005, 08:05 AM
Authorized User
 
Join Date: Nov 2005
Posts: 15
Thanks: 5
Thanked 0 Times in 0 Posts
Default

Thanks Jacob ,
 It Worked!
cheers
Jack






Similar Threads
Thread Thread Starter Forum Replies Last Post
ERROR....!when trying to use a simple custom contr benfowler ASP.NET 1.0 and 1.1 Basics 1 April 28th, 2008 07:39 PM
Simple assembly program runtime error vraifreud Visual C++ 2005 0 September 21st, 2007 01:20 AM
Simple Q Apocolypse2005 Beginning VB 6 5 August 8th, 2007 08:04 AM
Simple report adjustments give #error iaingblack Access 4 March 21st, 2007 03:41 PM
Error on simple page kkk_19245 ASP.NET 2.0 Basics 0 February 22nd, 2007 10:28 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.