Results 1 to 23 of 23

Thread: A function to return different types of objects.

Hybrid View

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2002
    Location
    http://www.vbforums.com
    Posts
    164

    A function to return different types of objects.

    Okay, I think VB.Net doesn't allow polymorphism between functions that differ only by the return types.

    I have a class called Manager and it has a collection property called Cards. Within this Cards property, I have stored different objects (different class).

    So what I want to do is to return the items whichever needed. Below is an example of what I want. (The codes do not work!)

    VB Code:
    1. 'In class Manager.
    2. '**************
    3.  
    4. public property Item(byval vIndex as Integer) as clsCardA 'Or clsCardB or clsCardC?
    5.  
    6.     'Methods to get an object from the Manager collection property with the index passed in.)
    7.  
    8. end property

    So what it'll do is to check the index, get the object and return the appropriate object.

    I know I can put the function return type as object but then when I try to use the function, it won't show me the public methods, properties of that returned object.

    Please help.
    "If ignorance is bliss, that probably explain why I'm in a such a mess right now!!"

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Yeah you can't have overloaded functions that differ only in return type. Then it can't figure out which one you are trying to call. This is a good example of when to use ByRef on a Sub instead. Nevermind I noticed you are using a property. I think its best to return object. To see intellisence for the returned object just cast it to the strong type that it actually is.
    VB Code:
    1. Dim crdA As clsCardA=CType(clsMngr.Item(1),clsCardA)
    The alternative is to have all cards be derived from a base class or implement the same card interface. Then return the base class/interface from the property and cast from that.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2002
    Location
    http://www.vbforums.com
    Posts
    164
    Originally posted by Edneeis
    Yeah you can't have overloaded functions that differ only in return type. Then it can't figure out which one you are trying to call. This is a good example of when to use ByRef on a Sub instead. Nevermind I noticed you are using a property. I think its best to return object. To see intellisence for the returned object just cast it to the strong type that it actually is.
    VB Code:
    1. Dim crdA As clsCardA=CType(clsMngr.Item(1),clsCardA)
    The alternative is to have all cards be derived from a base class or implement the same card interface. Then return the base class/interface from the property and cast from that.
    Thanks for the prompt reply. if I have implemented interface then the codes in the interface will be very long. The same goes to base class. The cards are similar in certain area only. Most of them are different when it comes to methods.
    "If ignorance is bliss, that probably explain why I'm in a such a mess right now!!"

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jan 2002
    Location
    http://www.vbforums.com
    Posts
    164
    And also, I don't want the clsCardA, clsCardB to be creatable by others.

    They have to go through clsCardMgr in order to access the Cards. Any idea?
    "If ignorance is bliss, that probably explain why I'm in a such a mess right now!!"

  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You can assign them Friend or private constructors to limit their creation to the CardManager. Using a base class/Interface may not be best if they are extremely different but if not then anything that is similiar will only have to be coded once the rest can be added on. A base class or interface will not make the code any longer than having it seperate, it could only get shorter, but that doesn't necessarily mean its the thing to do. Either way you are going to have to cast them to the strong type to get the intellisence you want. Usually a collection only has a single type. You could make functions to retrieve the different items if you want:
    VB Code:
    1. Public Function CardAItem(index as Integer) As CardA
    2.  
    3. Public Function CardBItem(index as Integer) As CardB
    4.  
    5. Public Function CardCItem(index as Integer) As CardC

  6. #6
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    I dont know what you are doing, but you may want to do something like this:

    declare a mustinherit class called Card, and make all the CardA, CardB, etc classes inherit from that class. Then your function can return a data of type Card

    here's what I did for my project: I had some different graphics objects... rectangles, bitmaps, textAreas, paths,....
    so I made a class called Shape, and every other class inherited from shape. Now you can declare a variable as shape and set it = to any of those other classes ( dim a as shape = new myRectangle() ). That's the easiest thing to do I guess... hope it's clear
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  7. #7
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    In C#, a function can return different data types .

    edit : sorry guys , I didn't know this thread was old enough and I didn't dig it out .

  8. #8
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    Originally posted by Pirate
    In C#, a function can return different data types .

    edit : sorry guys , I didn't know this thread was old enough and I didn't dig it out .
    It can return or the given data type or any derived type from that type, not more than that
    \m/\m/

  9. #9
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    It can return or the given data type or any derived type from that type, not more than that
    it can return any type regardless of its base class . Read about out keyword .

  10. #10
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    out keyword?
    From what i've understood the out keywords allows you to pass to a function a variable that still hasn't been instanciated so it's the function job to do it
    \m/\m/

  11. #11
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by PT Exorcist
    out keyword?
    From what i've understood the out keywords allows you to pass to a function a variable that still hasn't been instanciated so it's the function job to do it
    From one of MS books publishing .

    Output Parameters

    In Visual C#, you can also use output parameters. This feature is not available in Visual Basic .NET. An output parameter is a parameter that is passed from a called method to the method that called it—that is, the reverse direction. Output parameters are useful if you want a method to return more than a single value . An output parameter is specified by using the out keyword. Output parameters are always passed by reference and do not need to be initialized before use.

  12. #12
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    I think you misread it. It's saying exactly what I said: your function can "return" several parameters because you put all your vars as "out" that will be initialized and filled by your function, but it will never allow you to make your function RETURN multiple types of vars
    \m/\m/

  13. #13
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by PT Exorcist
    I think you misread it. It's saying exactly what I said: your function can "return" several parameters because you put all your vars as "out" that will be initialized and filled by your function, but it will never allow you to make your function RETURN multiple types of vars
    When was the last time you picked up a C# book . lol ?

    Here is what I'm talking about : It returns different types from inside the function to outside the function .

    PHP Code:
    private void MainFunction(out int i,out string s)
            {
                
    i=1000;
                
    s="blah";            
            }

            private 
    void CallIt()
            {
                
    int ii;
                
    string ss;

                
    MainFunction(out ii,out ss);
                
    MessageBox.Show("ii value is returned from the function \n" ii.ToString());
                
    MessageBox.Show("ss value is returned from the function \n" ss);
            }

            private 
    void button1_Click(object senderSystem.EventArgs e)
            {
                
    CallIt();
            } 

  14. #14
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    Originally posted by Pirate
    it can return any type regardless of its base class . Read about out keyword .
    Here you say that a function in c# regardless of the return type can return ANY variable type.

    Its completly different pass arguments as out values than to say your function CAN return any value regardless of the suposed type it should return. If you read carefully my posts you will see as I know perfectly what out parameters are. Out arguments are just args that pass types by ref that dont need to be instanciated before they are passed to the function so it's your function's job to instanciate them before using them.
    Last edited by PT Exorcist; Mar 7th, 2004 at 12:01 PM.
    \m/\m/

  15. #15
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by PT Exorcist
    Here you say that a function in c# regardless of the return type can return ANY variable type.

    Its completly different pass arguments as out values than to say your function CAN return any value regardless of the suposed type it should return. If you read carefully my posts you will see as I know perfectly what out parameters are. Out arguments are just args that pass types by ref that dont need to be instanciated before they are passed to the function so it's your function's job to instanciate them before using them.
    I don't quite understand what you want to say . You specify what data types your function should returns . . What's your bottom line anyway ?

  16. #16
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    There is a difference between a return and a variable that is passed out. A function can only return one type.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  17. #17
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by crptcblade
    There is a difference between a return and a variable that is passed out. A function can only return one type.
    Well , the way it works might be different than retuning function , but the result is the same . MS say that in each book they release and I do trust what MS say *usually* .

  18. #18
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    The point is : It is totally wrong to say that in c# you can return any variable type instead of the function's return type. You can return the function's return type or any derived type.
    If you want to pass variables as ref/out you can, but you are not returning its values, you are setting its values inside the function. OUT arguments are just REF arguments that don't need to be instanciated before they are passed into the functions.

    Code:
    private void funcOut(out MyClass) {
    ..blabla
    }
    
    private void funcRef(ref myClass) {
    ..blablabla
    }
    
    private void main() {
    MyClass c;
    funcRef(ref c); //THIS WILL NOT WORK BECAUSE THE VAR ISNT INSTANCIATED
    funcOut(out c); //THIS WILL WORK BECAUSE WITH OUT YOU DONT NEED TO INSTANCITE THE VAR BEFORE PASSING IT TO THE FUNCTION
    }
    \m/\m/

  19. #19
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by PT Exorcist
    The point is : It is totally wrong to say that in c# you can return any variable type instead of the function's return type. You can return the function's return type or any derived type.
    If you want to pass variables as ref/out you can, but you are not returning its values, you are setting its values inside the function. OUT arguments are just REF arguments that don't need to be instanciated before they are passed into the functions.

    Code:
    private void funcOut(out MyClass) {
    ..blabla
    }
    
    private void funcRef(ref myClass) {
    ..blablabla
    }
    
    private void main() {
    MyClass c;
    funcRef(ref c); //THIS WILL NOT WORK BECAUSE THE VAR ISNT INSTANCIATED
    funcOut(out c); //THIS WILL WORK BECAUSE WITH OUT YOU DONT NEED TO INSTANCITE THE VAR BEFORE PASSING IT TO THE FUNCTION
    }
    I understand what ref and out and I do know when to use them but I really don't understand why you are still ignoring what MS says . I think PT Exorcist is confusing ref with out .
    Don't tell me about ref and what's suppose to do . If you think ref is somehow out then ref is able to return more than datatype in VB.NET . This feature is in C# not in VB.NET .

    out keyword return different datatypes at the same time plus the main datatype the function can return .Don't be fooled by the tricke it forms in . Read about the main reason MS invented "out" for .

    Read here :
    http://msdn.microsoft.com/library/de...l/vclrfout.asp

    http://www.softsteel.co.uk/tutorials.../lesson13.html

  20. #20
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Pirate can you give us an example in C#? Convert this to C#, please.
    VB Code:
    1. Public Function GetItem() As TextBox
    2.     Return New TextBox()
    3. End Function
    4.  
    5. Public Function GetItem() As Button
    6.     Return New Button()
    7. End Function

  21. #21
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    Sorry Pirate, I did read both articles and they say just what I've said: Out is the same as Ref but you dont need to initialize vars before you pass them to the function.

    edit: Where a method parameter is defined (and invoked) using the 'out' modifier, it is passed by reference. The difference between the 'out' and the 'ref' modifier is this: a parameter modified by the 'out' keyword need not be assigned a value before being passed into the method, but must be assigned a value in the method.

    Exactly what I've been speaking about in all those posts

    If you arge about passing various vars and for the function to change them, VB.NET developers can still use the ByRef keyword, but don't tell me the function is RETURNING different values types than the function's return type. It is simply taking ref vars and setting new values to it, it MUST still return the right variable type.
    Last edited by PT Exorcist; Mar 7th, 2004 at 03:49 PM.
    \m/\m/

  22. #22
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by Edneeis
    Pirate can you give us an example in C#? Convert this to C#, please.
    VB Code:
    1. Public Function GetItem() As TextBox
    2.     Return New TextBox()
    3. End Function
    4.  
    5. Public Function GetItem() As Button
    6.     Return New Button()
    7. End Function
    This is the conversion but it won't compile because both has the same signature .

    PHP Code:
    public TextBox GetItem()
    {
    return new 
    TextBox(); 
    }


    public 
    Button GetItem()
    {
    return new 
    Button();


  23. #23
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Right, it wont in VB either, but that was the original issue.

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