Results 1 to 6 of 6

Thread: exiting a method [RESOLVED]

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489

    exiting a method [RESOLVED]

    in VB, you can use EXIT SUB to get out of a sub early. What is the syntax for c#?
    Last edited by Andy; Aug 13th, 2004 at 11:46 AM.

  2. #2

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    ok I THINK I got that resolved but here is another problem along the same lines:

    Code:
    		public string FormatPhone (string number)
    		{
    			try
    			{
    				if (number.IndexOf("-") > 0 || number.Length <7)
    				{
    					return number;
    				}
    			
    
    				//Variables.
    				string three, next3, four;
    
    				//is the value 10 digits?
    				//possibly a fully - qualified number.
    				if (number.Length == 10)
    				{
    					three = number.Substring(0, 3);
    					next3 = number.Substring(3, 3);
    					four = number.Substring(6);
    					number = three + "-" + next3 + "-" + four;
    				}
    
    				//is the value more than 7 but less than 10?
    				//possibly a valid 7 digit number.
    				if (number.Length==7)
    				{
    					three = number.Substring(0, 3);
    					four = number.Substring(3);
    					number = three + "-" + four;
    				}
    
    				return number;
    			}
    			catch (Exception ex)
    			{
    				MessageBox.Show(ex.Message);
    			}
    			
    		}
    i get an error saying Not all code paths return a value. and the name FormatPhone is underlined.

    what's that mean? is there something wrong with the code?

  3. #3
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    hello mate. your catch doesn't throw a code. that it is. you could try a returning a null string after the exception message is shown
    PHP Code:
          string f(string s)
          {
             
    int i;
             try
             {
                
    i=Convert.ToInt32(s);
             }
             catch(
    Exception ex)
             {
                
    MessageBox.Show(ex.Message);  // this doesn't return
                
    return "";  // you can return a null
             
    }
             return (
    i+1).ToString();
          } 

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    i see. So in order to NOT alter the string passed into the method, simply return that argument back? I didn't realize you had to return something in the catch. SO much different than VB!!

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    but this:
    PHP Code:
    public string FormatPhone (string number)
            {
                try
                {
                    if (
    number.IndexOf("-") > || number.Length <7)
                    {
                        return 
    number;
                    } 
    is correct? in VB, the 'return number;' would be exit sub. I simply want to get out of the routine when the 'if' statement is TRUE.

  6. #6
    Addicted Member
    Join Date
    Feb 2002
    Location
    closed
    Posts
    196
    it's because you have a return statement within the scope of the try/catch block. I'm guessing the compiler wont see past that.

    move your return to AFTER the catch block ends - that is the normal flow (return a zero-length string as an abnormal flow in the catch)

Posting Permissions

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



Click Here to Expand Forum to Full Width