Results 1 to 3 of 3
  1. #1
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default [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?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    No I'm getting it through
    Java Code:
    ((IStructuredSelection) event.getSelection()).getFirstElement()
    getSource() would give me something like TreeViewer@somecrap

    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

  1. Executing a jar double-click style in Linux
    By goodwillwins in forum AWT / Swing
    Replies: 24
    Last Post: 03-19-2011, 05:56 AM
  2. Replies: 2
    Last Post: 11-26-2010, 03:12 AM
  3. Replies: 1
    Last Post: 07-22-2008, 05:27 PM
  4. NetBeans-style Dockable-windows and toolbar?
    By jimm1 in forum Advanced Java
    Replies: 0
    Last Post: 01-31-2008, 09:41 PM
  5. Replies: 2
    Last Post: 08-10-2007, 01:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •