|
-
Aug 2nd, 2004, 03:11 PM
#1
Thread Starter
Frenzied Member
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.
-
Aug 2nd, 2004, 03:28 PM
#2
Thread Starter
Frenzied Member
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?
-
Aug 2nd, 2004, 10:47 PM
#3
Fanatic Member
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();
}
-
Aug 4th, 2004, 07:33 AM
#4
Thread Starter
Frenzied Member
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!!
-
Aug 4th, 2004, 07:35 AM
#5
Thread Starter
Frenzied Member
but this:
PHP Code:
public string FormatPhone (string number)
{
try
{
if (number.IndexOf("-") > 0 || 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.
-
Aug 4th, 2004, 09:06 AM
#6
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|