Reputation: 1863
DISCLAIMER: I'm not sure whether this question is answerable as it currently stands. If it is very difficult to answer or actually impossible, I'll repost a compilable example, but I currently don't have time.
Can someone tell me why this compiles:
public void addOutputConnLayer(OutputConnLayer mainOutputConnLayer) {
for (Map.Entry<String, AbstractPanelController<? extends AbstractPanelModel,
? extends AbstractPanelView<? extends AbstractPanelModel>>> entry : subControllers.entrySet()) {
if (entry.getValue() instanceof OutputConnLayerUser) {
OutputConnLayerUser tmp = (OutputConnLayerUser)entry.getValue();
tmp.addOutputConnLayer(mainOutputConnLayer);
}
}
}
Whereas I get an "OutputConnLayerUser cannot be resolved to a variable" error when I don't use a local variable? For example:
public void addOutputConnLayer(OutputConnLayer mainOutputConnLayer) {
for (Map.Entry<String, AbstractPanelController<? extends AbstractPanelModel,
? extends AbstractPanelView<? extends AbstractPanelModel>>> entry : subControllers.entrySet()) {
if (entry.getValue() instanceof OutputConnLayerUser) {
(OutputConnLayerUser)(entry.getValue()).addOutputConnLayer(mainOutputConnLayer); //<--- this line produces the error
}
}
}
OutputConnLayerUser is an interface with a single method. It looks as follows:
public interface OutputConnLayerUser {
public void addOutputConnLayer(OutputConnLayer ocl);
}
Upvotes: 0
Views: 661
Reputation: 333
You are not casting properly. Missing another parenthesis here
public void addOutputConnLayer(OutputConnLayer mainOutputConnLayer) {
for (Map.Entry<String, AbstractPanelController<? extends AbstractPanelModel,
? extends AbstractPanelView<? extends AbstractPanelModel>>> entry : subControllers.entrySet()) {
if (entry.getValue() instanceof OutputConnLayerUser) {
**(**(OutputConnLayerUser)(entry.getValue())**)**.addOutputConnLayer(mainOutputConnLayer); //<--- this line produces the error
}
}
}
Upvotes: 1
Reputation: 9954
You have to do a proper cast:
public void addOutputConnLayer(OutputConnLayer mainOutputConnLayer) {
for (Map.Entry<String, AbstractPanelController<? extends AbstractPanelModel,
? extends AbstractPanelView<? extends AbstractPanelModel>>> entry : subControllers.entrySet()) {
if (entry.getValue() instanceof OutputConnLayerUser) {
((OutputConnLayerUser)(entry.getValue())).addOutputConnLayer(mainOutputConnLayer); //<--- this line produces the error
}
}
}
Please note the additional brackets in the line with the error. You have to cast first and then call the method.
Upvotes: 0
Reputation: 13960
((OutputConnLayerUser) entry.getValue()).addOutputConnLayer(mainOutputConnLayer);
Upvotes: 2