Need Help With A Try/Catch Block

Results 1 to 2 of 2

Thread: Need Help With A Try/Catch Block

  1. #1
    Join Date
    Dec 1969

    Default Need Help With A Try/Catch Block

    Hi all,<BR><BR>I&#039;m attempting to write some code to move a list item from 1 listitem control to another. I have created 2 list items and 2 buttons...1 button moves the list item from A to B, and the other button moves the list item from B to A.<BR><BR>Here is the code I am executing...<BR><BR>Try<BR> lstAssigned.Items.Add(lstAvailable.SelectedItem)<B R> lstAvailable.Items.RemoveAt(lstAvailable.SelectedI ndex)<BR> lstAssigned.ClearSelection()<BR>Catch x As Exception<BR> lblMessage.Text = "Select a privilege to add"<BR>End Try<BR><BR>I tried putting a breakpoint on the code to step through and it errors at the "lstAssigned.Items.Add(lstAvailable.SelectedIt em)" because lstAvailable.SelectedItem is nothing. This part operates exactly how I figured it would. What I don&#039;t understand is why I still get a null exception error page after the subroutine completes. This is the kicker...if I changed lstAvailable.SelectedItem to lstAvailable.SelectedItem.Text it runs exactly how I want it to by putting a message in a label, but it fails every time without .Text. If I select an item before clicking either button the code runs fine.<BR><BR>Any help is greatly appreciated<BR><BR>Thanks, Brian

  2. #2
    Join Date
    Dec 1969

    Default RE: Need Help With A Try/Catch Block

    First and foremost you should not be using Try / Catch blocks as flow control. These blocks should only be used for truly exceptional circumstances. Reason being that using them for flow control leads to more difficult to understand code, plus in comparison to regular code execution throwing / catching is expensive.<BR><BR>That being said the code would be:<BR><BR>if (lstAvailable.SelectedIndex != -1) {<BR>lstAssigned.Add(lstAvailablle.SeletedItem);<B R>lstAvailable.RemoveAt(lstAvailable.SelectedIndex );<BR>lstAssigned.ClearSelection();<BR>}<BR>else {<BR>lblMessage.Text = "Selet a priviledge to add";<BR>}<BR><BR>

Posting Permissions

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