?
Solved

return a class from function

Posted on 2006-07-12
4
Medium Priority
?
229 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

801 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