Results 1 to 3 of 3
- 12-08-2008, 09: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, 05:02 PM #3
No I'm getting it through
getSource() would give me something like TreeViewer@somecrapJava Code:((IStructuredSelection) event.getSelection()).getFirstElement()
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.
Similar Threads
-
Executing a jar double-click style in Linux
By goodwillwins in forum AWT / SwingReplies: 24Last Post: 03-19-2011, 04:56 AM -
Operator < cannot be applied to java.lang.Object, Object
By Albert in forum Advanced JavaReplies: 2Last Post: 11-26-2010, 02:12 AM -
Parsing a superclass object to subclass object dynamicly
By Andrefs in forum Advanced JavaReplies: 1Last Post: 07-22-2008, 04:27 PM -
NetBeans-style Dockable-windows and toolbar?
By jimm1 in forum Advanced JavaReplies: 0Last Post: 01-31-2008, 08:41 PM -
Preventing inserted text from becoming colored from previous style
By jkhoa in forum AWT / SwingReplies: 2Last Post: 08-10-2007, 12:36 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks