|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
How do you get around this?that just now I have forgotten it for the moment. It happens when you're in a totally different rut of thought. Bail me out here. Just to quote an example: static void Main(string[] args) { Console.WriteLine((new Class1()).SomeMethod((Object)19)); } public int SomeMethod(System.Object obj) { if (obj == null) throw new System.Exception("The argument is null."); if ((int)obj >= 10) return (int)obj; } Error: Class1.cs(13): 'Exception.Class1.SomeMethod(object)': not all code paths return a value How do you get out of this? Hi Walter,
public int SomeMethod(System.Object obj) { //... if ((int)obj >= 10) return (int)obj; } What does it return if (int)obj is NOT greater than 10? -- Show quoteHTH, Kevin Spencer Microsoft MVP Professional Chicken Salad Alchemist Sequence, Selection, Iteration. "Water Cooler v2" <wtr_***@yahoo.com> wrote in message news:1153924967.384452.239030@m73g2000cwd.googlegroups.com... > Sorry for the stupid question, but I've been there and done that. Just > that just now I have forgotten it for the moment. It happens when > you're in a totally different rut of thought. Bail me out here. > > Just to quote an example: > > static void Main(string[] args) > { > Console.WriteLine((new Class1()).SomeMethod((Object)19)); > } > > public int SomeMethod(System.Object obj) > { > if (obj == null) > throw new System.Exception("The argument is null."); > > if ((int)obj >= 10) > return (int)obj; > } > > > Error: Class1.cs(13): 'Exception.Class1.SomeMethod(object)': not all > code paths return a value > > > > How do you get out of this? > Is it because you're not returning a value if the parameter passed is
null? (I know you're throwing an exception but the warning points to the fact that this particular code path doesn't return anything. HTH Martin Water Cooler v2 wrote: Show quote > Sorry for the stupid question, but I've been there and done that. Just > that just now I have forgotten it for the moment. It happens when > you're in a totally different rut of thought. Bail me out here. > > Just to quote an example: > > static void Main(string[] args) > { > Console.WriteLine((new Class1()).SomeMethod((Object)19)); > } > > public int SomeMethod(System.Object obj) > { > if (obj == null) > throw new System.Exception("The argument is null."); > > if ((int)obj >= 10) > return (int)obj; > } > > > Error: Class1.cs(13): 'Exception.Class1.SomeMethod(object)': not all > code paths return a value > > > > How do you get out of this? Pritcham wrote:
> Is it because you're not returning a value if the parameter passed is No, it's not the null code path that's causing the problem - it's the> null? (I know you're throwing an exception but the warning points to > the fact that this particular code path doesn't return anything. non-null, less than 10 code path. The compiler is perfectly happy for something to guarantee it will throw an exception instead of returning a value. Jon Water Cooler v2 wrote:
Show quote > Sorry for the stupid question, but I've been there and done that. Just What if obj is < 10? You still need to return something.> that just now I have forgotten it for the moment. It happens when > you're in a totally different rut of thought. Bail me out here. > > Just to quote an example: > > static void Main(string[] args) > { > Console.WriteLine((new Class1()).SomeMethod((Object)19)); > } > > public int SomeMethod(System.Object obj) > { > if (obj == null) > throw new System.Exception("The argument is null."); > > if ((int)obj >= 10) > return (int)obj; > } Return something from all paths, just like the error says.> > > Error: Class1.cs(13): 'Exception.Class1.SomeMethod(object)': not all > code paths return a value > > > > How do you get out of this? -cd The question should be... what does SomeMethod return if "obj < 10". You
have defined the function as returning an int, so it must return an int. As coded, if "obj>=10" then you return a value. If this test fails, you do not return anything. Steve Show quote "Water Cooler v2" <wtr_***@yahoo.com> wrote in message news:1153924967.384452.239030@m73g2000cwd.googlegroups.com... > Sorry for the stupid question, but I've been there and done that. Just > that just now I have forgotten it for the moment. It happens when > you're in a totally different rut of thought. Bail me out here. > > Just to quote an example: > > static void Main(string[] args) > { > Console.WriteLine((new Class1()).SomeMethod((Object)19)); > } > > public int SomeMethod(System.Object obj) > { > if (obj == null) > throw new System.Exception("The argument is null."); > > if ((int)obj >= 10) > return (int)obj; > } > > > Error: Class1.cs(13): 'Exception.Class1.SomeMethod(object)': not all > code paths return a value > > > > How do you get out of this? > "Water Cooler v2" wrote: Just make sure all paths return a value. In your example:> Sorry for the stupid question, but I've been there and done that. Just > that just now I have forgotten it for the moment. It happens when > you're in a totally different rut of thought. Bail me out here. public int SomeMethod(System.Object obj) { int i = 0; if (obj == null) throw new System.Exception("The argument is null."); if ((int)obj >= 10) i = (int)obj; return i; } Show quote > > Just to quote an example: > > static void Main(string[] args) > { > Console.WriteLine((new Class1()).SomeMethod((Object)19)); > } > > public int SomeMethod(System.Object obj) > { > if (obj == null) > throw new System.Exception("The argument is null."); > > if ((int)obj >= 10) > return (int)obj; > } > > > Error: Class1.cs(13): 'Exception.Class1.SomeMethod(object)': not all > code paths return a value > > > > How do you get out of this? > > |
|||||||||||||||||||||||