Solved

return a class from function

Posted on 2006-07-12
4
228 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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 …
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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

726 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