Visual C# Developer Center >
Visual C# Forums
>
Visual C# General
>
palndrome, not all code paths return a value
palndrome, not all code paths return a value
- Hello,
I am attempting to make a palindrome program (that reverse for example 1221 as 1221, 4578 ,....) .
If says if the result is a palindrome or not aswell (with a second method which im still missing).
The main problem is i canno find the current error thats holding me back on the "CijferOmkeringVan" in the class1 code.
Error 1 'Opgave_3_Getal_Palindroom.Class1.CijferOmkeringVan(int)': not all code paths return a value
Marked the error field in bold.
It does not even allow me to enter any number at all, the console exits straight away.
Also for the other method that says if its a palindrome or not is a IF enough or? Bit clueless.
Any input greatly appreciated,
Regards.
using System; namespace Opgave_3_Getal_Palindroom { class ConsCode { public void ExecuteProgram() { Class1 Pal = new Class1(); Console.Title = "Opgave 3: Getalpalindroom"; Console.Write("Geef een natuurlijk getal in a.u.b ..."); Console.WriteLine(); int n = int.Parse(Console.ReadLine()); Console.Write("\nDe cijferomkering van het getal {0} is {1}.", n, Pal.CijferOmkeringVan(n)); // program code }/*ExecuteProgram*/ }/*ConsCode*/ }/*Opgave_3_Getal_Palindroom*/using System; namespace Opgave_3_Getal_Palindroom { class Class1 { public ulong CijferOmkeringVan(int Getal) { int i = 1221; string s = i.ToString(); string palindroom = ""; int tel = 0; while (tel < s.Length) { palindroom += s.Substring((s.Length - 1) - tel, 1); ++tel; } } // class code }/*Class1*/ }/*Opgave_3_Getal_Palindroom*/
Answers
- Please use the "Insert Code Block" feature (little box in upper right) to paste code - it helps keep it formatted.
The problem in your code is the method. You're never returning a value (ulong), which is why you're receiving the error message. Try changing it to:
public ulong CijferOmkeringVan(int Getal) {<br/> // You probably want to use the input value? // int i = 1221; string s = Getal.ToString();<br/> //i.ToString(); string palindroom = ""; int tel = 0; while (tel < s.Length) { palindroom += s.Substring((s.Length - 1) - tel, 1); ++tel; } // This converts the string back into a ulong, and returns it return ulong.Parse(palindroom); }
Also, if you're using .NET 3.5, you can simplify this dramatically:
public ulong CijferOmkeringVan(int Getal) { string palindroom = new String(Getal.ToString().Reverse().ToArray()); return ulong.Parse(palindroom); }
Reed Copsey, Jr. - http://reedcopsey.com- Marked As Answer byNerathas Saturday, November 07, 2009 6:23 PM
All Replies
- The error message you're getting says what the problem is. Not all code paths of the method CijferOmkeringVan return a value. You've defined your method as returning a ulong but it doesn't ever return a value.
This is a compiler error and not a runtime error. - Please use the "Insert Code Block" feature (little box in upper right) to paste code - it helps keep it formatted.
The problem in your code is the method. You're never returning a value (ulong), which is why you're receiving the error message. Try changing it to:
public ulong CijferOmkeringVan(int Getal) {<br/> // You probably want to use the input value? // int i = 1221; string s = Getal.ToString();<br/> //i.ToString(); string palindroom = ""; int tel = 0; while (tel < s.Length) { palindroom += s.Substring((s.Length - 1) - tel, 1); ++tel; } // This converts the string back into a ulong, and returns it return ulong.Parse(palindroom); }
Also, if you're using .NET 3.5, you can simplify this dramatically:
public ulong CijferOmkeringVan(int Getal) { string palindroom = new String(Getal.ToString().Reverse().ToArray()); return ulong.Parse(palindroom); }
Reed Copsey, Jr. - http://reedcopsey.com- Marked As Answer byNerathas Saturday, November 07, 2009 6:23 PM


