Solved

return a class from function

Posted on 2006-07-12
4
223 Views
Last Modified: 2010-04-23
I have a function in vb6. Which returns an object.
I create a varaiable as an object, set it to a class and return it.

function myfunction(stepNameLong) as object

       dim stepToBeProgrammed  as object

       select Case true
       Case InStr(stepNameLong, "xxx")
                stepToBeProgrammed = ClsMyClass
        End Select

        myfunction=stepToBeProgrammed

end function


when I do it in vb.net
I get an error when I try to set the class to the object

       dim stepToBeProgrammed  as object
        stepToBeProgrammed = ClsMyClass

If I do this

       dim stepToBeProgrammed  = ClsMyClass

it works, but I get an error at the bottom when I try to return the class
since it gets declared in the case statement.  It is a compile time error.

        myfunction=stepToBeProgrammed


0
Comment
Question by:jackjohnson44
  • 2
4 Comments
 
LVL 8

Expert Comment

by:doobdave
Comment Utility
Hi there,

What's the error message you're getting please?

Is the snippet you provided the full code?
If not, please give a bit more code so as we can see exactly what's going on.

Best Regards,

David
0
 
LVL 8

Accepted Solution

by:
Shakti109 earned 500 total points
Comment Utility

   Public Function myfunction(ByVal mu As Integer) As Object

      Dim stepToBeProgrammed As Object
      Select Case mu
         Case 1
            stepToBeProgrammed = New MyNewClass
         Case 2
            stepToBeProgrammed = New MyNewClass
      End Select
      myfunction = stepToBeProgrammed

   End Function

This compiles and runs just fine, even with option strict/explicit on.


It looks like you are missing the NEW keyword in your function :

function myfunction(stepNameLong) as object

       dim stepToBeProgrammed  as object

       select Case true
       Case InStr(stepNameLong, "xxx")
                stepToBeProgrammed = NEW ClsMyClass
        End Select
        myfunction=stepToBeProgrammed

end function

You need to create an instance of the class to pass back.

0
 
LVL 35

Expert Comment

by:YZlat
Comment Utility
try

function myfunction(stepNameLong) as object

       dim stepToBeProgrammed  as object

       select Case true
       Case InStr(stepNameLong, "xxx")
                stepToBeProgrammed = New ClsMyClass()
        End Select

        myfunction=stepToBeProgrammed

end function
0
 
LVL 35

Expert Comment

by:YZlat
Comment Utility
oops, I didn't refresh the page
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now