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
Solved

return a class from function

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
How to use odbc in vb to connect to ms sql 14 63
vb.net winforms sizing/resolution? 4 42
VB.Net. Reading xml value 6 30
Code enhancement 4 22
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 …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

829 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