Solved

return a class from function

Posted on 2006-07-12
4
225 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
ID: 17090454
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
ID: 17090521

   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
ID: 17090721
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
ID: 17090725
oops, I didn't refresh the page
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

776 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