CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Threaded View

  1. #1
    Join Date
    Apr 2006
    Posts
    82

    Java programmer has question about Generics

    Hi all

    I was a Java programmer, I am used to doing something as followed:

    IO Example: (It's not the best example I can come up with, but it is the simplest one)

    Code:
    public interface IO<T>{
        T parse(stream s);
        String print(T o);
    }
    Code:
    public class IOA extends IO<A>{
        A parse(stream s){
            A instanceA;
            // parse A
            return instanceA;
        }
    
        String print(A a){
             return a.toString();
        } 
    }
    Code:
    public class IOB extends IO<B>{
        B parse(stream s){
            A instanceA;
            //parse B
            return instanceB;
        }
    
    
        String print(B b){
             return b.toString();
        } 
    }
    so far, everything can translate to C#
    BUT
    I cannot find a way to translate the following to C#

    Code:
    public class Peeker{
    
          public Parser<?> peekAndFindSuitableParser(stream s){
    
                 // peeking.... then determine
                 if (/* need to return parserA */...){
                       return new ParserA();
                 }
                 else if (/* need to return parserB */...){
                       return new ParserB();
                 }
    
          }
    }
    Is it possible to translate this to C#?
    If not, should I just remove all the generics in these code?
    Last edited by xusword; April 2nd, 2009 at 11:00 AM.

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

Featured