Function giving warning saying no return values - vb.net

Function LoginPass() As Boolean
Dim LogPass As New waxClass
Dim Ldt As DataTable = LogPass.LoginPass(LCase(UserName_TextBox.Text),
LCase(UserPass_TextBox.Text))
If Ldt.Rows.Count > 0 Then
waxClass.LMUser = Ldt.Rows(0).Item("Username").ToString
'Utility.LMUserID = Ldt.Rows(0).Item("UserID").ToString
Return True
End If
End Function
When I try to use the above function it gives a warning saying:
Function 'LoginPass' doesn't return a value on all code
paths. Are you missing a 'Return' statement?

Your function requires a Boolean return value.
You do not have a return value if Ldt.Rows.Count <= 0, so that is where the warning is coming from. Add and Else Return False and you should be fine.
If Ldt.Rows.Count > 0 Then
waxClass.LMUser = Ldt.Rows(0).Item("Username").ToString
'Utility.LMUserID = Ldt.Rows(0).Item("UserID").ToString
Return True
Else
Return False
End If

Related

visual basic .net Function have error return

So the error is BC42353 ( Function ValidateInputFields doesn't return a value on all code paths. Are you missing a Return statement? ) I'm getting this error twice. I put exclamation points on the lines that it says the errors are on.
Private Function CekBarcode(sID As String, cost As Long, price As Long, ByVal vslist As DataGridView) As Boolean
Dim i As Long
Try
For i = 1 To vslist.Rows.Count - 1
If sID = vslist.Rows(i - 1).Cells(1).Value Then
' CekBarcode = True
vslist.Rows(i - 1).Cells(10).Value = vslist.Rows(i - 1).Cells(10).Value + 1
Return True
Exit For
Else
Return False
End If
Next
Catch
Return False
End Try
End Function
Private Function CekBarcode(sID As String, cost As Long, price As Long, ByVal vslist As DataGridView) As Boolean
Dim result As Boolean = False
'maybe to add this below line /check is there more than one row in grid
If vslist.Rows.Count < 2 Then Return result
Try
For i = 1 To vslist.Rows.Count - 1
If sID = vslist.Rows(i - 1).Cells(1).Value.ToString() Then
' CekBarcode = True
vslist.Rows(i - 1).Cells(10).Value = vslist.Rows(i - 1).Cells(10).Value + 1
result = True
Exit For
End If
Next
Catch
result = False
End Try
Return result
End Function
You don't seem to use either cost or price and you probably want to specify ByRef on the DGV to update it.
Private Function CekBarcode(sID As String, ByRef vslist As DataGridView) As Boolean
Dim result As Boolean = False
Try
For Each row As DataRow In vslist.Rows
If row.Item(1).Equals(sID) Then
row.Item(10) = CInt(row.Item(10)) + 1
result = True
Exit For
End If
Next row
Catch
'result = False ' Not required - result is only changed within the If
End Try
Return result
End Function
Or you can dispense with the result variable and Exit For:
Private Function CekBarcode(sID As String, ByRef vslist As DataGridView) As Boolean
Try
For Each row As DataRow In vslist.Rows
If row.Item(1).Equals(sID) Then
row.Item(10) = CInt(row.Item(10)) + 1
Return True
End If
Next row
Catch
' Fall through to return False
End Try
Return False
End Function

Custom error handling vba

I have a cell that is a brutal nested if and if error. I don't want to have to paste my vlookup 5 times in the formula. Instead, I'd like to have a macro check the errors. The special case is 0 or 1.
I wrote this function to get the value of the vlookup.
Public Function ValidateRealization(value As String)
Dim validate As String
If value = "#N/A" Or value = "#VALUE!" Or value or = "#REF!" Or value = "#DIV/0!" Or value = "#NUM!" Or value = "#NAME?" Or value = "#NULL!" Or value = "0" Or value = "1" Then
validate = ""
Else
validate = value
End If
ValidateRealization = validate
End Function
However, I know it can be improved for one when I set the value, the formatting as a percentage is overridden.
Is there a better way to do this? Maybe by getting the active cells range, and setting the value property?
You are bringing in a string and not a value, so the return is a string.
set the parameter as variant and test for error:
Public Function ValidateRealization(value) As Variant
Dim validate As Variant
If Not IsError(value) Then
If value = 0 Or value = 1 Then
validate = ""
Else
validate = value
End If
Else
validate = ""
End If
ValidateRealization = validate
End Function
Which can be simplified to:
Public Function ValidateRealization(value) As Variant
ValidateRealization = ""
If Not IsError(value) Then
If Not (value = 0 Or value = 1) Then
ValidateRealization = value
End If
End If
End Function
2 points: first, you have ervalue which is undefined. Go to the top of your module, and add the line Option Explicit. Then go "Tools" > "Options..." and tick "Require Variable Declaration".
Secondly: Why not accept a Variant instead of a String, and use IsError?
Public Function ValidateRealization(value As Variant) As Variant
If IsError(value) Or CStr(value) = "0" Or CStr(value) = "1" Then
ValidateRealization = ""
Else
ValidateRealization = value
End If
End Function
{EDIT} If you have converted your value to a String before passing it, then use Select Case
Public Function ValidateRealization(value As Variant) As Variant
If IsError(value) Then
ValidateRealization = ""
Else
Select Case CStr(value)
Case "#N/A", "#VALUE!", "#REF!", "#DIV/0!", "#NUM!", "#NAME?", "#NULL!", "0", "1"
ValidateRealization = ""
Case Else
ValidateRealization = value
End Select
End If
End Function

Visual Basic - function to check if number is binary or not

Im code newbie and im stuck with this code. It appears that i always get True as response from this function. What am i doing wrong ?
Private Function binary() As Boolean
Dim number, temp As Integer
Dim status As Boolean
TextBox1.Text = number
status = True
While (True)
If (number = 0) Then
Exit While
Else
temp = number Mod 10
If (temp > 1) Then
status = False
Exit While
End If
number = number / 10
End If
End While
Return status
End Function
You have your assignment the wrong way around:
TextBox1.Text = number
With this, number will always be 0, its initial value, so your While loop exits immediately, every time. It should be:
number = Convert.ToInt32(TextBox1.Text)
Or better yet, pass it in as a parameter to the function:
Private Function binary(number as Integer) As Boolean
Dim temp As Integer
Dim status As Boolean
status = True
While (True)
If (number = 0) Then
Exit While
Else
temp = number Mod 10
If (temp > 1) Then
status = False
Exit While
End If
number = number / 10
End If
End While
Return status
End Function
Then:
Dim isBinary as Boolean
isBinary = binary(Convert.ToInt32(TextBox1.Text))

Function 'DisplayArray' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used

I keep getting a error and because of this my code wont work. The error is "Function 'DisplayArray' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used". I can't get rid of this error. I'm new to programming, can anyone help?
Private Function DisplayArray() As String
Dim j As Integer = 0
ReDim Preserve Array(UpperSub)
Dim AddNum As Double = 0.0
txtAddNum.Focus()
If Double.TryParse(txtAddNum.Text, AddNum) Then
If AddNum > 100 Then
MessageBox.Show("Number must be below 100")
ElseIf AddNum < 0 Then
MessageBox.Show("Number must be above 0")
Else
Array(UpperSub) = CDec(AddNum)
UpperSub = UpperSub + 1
End If
Else
MessageBox.Show("Value has to be a number")
End If
txtAddNum.Clear()
txtDisplay.Clear()
For j = 0 To UpperSub - 1
txtDisplay.Text = txtDisplay.Text _
& CStr(Array(j)) & ControlChars.NewLine
Next
txtNumberOfScores.Text = CStr(UpperSub)
End Function
A Function is designed to return a result, using the Return keyword. Your Function does not have a Return statement. Since none of the possible code paths (determined by branching on the If statements as well as the flow from beginning to end) return a value, you're getting this part of the error message: "Function 'DisplayArray' doesn't return a value on all code paths."
The second part of the error message means that if you tried to assign the return value of the Function to a variable, like this:
Dim result As String = DisplayArray()
You'd get a null value, as nothing is returned from the function.
The simplest solution is to change from a Function to a Sub. Subs in VB.NET do not return a value. So change
Private Function DisplayArray() As String
To
Private Sub DisplayArray()
And
End Function
To
End Sub
Note that the As String() in the Function declaration says this method will return a value that is a String, and the Sub has no return value (again, because it doesn't return a value).
To make this a Function that returns a value, you'll have to return at least one value from the method. Here's an example:
Private Function DisplayArray() As String
Dim j As Integer = 0
ReDim Preserve Array(UpperSub)
Dim AddNum As Double = 0.0
txtAddNum.Focus()
If Double.TryParse(txtAddNum.Text, AddNum) Then
If AddNum > 100 Then
MessageBox.Show("Number must be below 100")
Return String.Empty
ElseIf AddNum < 0 Then
MessageBox.Show("Number must be above 0")
Return String.Empty
Else
Array(UpperSub) = CDec(AddNum)
UpperSub = UpperSub + 1
txtAddNum.Clear()
txtDisplay.Clear()
For j = 0 To UpperSub - 1
txtDisplay.Text = txtDisplay.Text _
& CStr(Array(j)) & ControlChars.NewLine
Next
Return CStr(UpperSub)
End If
Else
MessageBox.Show("Value has to be a number")
Return String.Empty
End If
End Function
Essentially, if the validation fails, an empty string is returned. If the validation passes, the rest of the code is executed and the string value of UpperSub is returned.
You could then assign it to the TextBox like this:
txtNumberOfScores.Text = DisplayArray()
The above is a simple example based on your posted code, intended to show you how to return values from a Function. Adjust it to fit your needs (or use a Sub instead). Given that you want to update the display of the txtDisplay with the array and txtNumberOfScores as well you should do fine with a Sub.

IIF-Else statement Never Reachs Else Condition

Im using this custom Code in SQL Reporting Services. The problem is it NEVER reachs the false condition. If the condition is NOT true, the function won't return "false". It works fine when the value is True (prints "X") , otherwise I get an #Error .
I'm calling the function from a textbox :
= IIF(Code.CodeExist(Variables!MyCode.Value) = true, "X", "")
function CodeExist( ByVal Codigo As String) as Boolean
dim i as integer
dim Centinela as integer
i = 0
Centinela = 0
for i = 0 To Report.Parameters!CodeList.Count()
if Report.Parameters!CodeList.Value(i) = Codigo Then
Centinela = 1
Exit For
End If
Next
If Centinela = 1 Then
Return true
Else
Return false // IT NEVERS RETURN FALSE even if Centinela = 0
End If
End function
I don't know what's happening. Thanks in advance.
VBA has no "Return" statement. To return a value you'd use (eg)
....
If Centinela = 1 Then
CodeExist = True
Else
CodeExist = False
End If
End Function
or (much tidier):
....
CodeExist = (Centinela = 1)
End Function