Reputation: 3311
I'm not a pro and I learn by myself a few of coding rules.
I started with vba
and now I'm approaching to vb.net
but my knowledge is very poor.
I wrote this function to extract a column from a recordset (query on DB) and put it into an Hashset of string.
Trying to handle errors on wrong number of column, I used these criteria:
1) declare "mErr
" as boolean
in code that calls the function;
2) send "mErr
" byRef
to the function;
3) when error occur: change mErr
to true and insert into mHash
an empty string.
Public Function RSCol(ByVal mRS As Object, ByVal mCol As Byte, ByRef mErr As Boolean)
Dim i As Long
Dim mHash As New HashSet(Of String)
If mRS.GetUpperBound(0) < mCol Then
mErr = True
mHash.Add("")
Return mHash
Exit Function
End If
For i = 0 To mRS.GetUpperBound(1)
mHash.Add(mRS(mCol, i))
Next
Return mHash
End Function
It seems to work but I think isn't a good coding and I would like to improve my coding skill.
Every suggest is appreciated.
Upvotes: 2
Views: 1968
Reputation:
In your code :
Exit Function
is unreachable since theReturn
statement above theExit Function
function will transfer the control out to the function.More over a function should return a value otherwise you will get a warning. in such cases you can use
Sub
.
You code can be
Public Function RSCol(ByVal mRS As Object, ByVal mCol As Byte, ByRef mErr As Boolean) As HashSet(Of String)
Dim loopCounter As Long
Dim mHash As New HashSet(Of String)
Try
If mRS.GetUpperBound(0) < mCol Then
Throw New Exception("")
End If
For loopCounter = 0 To mRS.GetUpperBound(1)
mHash.Add(mRS(mCol, loopCounter))
Next
Catch ex As Exception
mHash.Add(ex.ToString())
End Try
Return mHash
End Function
You can refer Try..Catch mechanism in detail Click Here For more about Exceptions
Since i notice that your naming conventions are so poor i suggest you to go through this article by Microsoft
Upvotes: 1
Reputation: 13971
Before answering your question let me make clear about two things:
Exceptions are those which can be handled at the run time whereas errors cannot be handled.
It is always a good practice to handle exception/errors effectively.
Here comes Try-Catch-Throw for our rescue.
Basic Syntax :
Try
' Do something in here that
' might raise an error.
Catch
' Handle exceptions that occur within
' the Try block, here.
Finally
' Perform cleanup code in here.
End Try
Try: A Try block identifies a block of code for which particular exceptions will be activated. It's followed by one or more Catch blocks.
Catch: A program catches an exception with an exception handler at the place in a program where you want to handle the problem. The Catch keyword indicates the catching of an exception.
Finally: The Finally block is used to execute a given set of statements, whether an exception is thrown or not thrown. For example, if you open a file, it must be closed whether an exception is raised or not.
Throw: A program throws an exception when a problem shows up. This is done using a Throw keyword.
If you are not getting any exception in program also it is recommended to use the try catch blocks
Upvotes: 0
Reputation: 2061
You could use exceptions to control error on functions. When you detect something is going wrong within your function, you throw it:
if ( something ) then 'check if something bad happened
Throw New Exception("X has occurred")
end if
Then, when this function which throw this exception, you should catch it like:
try
' Code with the call to the function which throws the exception
catch excp as Exception
'Your control code for this situation, as example just show it
MessageBox.show(excp.Message)
end
Upvotes: 0