Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Console Application based on delegates

Console Application based on delegates

Scheduled Pinned Locked Moved C#
csharpvisual-studiolinqhelp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    praveengb
    wrote on last edited by
    #1

    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

    S 1 Reply Last reply
    0
    • P praveengb

      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

      S Offline
      S Offline
      Sandeep Mewara
      wrote on last edited by
      #2

      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[^]

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups