?
Solved

c# alternative to '? extends class' (java)

Posted on 2011-10-21
8
Medium Priority
?
508 Views
Last Modified: 2012-05-12
Hi,

I'm converting some java code to c# and am wondering if there is a direct alternative for java's '? extends MyClass' i.e. i have a class type that inherits from an abstract class that implements an interface, how do i pass an instance of the child class into a method that takes any class that extends my abstract class?

Currently i have the sig in c# taking an interface but this isn't accepting my class even though its parent implements the interface
0
Comment
Question by:basil365
  • 5
  • 3
8 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37006269
Have you tried setting the signature of the method to accept objects of type "AbstractClass" (that is, your abstract class name)? As long as a child is within the inheritance chain of the abstract class, you should be able to pass it into the function without incident.
0
 

Author Comment

by:basil365
ID: 37006435
unfortunately that doesn't seem to work either
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37006624
Can you provide a crude C# example of what you are doing?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37006650
Here is an example of what I was referring to:

using System;

namespace Application
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            Employee e = new Employee() { FirstName = "John" };
            Manager m = new Manager() { FirstName = "Fred" };
            
            PrintName(e);
            PrintName(m);
            
            Console.ReadKey();
        }
        
        public static void PrintName(Person p)
        {
            Console.WriteLine(p.FirstName);
        }
    }
    
    abstract public class Person
    {
        public string FirstName { get; set; }
    }
    
    public class Employee : Person { }
    
    public class Manager : Person { }
}

Open in new window

0
 

Author Comment

by:basil365
ID: 37006831
what i have is:

interface:

public interface ILookup<T>
    {
        T KeyValue { get; set; }
    }

Open in new window


abstract:
public abstract class AbstractLookup<T> : ILookup<T>
    {
        private T keyValue;

        public T KeyValue
        {
            get { return keyValue; }
            set { keyValue = value; }
        }

    }

Open in new window


implementation:

 public class FOChart:AbstractLookup<int>
    {

        private DecimalKeyLookupMap fractionalOddMap;

        public DecimalKeyLookupMap FractionalOddMap
        {
            get { return fractionalOddMap; }
        }

        public  FOChart(int chartId, DecimalKeyLookupMap map)
        {
             KeyValue = chartId;
            this.fractionalOddMap = map;
        }

    }

Open in new window



(problem here is in another objects constructor)

 public class IntegerKeyLookupMap 
    {

        public IntegerKeyLookupMap(Dictionary<int,  FractionalOddChart> lookupMap)

        {

        }

        public ILookup<int> getEntry(int entryId)
        {
            //do stuff
        }

in the java version  the 'ILookup<int>' was replaced with '? extends'ILookup<int>'.

In the above version i get an error when i try to pass a dictionary of 'Dictionary<int, ILookup<int>> '

    }

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37006940
What version of the framework are you targeting?
0
 

Author Comment

by:basil365
ID: 37007180
4.0
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 37017746
You haven't displayed the definition of FractionalOddChart, but barring that, shouldn't the constructor for IntegerKeyLookupMap be defined as:

public IntegerKeyLookupMap(Dictionary<int, ILookup<int>> lookupMap)
{

}

Open in new window

0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month14 days, 17 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question