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<ColoriO pt = new Option<Color>(C olor.BLACK);
IOption<FontfOp t = new Option<Font>(Fo nt.getFont("xyz "));
List<IOption<?> list = new ArrayList<IOpti on<?>>();
list.add(iOpt);
list.add(fOpt);
for (IOption<?optio n : list) {
System.out.prin tln(option.resu lt().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<Timpleme nts 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<ColoriO pt = new Option<Color>(C olor.BLACK);
IOption<FontfOp t = new Option<Font>(Fo nt.getFont("xyz "));
List<IOption<?> list = new ArrayList<IOpti on<?>>();
list.add(iOpt);
list.add(fOpt);
for (IOption<?optio n : list) {
System.out.prin tln(option.resu lt().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<Timpleme nts 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
Comment