|
|
||||||
|
#1
|
|
|
|
|
I have a very simple function I'm working on.
Function CarSpeed(ByVal kph As Integer) If kph < 0 Then _quote = "the car is in reverse" _actualspeed = kph ElseIf kph = 0 Then _quote = "the car is parked." _actualspeed = kph ElseIf kph > 0 And kph <= 10 Then _quote = "the car is moving slowly." _actualspeed = kph Else _quote = "the car is moving quickly." _actualspeed = kph End If 'New for VS 2005 apparently... Return kph End Function Originally, I built this using Visual Studio .NET 2003. I just downloaded the Visual Studio .NET 2005 express and when I initially typed in the old code I didn't have the line "Return kph" on the original version, and I got a warning saying, "Function CarSpeed doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used." So why do I need to include the line "Return kph" for VS 2005? |
|
|
|
#2
|
|
|
|
|
Military Smurf wrote:
[..] > > End Function > > Originally, I built this using Visual Studio .NET 2003. I just downloaded > the Visual Studio .NET 2005 express and when I initially typed in the old > code I didn't have the line "Return kph" on the original version, and I got a > warning saying, "Function CarSpeed doesn't return a value on all code paths. > A null reference exception could occur at run time when the result is used." > > So why do I need to include the line "Return kph" for VS 2005? Since you don't explicitly declare a type, this function returns an Object. If a function that returns an Object never sets its return value (either by the older FunctionName= syntax or with Return), then it will return a null reference. Callers attempting to do anything with the return value of the function will then most likely cause a null reference exception. VS2005 notices this and therefore suggests that the return value not be Nothing, which your Return statement duly does. That's the simple facts, now the commentary: - VS2003 with Option Strict On doesn't like your original code, which tells me you are running without Option Strict On - you may want to consider making this an absolute standard in the future. The time for additional typing sometimes required is more than compensated by the debugging time savings produced by working with strongly-typed code. - It's bad style to have 'side effects' in procedures, and worse to have functions that consist _only_ of side effects! A call to this function should be replaced with something like _actualspeed = kph _quote = DescribeSpeed(kph) where DescribeSpeed takes an Integer and returns a String. |
|
#3
|
|
|
|
|
"Larry Lard" wrote:
[..] > _actualspeed = kph > _quote = DescribeSpeed(kph) > > where DescribeSpeed takes an Integer and returns a String. > > -- > Larry Lard > Replies to group please > Thanks for that, and while I understand a lot of what you are saying, what are these "side effects" I need to be aware of? |
|
#4
|
|
|
|
|
If you didn't have the Return statement in your function before, I
presume that you were not calling it as a function. In that case, you should make the method a Sub instead of a function. |
|
#5
|
|
|
|
|
"Military Smurf" <MilitarySmurf> schrieb:
[..] > End Function > > Originally, I built this using Visual Studio .NET 2003. I just downloaded > the Visual Studio .NET 2005 express and when I initially typed in the old > code I didn't have the line "Return kph" on the original version, and I > got a > warning saying, "Function CarSpeed doesn't return a value on all code > paths. > A null reference exception could occur at run time when the result is > used." Use 'Sub' instead of 'Function' if 'CarSpeed' is not intended to return a value. |
|
#6
|
|
|
|
|
As addition to Herfried because he obvious missed that.
to make it even less troubleful use "AndAlso" instead of "And" Cor "Herfried K. Wagner [MVP]" <hirf-spam-me-here> schreef in bericht news:a916 [..] |
|
#7
|
|
|
|
|
if andalso orelse = new to you then
read the The Ballad of AndAlso and OrElse http://www.panopticoncentral.net/arc...08/18/179.aspx end if :-) Michel Posseth [MCP] "Cor Ligthert [MVP]" <notmyfirstname> schreef in bericht news:a648 [..] |
|
#8
|
|
|
|
|
Military Smurf wrote:
> "Larry Lard" wrote: > > - It's bad style to have 'side effects' in procedures, and worse to > > have functions that consist _only_ of side effects! A call to this > > function should be replaced with something like > > > > _actualspeed = kph > > _quote = DescribeSpeed(kph) > > > > where DescribeSpeed takes an Integer and returns a String. > > Thanks for that, and while I understand a lot of what you are saying, what > are these "side effects" I need to be aware of? You can read more about side effects here: <http://en.wikipedia.org/wiki/Side-effect_%28computer_science%29> but in short, a side effect is when a function changes something that it isn't obvious that it changes, typically a variable at some broader scope. An example: 'module level Dim a as integer Function Negate(n as integer) as integer Return -n End Function Function NegateWithSideEffect(n as integer) as integer a = a + 1 Return -n End Function Sub Example() Dim b as integer b = 3 b = Negate(b) 'now b = -3 and no surprises a = 2 b = NegateWithSideEffect(b) 'now b = 3 as expected 'BUT a has also changed! End Sub In this example, there is nothing to tell a caller of NegateWithSideEffect that calling this function will also change a. Real world examples don't have such 'giveaway' names! :) There's a guideline for coding called the Principle of Least Astonishment (more at <http://en.wikipedia.org/wiki/Principle_of_least_astonishment>) which basically says: don't do anything surprising. In this example changing a might well come as a surprise, and an unpleasant one, to an unwitting user of NegateWithSideEffect. Like all guidelines and principles, how rigorously to stick to them depends on a number of factors - throwaway programs for personal consumption, you can get away with anything you like; flying a space shuttle, get ten people to check every line. But in general it is better to get into the habit of doing things 'the right way', so that everything one does is better than it needs to be. |
|
|
| Similar Threads | |
| Thread | Thread Starter |
| Warning: error writing VTOC Warning: no backup labels Write labelfailed i canot format my new hard disk or use it. i found dublacate action like the following: root@dnuspc02 # format Searching for... |
YUOSEF RADI |
| What Cause this warning:warning BEC2004: Record tags at the same level should be unique. When I transfer the Flat file to XML,usually causes the warning like this: warning BEC2004: Record tags at the same level should be unique. Why? Thanks! |
linxd |
| Turning off a specific kind of gcc warning... Hi! ---- Does anyone know how I can turn off the following gcc warning: /home/gismobile/projects/xorg/xprint_renderext/xc/programs/Xserver/Xprint/ps/psrendertrap.c:492: the... |
Roland Mainz |
| A code fix for MSVC warning C4267 (64-bit compatibility warning, e.g. Boost Spirit) Microsoft Visual C++ warning C4267 (the number is not a typo) can occur e.g. when compiling Boost Spirit, or when doing std::size_t x = 0; std::cout << x; when compiled... |
Alf P. Steinbach |
| CommitChanges() - Warning: Node d not committed or rolled back at start of commit - Are these warning permanent Hi All, I had some strange JavaScript on my page that was breaking the second postback (I think) from the authoring page when saving. My event log has about 10-15 items... |
Angus Logan |
|
Privacy Policy | All times are GMT. The time now is 12:36 PM.
|
|
|