Return a class in a get-method

How do I return a userdefined classtype (other than Object) in a get-method It is easy in java, but in V J#
public class WhatEver {
private MyClass myClass;
public WhatEver() {
myClass = new MyClass();
}
public MyClass getMyClass() {
return myClass;
}
}


Answer this question

Return a class in a get-method

  • Tom Johnson

    The code is...

    /**

    * Get Card at i'th possition in deck.

    */

    public Card getCard(int i) {

    return deck.get(i);

    }

    I use the following method. It works fine in DrJava with java 1.5 (5.0 whatever), but I get the this error:

    Error 1 The type 'Object' cannot be returned as a 'Poker.Card'

    Error 2 Method 'Poker.AbstractPlayer.getCard' must return a value

    I have declared deck as a LinkedList.

    In java 1.5 I use the constructor LinkedList deck = new LinkedList<Card>() which obviously does

    not work in VJ#. Might the error occur because deck doesn't have a type If so, how do I declare a List of type Card

    Thanks.


  • GalenC

    You might misunderstand Ramakrishna's reply. For example, new System.Collections.Generic.LinkedList().get_First().get_Value() always returns T object.

    Be notice the following.  

    • System.Collections.Generic.LinkedList is neither List<T> nor IList<T> in CLR.
    • The function of Java’s iterator is shouldered by the node of LinkedList object. 
    • get(int) function is missing in LinkedList type in CLR.

    You need to rewrite your code or implement generic version of java.util.LinkedList in C# because Visual J# doesn’t support to create generic classes yet.


  • Mark Opti

    System.Collections.Generic.List<T> is the equivalent class of java.util.ArrayList<E> in JDK1.5.


  • Tim erin

    Thanks that did the trick. But there is clearly something wrong when you have to cast the returntype of a method which explicitly is defined with returntype of Card! There is of course no guarantee that an element in deck is of type Card in the first place, which might explane the need of casting.

  • BretUpdegraff

    Is there an ArrayList in J#
  • FannwongCindy

    The above code works perfectly fine in VJ# 2005. Did you see any problem with above code If so please provide the exact error message and full code.

    Thanks,



  • Catherine - PRA

    Yes, it is there in J# 2.0.

    Refer java.util.ArrayList --> jdk 1.1 equivalent

    Refer System.Collections.ArrayList --> for .Net.

    If you are using any of the Visual Studio 8.0 SKUs then you can find list of classes that are availble for project from Object Browser. To view Object Browser, click on View --> "Object Browser" then select "All Componets" scope for Browse drop down.



  • CSharpShooter

    I guess, return type of deck.get(i) is Object and not Card type, hence the error.

    try

    public Card getCard(int i) {

        return (Card) deck.get(i);

    }

     

    Please System.Collections.Generic.LinkedList<T> package for generic LinkedList class for .NET



  • Return a class in a get-method