O
Olaf Krumnow
Hi,
I have a requirement that, I thought, was very simple, but in fact
turned out as impossible for me.
I read some posts here and the language specs from MS, but couldn't
get a solution.
I coded the part in java, because it's very straightforward there:
public class Test {
void test(){
IOption<Color> iOpt = new Option<Color>(Color.BLACK);
IOption<Font> fOpt = new Option<Font>(Font.getFont("xyz"));
List<IOption<?>> list = new ArrayList<IOption<?>>();
list.add(iOpt);
list.add(fOpt);
for (IOption<?> option : list) {
System.out.println(option.result().toString());
}
}
}
It creates a list of options and adds some of different kind. Finally
it iterates the list and prints the string representation of all
results. The interface IOption and the class Option are very simple
for demonstration purposes:
public interface IOption<T> {
T result();
}
public class Option<T> implements IOption<T> {
private T _val;
public Option(T val) {
_val = val;
}
public T result() {
return _val;
}
}
I couldn't get this to work in C# because I didn't find that wildcard-
thingy of java. What am I missing? Or isn't it possible using
generics? Any idea?
Thanks in advance,
Olaf
I have a requirement that, I thought, was very simple, but in fact
turned out as impossible for me.
I read some posts here and the language specs from MS, but couldn't
get a solution.
I coded the part in java, because it's very straightforward there:
public class Test {
void test(){
IOption<Color> iOpt = new Option<Color>(Color.BLACK);
IOption<Font> fOpt = new Option<Font>(Font.getFont("xyz"));
List<IOption<?>> list = new ArrayList<IOption<?>>();
list.add(iOpt);
list.add(fOpt);
for (IOption<?> option : list) {
System.out.println(option.result().toString());
}
}
}
It creates a list of options and adds some of different kind. Finally
it iterates the list and prints the string representation of all
results. The interface IOption and the class Option are very simple
for demonstration purposes:
public interface IOption<T> {
T result();
}
public class Option<T> implements IOption<T> {
private T _val;
public Option(T val) {
_val = val;
}
public T result() {
return _val;
}
}
I couldn't get this to work in C# because I didn't find that wildcard-
thingy of java. What am I missing? Or isn't it possible using
generics? Any idea?
Thanks in advance,
Olaf