Simple if statement doesn't work

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andy B

    #1

    Simple if statement doesn't work

    I have a wizardStep with 2 textBoxes and a button on it. The textBoxes are
    called WordTextBox and DefinitionTextB ox. In the button_click event, I added
    this code:

    AddDefinition() ;

    The code for the AddDefinition method is below:

    private void AddDefinition() {

    ///Todo: Add validation for adding duplicate words, empty values

    StockContract.D ictionary = new ContractDiction ary<string, string>();

    if(StockContrac t.Dictionary.Co ntainsKey(WordT extBox.Text)) {

    DefinitionList. Caption = "This word already exists!";

    return;

    }


    StockContract.D ictionary.Add(W ordTextBox.Text , DefinitionTextB ox.Text);

    DefinitionList. DataSource = StockContract.D ictionary;

    DefinitionList. DataBind();

    WordTextBox.Tex t = String.Empty;

    DefinitionTextB ox.Text = String.Empty;

    }

    It creates a Dictionary collection and assigns it to the Dictionary of the
    StockContract object. It then takes the 2 textboxes and puts the values
    inside the dictionary collection. Everything works fine except the if
    statement. It is supposed to determine if the value of WordTextBox.Tex t is
    actualy a Key in the dictionary. If it is already in the collection, change
    the caption of the gridview to something different and exit the method
    without continuing. If it doesn't already exist, continue adding it to the
    collection. Any idea what might be wrong with it?






  • Jeff Dillon

    #2
    Re: Simple if statement doesn't work

    What is not working? I assume you've tested with hard coded values, and
    stepping through the code in the debugger, etc

    Jeff

    "Andy B" <a_borka@sbcglo bal.netwrote in message
    news:OCDhdYvpIH A.5096@TK2MSFTN GP02.phx.gbl...
    >I have a wizardStep with 2 textBoxes and a button on it. The textBoxes are
    >called WordTextBox and DefinitionTextB ox. In the button_click event, I
    >added this code:
    >
    AddDefinition() ;
    >
    The code for the AddDefinition method is below:
    >
    private void AddDefinition() {
    >
    ///Todo: Add validation for adding duplicate words, empty values
    >
    StockContract.D ictionary = new ContractDiction ary<string, string>();
    >
    if(StockContrac t.Dictionary.Co ntainsKey(WordT extBox.Text)) {
    >
    DefinitionList. Caption = "This word already exists!";
    >
    return;
    >
    }
    >
    >
    StockContract.D ictionary.Add(W ordTextBox.Text , DefinitionTextB ox.Text);
    >
    DefinitionList. DataSource = StockContract.D ictionary;
    >
    DefinitionList. DataBind();
    >
    WordTextBox.Tex t = String.Empty;
    >
    DefinitionTextB ox.Text = String.Empty;
    >
    }
    >
    It creates a Dictionary collection and assigns it to the Dictionary of the
    StockContract object. It then takes the 2 textboxes and puts the values
    inside the dictionary collection. Everything works fine except the if
    statement. It is supposed to determine if the value of WordTextBox.Tex t is
    actualy a Key in the dictionary. If it is already in the collection,
    change the caption of the gridview to something different and exit the
    method without continuing. If it doesn't already exist, continue adding it
    to the collection. Any idea what might be wrong with it?
    >
    >
    >
    >
    >
    >

    Comment

    • cfps.Christian

      #3
      Re: Simple if statement doesn't work

      You may not be showing us everything, but it doesn't look like you're
      looking for case sensitivity in here. I would insert all values
      as .ToUpper or .ToLower and check the textbox.text.to lower[toUpper]
      against the dictionary. You'll also want to .Trim the text.

      Comment

      Working...