Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB How-To
|
VB How-To Ask your "How do I do this with VB?" questions in this forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 2nd, 2004, 02:50 PM
Registered User
 
Join Date: Jun 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default How do I make a function return a class?

I'm doing this in order to implement a better error handling routine in my code:

---> Class cMensagens:

Private mvarMsg As String
Private mvarRes As Boolean

Public Property Get Msg() As String
    Msg = mvarMsg
End Property

Public Property Let Msg(ByVal vMsg As String)
    mvarMsg = vMsg
End Property


Public Property Get Res() As Boolean
    Res = mvarRes
End Property

Public Property Let Res(ByVal vRes As Boolean)
    mvarRes = vRes
End Property


--> Form:

Private Function CanFinish(idT As Long, numO As Integer) As cMensagens

   ...

    If rst.EOF Then
        CanFinish.Res = False 'PROBLEM HERE!!!
        CanFinish.Msg = "Error message"
     End If

End Function

But I always get the "Object Variable or With block variable not set" error when I try to set the return values of the function. What am I doing wrong?

Thanks!

Rui
 
Old November 2nd, 2004, 04:16 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

you have to do something like this

[code]
Private Function CanFinish(idT As Long, numO As Integer) As cMensagens

   ...

     dim Objmsg as cmensagens
     set objmsg = new cmensagens
...
    If rst.EOF Then
   objmsg .Res = False 'PROBLEM HERE!!!
        objmsg .Msg = "Error message"
        set CanFinish = objmsg
        set objmsg = nothing


     End If

End Function


HTH

Gonzalo
 
Old November 2nd, 2004, 07:49 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts
Default

CanFinish must be initialized before using it:

Private Function CanFinish(idT As Long, numO As Integer) As cMensagens
set canfinish = new cMensages '' <- this was missing


and when you use it:

dim cTmp as cMensagen
set cTmp = CanFinish(...)

Marco
 
Old November 3rd, 2004, 06:08 AM
Registered User
 
Join Date: Jun 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks a lot guys! Problem solved!






Similar Threads
Thread Thread Starter Forum Replies Last Post
return collection by function in class jimusa Visual Basic 2005 Basics 1 July 30th, 2008 03:18 PM
function return collection in class by VB jimusa Visual Studio 2005 1 July 30th, 2008 03:12 PM
return more then one array from class file Komila ASP.NET 2.0 Basics 4 June 27th, 2007 07:10 PM
How to make a class public johnjonsson VS.NET 2002/2003 0 October 6th, 2003 02:17 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.