jvcoach23 Posted December 5, 2005 Posted December 5, 2005 I'm trying to write a function in a class. when I create the function, before I even put any real meat in the function, i'm getting the warning that the function doesn't return a value on all code paths. A null reference exception could occur at run time when teh result is used. I've done some digging but can't turn anything up. can anyone help me out on by explaining what is going on. I did this in vb 2003 and didn't run into it... hope someone can explain it to me. thanks shannon Quote JvCoach23 VB.Net newbie MS Sql Vet
Cags Posted December 5, 2005 Posted December 5, 2005 Basically VisualStudio is warning you of a potential problem in your program. I don't know much about vb, but heres a c# example hopefully it will be self explanatory. // function that returns a boolean private bool MyFunction() { if(a == b) { return true; } } In this example a simple function checks if two objects are equal, if they are it returns true, if they aren't the function doesn't return a value. VisualStudio is simply informing you that a logical code path through your function doesn't return a value. This can cause crashes in your application if for example you do.. Dim myBool as Boolean myBool = MyFunction() If myBool = True Then 'do something End If If MyFunction ends up following the logic path in MyFunction that doesn't return a value this could cause your application to crash. Quote Anybody looking for a graduate programmer (Midlands, England)?
Leaders snarfblam Posted December 5, 2005 Leaders Posted December 5, 2005 (edited) In the underlying .Net engine, a function must place its return value on the stack. If it does not, the code will not be verifiable. Depending on the end user's settings, this will cause the program to either not run at all, or simply crash, so it is important that a value is returned. If you are using VB, a default value is always placed on the stack. Allowing it to be returned could cause your program to crash. Most likely it will just produce odd or unexpected results. When you call a function it is expected behavior that the function will inspect input and provide a meaningful output. It is, therefore, considered good programming practice (or required programming practice in C#) that every possible code path explicitly returns a result so that a default value is not returned by accident. With reference types, a code path that returns a default returns a null reference, which may cause a NullReferenceException to be thrown, and without an exception handler in place, this will crash your application. With value types, these unexpected default values could cause exceptions due to overflow, out of bounds array access, division by zero, etc. In C# (at least 2.0, I'm not positive about 1.x) there is no provision for default values, therefore a value must be explicitly returned. Otherwise the resulting IL would be unverifiable and if allowed to run would crash the CLR. Edited December 5, 2005 by snarfblam Quote [sIGPIC]e[/sIGPIC]
jvcoach23 Posted December 6, 2005 Author Posted December 6, 2005 So if i'm using a function that is making a database call and returning a dataset, then I need to make sure that that dataset has at least a table and row in the table.. so if the db call returns nothing, i need to manually add a row.. am i getting this.. thanks shannon In the underlying .Net engine, a function must place its return value on the stack. If it does not, the code will not be verifiable. Depending on the end user's settings, this will cause the program to either not run at all, or simply crash, so it is important that a value is returned. If you are using VB, a default value is always placed on the stack. Allowing it to be returned could cause your program to crash. Most likely it will just produce odd or unexpected results. When you call a function it is expected behavior that the function will inspect input and provide a meaningful output. It is, therefore, considered good programming practice (or required programming practice in C#) that every possible code path explicitly returns a result so that a default value is not returned by accident. With reference types, a code path that returns a default returns a null reference, which may cause a NullReferenceException to be thrown, and without an exception handler in place, this will crash your application. With value types, these unexpected default values could cause exceptions due to overflow, out of bounds array access, division by zero, etc. In C# (at least 2.0, I'm not positive about 1.x) there is no provision for default values, therefore a value must be explicitly returned. Otherwise the resulting IL would be unverifiable and if allowed to run would crash the CLR. Quote JvCoach23 VB.Net newbie MS Sql Vet
Cags Posted December 6, 2005 Posted December 6, 2005 Using the example you specified you need to ensure that a dataset is always returned. If your database call returns null then you either have to return a null dataset (in which case you should check for this in the place you call the function to prevent null reference exceptions) or as you say you could create a new dataset manually and return that. What you shouldn't do is naturally exit the function without a return Dataset statement. Quote Anybody looking for a graduate programmer (Midlands, England)?
jvcoach23 Posted December 7, 2005 Author Posted December 7, 2005 playing around some more.. i have figured out that when i use a try and catch that that is when I'm getting my error. So i need to make sure I return a dataset in the catch and the error goes away... not sure if i want to do that.. but that is why i'm getting the warning.. thanks again for your help shannon Using the example you specified you need to ensure that a dataset is always returned. If your database call returns null then you either have to return a null dataset (in which case you should check for this in the place you call the function to prevent null reference exceptions) or as you say you could create a new dataset manually and return that. What you shouldn't do is naturally exit the function without a return Dataset statement. Quote JvCoach23 VB.Net newbie MS Sql Vet
Cags Posted December 7, 2005 Posted December 7, 2005 If you don't want to return a dataset do something like this... Private Sub myFunction() Dim mySet As DataSet mySet = getDataSet() If mySet Is Nothing Then ' handle null value Else ' proceed with dataset End If End Sub Private Function getDataSet() As DataSet Dim mySet As DataSet Try mySet = New DataSet Return mySet Catch ex As Exception Return Nothing End Try End Function Quote Anybody looking for a graduate programmer (Midlands, England)?
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.