If clause returning an error

Results 1 to 2 of 2

Thread: If clause returning an error

  1. #1
    Join Date
    Dec 1969

    Default If clause returning an error

    Dear All,<BR><BR>I am new to C# and I am trying to play with some code to get some hands on.<BR><BR>My problem is this. I have created a function to check the image extension of a file, and if it is .jpg or .gif, I want to return true.<BR><BR>I have written this code :-<BR><BR>public bool check_image_extension(string filename)<BR>{<BR>string extension;<BR>extension = System.IO.Path.GetExtension(filename);<BR><BR>if ((extension == ".jpg") &#124&#124 (extension == ".gif"))<BR>{<BR>return true;<BR>}<BR>}<BR><BR><BR>However, when i compile the code, i am returned with this error:-<BR><BR>&#039;AdminJM.Components.check_image_exten sion(string)&#039;: not all code paths return a value.<BR><BR>Can you please help me out on what the problem is?<BR><BR>Thanks for your help and time<BR><BR>Johann

  2. #2
    Join Date
    Dec 1969

    Default The error message says it all

    not all code paths return a value.<BR><BR>You have a function that you declared as returning a bool value.<BR><BR>If the extension is either ".jpg" or ".gif" then your code returns the boolean value true.<BR><BR>But what about the other "code path"?<BR><BR>*************<BR><BR>That code could be written more simply, you know.<BR><BR>public bool check_image_extension(string filename)<BR>{<BR> string extension = System.IO.Path.GetExtension(filename);<BR> return ((extension == ".jpg") &#124&#124 (extension == ".gif"));<BR>}<BR> <BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts