user1899082
user1899082

Reputation:

Function in VB that doesn't have a return type

I am not much familiar with Visual Basic 6.0 and am not having a VB compiler installed, but I was looking at some VB code for some debugging and saw this:

Private Function IsFieldDeleted(oLayoutField As Object)
    Dim oColl As Collection
    Set oColl = GetFieldIdsForField(oLayoutField)

    IsFieldDeleted = (oColl.Count = 0)

    Set oColl = Nothing
End Function

In other functions I see they define the return type with an "As" for example "As Boolean" but this one does not have an "As" :D and then how they have used it is like this:

  If Not IsFieldDeleted(oRptField.GetUCMRLayoutField) Then
      Call oCollection.Add(oRptField, oRptField.ObjectKeyString)
      Call AddToNewLineSeperatedString(sCaseFldDescMsg, oFld.FieldDescription)
  End If

How is this working? Is it just like rewriting it and saying that the function returns an integer and compare the return type to be either 0 or 1? Or are there some other hidden tips in there?

Upvotes: 3

Views: 1484

Answers (1)

Steven Doggart
Steven Doggart

Reputation: 43743

When no type is specified, in VB.NET it assumes Object for the return type. In VB6, it assumes Variant. In VB.NET you can make things much more obvious by turning Option Strict On, but I don't believe that option was available in VB6.

The value that is returned, in reality, is still typed as a Boolean, but you are viewing the returned value as a Variant. So, to do it "properly", you really ought to cast the return value like this:

If Not CBool(IsFieldDeleted(oRptField.GetUCMRLayoutField)) Then
   ....
End If

Calling CBool casts the value to a Boolean instead of a Variant. This is unnecessary, though, since VB will use late-binding to determine the type of the return value is a boolean.

The best thing to do in this case is to change the function to As Boolean. Doing so will not break any existing code since that's all it ever returned anyway. However, if it's a public member in a DLL, that would break compatibility.

Upvotes: 4

Related Questions