2

I understand that the type structure is the ancestor of the class function. However coding a Type structure is quick, simple and easy. Thus I tried the following without success.

Working inside a class in VBa, I tried to return multiple variables from an internal function to another function within the class. I have tried to do that with a type element, however there is an internal conflict within the class function.

I have two questions: 1.) Are types not allowed in classes? 2.) What is a good practice method for returning multi-variable outputs from a function within a class?

Private Type checkResult
    status As Boolean
    errorp As String
End Type

Function CheckPTID(PTid As String) As checkResult
    Dim plen As Boolean ' PT length
    Dim numdash As Boolean ' numbers and dashes
    Dim titles As Boolean ' Tiles correct

    ' initials
    plen = False
    numdash = False
    titles = False

    ' Checks
    If Len(PTid) = 8 Then plen = True
    If InStr(PTid, "-") > -1 Then numdash = True
    If (Left(PTid, 2) = "XP" Or Left(PTid, 2) = "XA") Then titles = True

    ' output
    If (plen = False Or numbdash = False Or titles = False) Then
        CheckPTID.status = False
        If Not plen Then CheckPTID.errorp = "** Error Name length incorrect:" & PTid
        If Not numdash Then CheckPTID.errorp = "** Error Name format incorrect:" & PTid
        If Not titles Then CheckPTID.errorp = "** Error Name titles incorrect:" & PTid
    Else
        CheckPTID.status = True
        CheckPTID.errorp = "N/A"
    End If

End Function

Error given in above code is: User-defined type not defined. Thanks

EDIT:

To help with the understanding of the structure. The following is shown :

Class
|--Properties
|--Function: CheckPTID
|--Type: checkResult

The real question is, how does one use the type function directly in a class without creating a new class.

5
  • On which line does the error get thrown? Commented Nov 14, 2016 at 11:31
  • use a sub class, just containing public vars, not properties. public status as Boolean and public errorp as string in a class checkResult You can create Private types Commented Nov 14, 2016 at 11:32
  • Thanks @Nathan_Sav, can you create private Types inside a class? Commented Nov 14, 2016 at 11:52
  • If you make your function Private (and correct the numbdash typo) it should work. Commented Nov 14, 2016 at 13:16
  • You can return multiple variables passing them as arguments to Sub or Function byref (wich is default), and modifying them within that Sub or Function. Commented Nov 14, 2016 at 13:38

2 Answers 2

1

Working inside a class in VBa, I tried to return multiple variables from an internal function to another function within the class.

If I have understood your above comment, then you are trying to use code outside of the local/module level. As per @Nathan_Sav comment above, declare everything publicly. See below.

Option Explicit

Public plen As Boolean ' PT length
Public numdash As Boolean ' numbers and dashes
Public titles As Boolean ' Tiles correct

Public Type checkResult
    public status As Boolean
    public errorp As String
End Type


Public Function CheckPTID(PTid As String) As checkResult

'initials
plen = False
numdash = False
titles = False

'Checks
If Len(PTid) = 8 Then plen = True
If InStr(PTid, "-") > -1 Then numdash = True
If (Left(PTid, 2) = "XP" Or Left(PTid, 2) = "XA") Then titles = True

'output
If (plen = False Or numbdash = False Or titles = False) Then
    CheckPTID.status = False
    If Not plen Then CheckPTID.errorp = "** Error Name length incorrect:" & PTid
    If Not numdash Then CheckPTID.errorp = "** Error Name format incorrect:" & PTid
    If Not titles Then CheckPTID.errorp = "** Error Name titles incorrect:" & PTid
Else
    CheckPTID.status = True
    CheckPTID.errorp = "N/A"
End If

End Function

Please let me know how this works for you as I have not tested it out! :)

Sign up to request clarification or add additional context in comments.

2 Comments

I think that works outside of a class, but in a class the error is: Compile error: User-defined type not defined this happens on the Public Function CheckPTID(PTid As String) As checkResult line. That leads me to think that the structure type is not allowed in a class? but rather a class with a class?
hi any progress with this??
0

Your narrative says: "I tried to return multiple variables from an internal function to another function within the class." . Thus I assume you want to use that Type (and CheckPTID() method too) inside your class only, i.e. you don't want to use that type in any other module.

Then declare Private that type and any other function inside your class that returns a variable of that type (like CheckPTID() does)

Furthermore you function has to be amended, since you have to:

  • initialize a variable of checkResult type by declaring it

    Dim retCheckPTID As checkResult
    
  • use it throughout your sub to set its properties

    retCheckPTID.status = False
    If Not plen Then retCheckPTID.errorp = "** Error Name length incorrect: " & PTid
    ...
    
  • and finally set the return value of your function to it

        CheckPTID = retCheckPTID
    End Function
    

here comes the entire code:

Option Explicit

Private Type checkResult
    status As Boolean
    errorp As String
End Type

Private Function CheckPTID(PTid As String) As checkResult
    Dim plen As Boolean ' PT length
    Dim numdash As Boolean ' numbers and dashes
    Dim titles As Boolean ' Tiles correct
    Dim retCheckPTID As checkResult

    ' initials
    plen = False
    numdash = False
    titles = False

    ' Checks
    If Len(PTid) = 8 Then plen = True
    If InStr(PTid, "-") > -1 Then numdash = True
    If (Left(PTid, 2) = "XP" Or Left(PTid, 2) = "XA") Then titles = True

    ' output
    If (plen = False Or numdash = False Or titles = False) Then
        retCheckPTID.status = False
        If Not plen Then retCheckPTID.errorp = "** Error Name length incorrect: " & PTid
        If Not numdash Then retCheckPTID.errorp = "** Error Name format incorrect:" & PTid
        If Not titles Then retCheckPTID.errorp = "** Error Name titles incorrect: " & PTid
    Else
        retCheckPTID.status = True
        retCheckPTID.errorp = "N/A"
    End If

    CheckPTID = retCheckPTID
End Function

1 Comment

@CromeX, did you get through it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.