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 February 11th, 2008, 03:04 PM
Authorized User
 
Join Date: Jan 2008
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Doom
Default Not all code returns a value

Hi there i have written this application.

Code:
using System;

namespace StevenHaynesCSharp.Maths
{
    class AcuteObtuse
    {
        public float AcuteObtuseTester(int x)
        {
            if (x <= 90)
            {
                Console.WriteLine("Your Angle Is Acute");
            }
            else if ((x > 90) && (x <= 180))
            {
                Console.WriteLine("Your Angle Is Obtuse");
            }
            else
            {
                Console.WriteLine("Invalid");
            }
        }
    }

    class MainEntryPoint
    {
        public static int Main()
        {
            AcuteObtuse acuteobtuse = new AcuteObtuse();

            Console.WriteLine(acuteobtuse.AcuteObtuseTester(66));
            return 0;
        }
    }
}
This the error is get when trying to compile it.
Code:
Error    1    'StevenHaynesCSharp.Maths.AcuteObtuse.AcuteObtuseTester(int)': not all code paths return a value    C:\Users\Administrator\AppData\Local\Temporary Projects\Project1\CodeFile1.cs    7    22    Project1
Can anyone point me in the correct direction for fixing this.

Thanks
Doom


 
Old February 11th, 2008, 03:09 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Take a look at the function header:

public float AcuteObtuseTester(int x)

You're saying it's returning a float. Yet, you never return a value from it. So, change the method so it returns a valid float value (or uses a different mechanism to successfully end the method (like throwing an exception), or change the method from float to void which means you don't need to return anything:

public void AcuteObtuseTester(int x)

Finally, it's odd to have a method that outputs to the console being called from another call to Consoles.WriteLine...

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
 
Old February 11th, 2008, 03:10 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

O.o

I am going to assume that you are a Novice in a programming. The compiler is telling you everything you need to know: all of the code paths in your method AcuteObtuseTester do not return values.

What is confusing is that you have defined a return type of float but you don't return anything in any code paths? Plainly put, I am not sure what your intent is here.

Anyway, you can change your code like this:

        public string AcuteObtuseTester(int x)
        {
            if (x <= 90)
            {
                return "Your Angle Is Acute";
            }
            else if ((x > 90) && (x <= 180))
            {
                return "Your Angle Is Obtuse";
            }
            else
            {
                return "Invalid";
            }
        }

     public static int Main()
        {
            AcuteObtuse acuteobtuse = new AcuteObtuse();

            Console.WriteLine(acuteobtuse.AcuteObtuseTester(66 ));
            return 0;
        }

hth.



================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
 
Old February 11th, 2008, 03:12 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

And Imar beat me to it. =P

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
 
Old February 11th, 2008, 03:48 PM
Authorized User
 
Join Date: Jan 2008
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Doom
Default

Thankyou for helping me. My code now looks like.
Code:
using System;

namespace StevenHaynesCSharp.Maths
{
    class AcuteObtuse
    {
        public void AcuteObtuseTester(int x)
        {
            if (x <= 90)
            {
                Console.WriteLine("Your Angle Is Acute");
            }
            else if ((x > 90) && (x <= 180))
            {
                Console.WriteLine("Your Angle Is Obtuse");
            }
            else
            {
                Console.WriteLine("Invalid");
            }
        }
    }

    class MainEntryPoint
    {
        public static int Main()
        {
            AcuteObtuse acuteobtuse = new AcuteObtuse();

            acuteobtuse.AcuteObtuseTester(66);
            return 0;
        }
    }
}





Similar Threads
Thread Thread Starter Forum Replies Last Post
ReadProcessMemory returns nothing evolution445 Visual Basic 2005 Basics 17 October 8th, 2008 07:56 AM
for-each carriage returns TiLaser XSLT 4 April 28th, 2008 07:31 PM
Stored Proc that returns a value elygp SQL Server 2000 4 May 9th, 2007 01:05 AM
using() in a method that returns.... belphoebes_friend General .NET 1 April 12th, 2007 04:41 PM
remove returns redruff Classic ASP Basics 13 January 9th, 2007 11:56 PM





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