Results 1 to 3 of 3
- 12-08-2008, 10:57 PM #1
[SOLVED] Is it bad programming style to just typecast an object?
I have multiple sections of code where I take the selected object(using a selectionListener) from my tree and just work with a type casted version of that. for example
server = (Server)selObj;
works the exact same way as iterating through a loop doing selObj.getName().equalsIgnoreCase(servers[x]) where servers is an array created using ServerGroup.getChildren().
I'm assuming the typecast does the same as my loop, just behind the scenes, since I can comment out the compare loop and have everything work exactly as it should.
Is there some reason I shouldn't do this over the loop?
Are you getting your selObj via TreeSelectionListener's TreeSelectionEvent parameter, calling getSource() on it? If so, since it returns an Object, I don't see that you have much choice but to typecast here. But maybe I misunderstand your question.
- 12-09-2008, 06:02 PM #3
No I'm getting it through
I'm assuming that an element is another term for object? I guess ya it make's sense now that I think about it. To clear things up originally I was comparing the name of selObj to the name of each server object and if they were the same just setting server to server[x].
I guess it work's either way and after asking my boss his preference I was told to just typecast it. Makes it easier for him to review my code.
- By goodwillwins in forum AWT / SwingReplies: 24Last Post: 03-19-2011, 05:56 AM
- By Albert in forum Advanced JavaReplies: 2Last Post: 11-26-2010, 03:12 AM
- By Andrefs in forum Advanced JavaReplies: 1Last Post: 07-22-2008, 04:27 PM
- By jimm1 in forum Advanced JavaReplies: 0Last Post: 01-31-2008, 09:41 PM
- By jkhoa in forum AWT / SwingReplies: 2Last Post: 08-10-2007, 12:36 AM