in VB, you can use EXIT SUB to get out of a sub early. What is the syntax for c#?
Printable View
in VB, you can use EXIT SUB to get out of a sub early. What is the syntax for c#?
ok I THINK I got that resolved but here is another problem along the same lines:
i get an error saying Not all code paths return a value. and the name FormatPhone is underlined.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);
}
}
what's that mean? is there something wrong with the code?
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();
}
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!!
but this:
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.PHP Code:public string FormatPhone (string number)
{
try
{
if (number.IndexOf("-") > 0 || number.Length <7)
{
return number;
}
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)