Jump to content

quick question. [VB.net]

Go to solution Solved by Sauron,

It's not an error, it's a warning. It's warning you that if n isn't one of those 4 options the function won't return anything.

can someone tell my what im doing wrong with this function??

 

Public Function checkOperation(ByVal n As String, ByVal x As Double, ByVal y As Double)

        If n Is "+" Then
            Return x + y
        ElseIf n Is "-" Then
            Return x - y
        ElseIf n Is "×" Then
            Return x * y
        ElseIf n Is "÷" Then
            Return x / y
        End If
    End Function

ive been looking at this for 30min now... still dont understand why its returning an error when i debug it...

 

Severity	Code	Description	Project	File	Line	Suppression State
Warning	BC42105	Function 'CheckOperation' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.	BasicCalculator	C:\Users\pewpew\Google Drive\School Stuffs\Computer Prog\.Net Files\BasicCalculator\BasicCalculator\BasicCalcModule.vb	13	Active

this is the error warning

Link to comment
https://linustechtips.com/topic/1152084-quick-question-vbnet/
Share on other sites

Link to post
Share on other sites

Severity	Code	Description	Project	File	Line	Suppression State
Warning	BC42105	Function 'CheckOperation' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.	BasicCalculator	C:\Users\mah name\Google Drive\School Stuffs\Computer Prog\.Net Files\BasicCalculator\BasicCalculator\BasicCalcModule.vb	13	Active

here it is ^^

Link to comment
https://linustechtips.com/topic/1152084-quick-question-vbnet/#findComment-13269536
Share on other sites

Link to post
Share on other sites

Is the error "function checkOperation does not allows return something"?

From what I can see, you have 4 things that could be happening for which you return something. But what if the input is not one of these 4 things? In such a case, nothing would be returned.

 

Usually in this case what you do is have a default value, like return an error value/0/whatever.

Easiest way to fix it now is by doing something like this:

int answer = 0;

If n Is "+" Then
  	answer = x + y
  ElseIf n Is "-" Then
  	answer = x - y
  ElseIf n Is "×" Then
  	answer = x * y
  ElseIf n Is "÷" Then
  	answer = x / y
End If
  
return answer;

 

With this setup you make a variable and fill that with info, in the different if's.

But what if none of these ifs get triggered? Well in that case, it will always return answer. with a 0 value.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
https://linustechtips.com/topic/1152084-quick-question-vbnet/#findComment-13269538
Share on other sites

Link to post
Share on other sites

1 minute ago, Sauron said:

It's not an error, it's a warning. It's warning you that if n isn't one of those 4 options the function won't return anything.

aaaaa.. i had an idea it was something like dat... damn you VB.net >.< thanks.. ill use @minibois solution for now ^^

Link to comment
https://linustechtips.com/topic/1152084-quick-question-vbnet/#findComment-13269544
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×