0

I'm trying to return an object from a function.

E.g. Ive got a function populateDog that returns Dog

So in my aspx class I want to be able to be able to pass in Lassie as the name of the dog(I have a dog class) and have the function return the object with the data it populated.

So that in my aspx class i can go lassie.color, lassie.breed

Main Goal is: lbl.txt = Lassie.Color

Thanks

EDIT

Public Function populateDog(ByVal dName As String) As dog
        dbConnection()
        Dim ObjDog As New dog(dName)
        ObjDog.sBreed = "Collie"
        Return ObjDog
    End Function

The idea was to have a database and I would eventually pass in an ID to query results and return it. For now though I just wanna get this understanding and move forward.

3
  • 2
    What's exactly your problem? Can you show your code so far? Commented Mar 13, 2011 at 22:12
  • @AlfonsoML Edited orginal post. Thanks - Problem is once i return the dog object I dont know how to use it. e.g. Dim Lassie = populateDog("lassie") then I cant do Lassie.sBreed? Commented Mar 13, 2011 at 22:22
  • Specify the class of the Lassie variable: Dim Lassie as dog = populateDog("lassie") Commented Mar 13, 2011 at 22:26

1 Answer 1

3
Public Function populateDog(ByVal dName As String) As dog
        dbConnection()
        Dim ObjDog As New dog(dName)
        ObjDog.sBreed = "Collie"
        ObjDog.Color = "White"
        Return ObjDog
    End Function

and

Dim Lassie as dog
Lassie = populateDog("Lassie")
lbl.Text = Lassie.Color

assuming your dog class is something like

Class dog
    Public sBreed As String
    Public Color As String
    ' other properties and functions
End Class
Sign up to request clarification or add additional context in comments.

2 Comments

@AlfonsoML Is it better to have my populateDog inside the Dog class or should I have it in my database class? Thanks
@kb88 It would fit better in your database class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.