Results 1 to 3 of 3

Thread: [RESOLVED] Set return type of function based on internal variable type

  1. #1

    Thread Starter
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Resolved [RESOLVED] Set return type of function based on internal variable type

    Hi guys!

    Another question for you. Hopefully someone knows the answer to this one and is willing to point me in the right direction.

    What I have is a function that reads a WMI Query then returns a specific property that is specified in the argument for the function.

    What I'd like to do is make the function return the property in it's original type. I don't want to use object because I'd still have to cast it later.

    So basically I need a way to make a functions return type the same as the type of the variable. I'm not sure if this can be done with generics or not since I've never used them so pointers would be appreciated.

    Thanks everyone!
    My Blog.

    Ryan Jones.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Set return type of function based on internal variable type

    What you're asking for is not possible. Without generics, a functions return type must be set a design time. With generics, a functions return type must be set when you call it. Think about it. If you didn't know what type a function was going to return when you called it, how could you possibly use the result as its own type without a cast anyway? You couldn't declare a variable to assign it to as that type because you don't know what the type is, so you'd still have to assign it to an Object variable anyway.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: Set return type of function based on internal variable type

    I figured that would probably be the case but I just wanted to be sure. Guess I'll stick with the method I'm using then.

    Thanks for the reply.
    My Blog.

    Ryan Jones.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width