Console Application based on delegates
-
hello friends, I have an one console application w.r.t delegates but while executing am facing 2 errors, here is the code . . using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Delegates { delegate string StrMod(string str); class DelegateTest { // replace spaces with hypens static string ReplacesSpaces(string s) { Console.WriteLine("raplacing spaces with hypens."); return s.Replace(' ','-'); } //reomve spaces static string RemoveSpaces(string s) { string temp=""; int i; Console.WriteLine("Removing spaces."); for(i=0;i=0) { temp=temp+s[i]; } return temp; } } static void Main(string[] args) { // construct a delegate StrMod strOp = new StrMod(ReplacesSpaces); string str; //call method through delegate str = strOp("This is a test."); Console.WriteLine("Resulting string:" + str); Console.WriteLine(); strOp = new StrMod(RemoveSpaces); str = strOp("This is a test."); Console.WriteLine("Resulting string:" + str); Console.WriteLine(); strOp = new StrMod(Reverse); str = strOp("This is a test."); Console.WriteLine("Resulting string:" + str); Console.WriteLine(); } } } error: Error 1 'Delegates.DelegateTest.RemoveSpaces(string)': not all code paths return a value C:\Users\Praveen\Documents\Visual Studio 2008\Projects\Delegates\Delegates\Program.cs 20 23 Delegates Error 2 Cannot implicitly convert type 'int' to 'bool' C:\Users\Praveen\Documents\Visual Studio 2008\Projects\Delegates\Delegates\Program.cs 43 21 Delegates A
-
hello friends, I have an one console application w.r.t delegates but while executing am facing 2 errors, here is the code . . using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Delegates { delegate string StrMod(string str); class DelegateTest { // replace spaces with hypens static string ReplacesSpaces(string s) { Console.WriteLine("raplacing spaces with hypens."); return s.Replace(' ','-'); } //reomve spaces static string RemoveSpaces(string s) { string temp=""; int i; Console.WriteLine("Removing spaces."); for(i=0;i=0) { temp=temp+s[i]; } return temp; } } static void Main(string[] args) { // construct a delegate StrMod strOp = new StrMod(ReplacesSpaces); string str; //call method through delegate str = strOp("This is a test."); Console.WriteLine("Resulting string:" + str); Console.WriteLine(); strOp = new StrMod(RemoveSpaces); str = strOp("This is a test."); Console.WriteLine("Resulting string:" + str); Console.WriteLine(); strOp = new StrMod(Reverse); str = strOp("This is a test."); Console.WriteLine("Resulting string:" + str); Console.WriteLine(); } } } error: Error 1 'Delegates.DelegateTest.RemoveSpaces(string)': not all code paths return a value C:\Users\Praveen\Documents\Visual Studio 2008\Projects\Delegates\Delegates\Program.cs 20 23 Delegates Error 2 Cannot implicitly convert type 'int' to 'bool' C:\Users\Praveen\Documents\Visual Studio 2008\Projects\Delegates\Delegates\Program.cs 43 21 Delegates A
praveengb wrote:
Error 1 'Delegates.DelegateTest.RemoveSpaces(string)': not all code paths return a value
//reomve spaces
static string RemoveSpaces(string s)
{
string temp="";
int i;
Console.WriteLine("Removing spaces.");
for(i=0;i<s.Length;i++)
{
if (s[i] != ' ')
{
temp = temp + s[i];
return temp;
}
}
}This method does not return anything if the if-case is never true. This leads to 'not every possible scenario' is returning a string as expected. Add a return statement after '
for
' ends to correct it. If nothing else, return an empty string.praveengb wrote:
Error 2 Cannot implicitly convert type 'int' to 'bool'
Your checking condition in for loop here:
for (j = 0; i = s.Length - 1; i--, j++) //Incorrect
It should be a boolean true-false condition. Try:
i <= s.Length -1
Sandeep Mewara [My last tip/trick]: Server side Delimiters in ASP.NET[^]