1
\$\begingroup\$

I have page where I am inserting some data into Entry fields. For each Entry I have written in XAML different controls with parameters. One of them is Formated parameter where I am giving a string which I am checking on C# logic code. The problem which I figure out that some one could insert wrong string or string but for example with lower case. How can I improve this code for any wrong input scenarios ?

This is my xaml code:

<ContentPage.Content>

    <StackLayout Padding="7,7,7,7" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" Spacing="0">

      <cv:LabelEntryView x:Name="Name" LabelText="some text 4" ColorBg="White" MaxLength="20" Placeholder="some text 4" Formated="NameValidation" Keyboard="Text" >
      </cv:LabelEntryView>

      <cv:LabelEntryView x:Name="CardNr" LabelText="some text 3" ColorBg="White" MaxLength="20" Placeholder="some text 3" Formated="CardNrValidation" Keyboard="Numeric"  >
      </cv:LabelEntryView>

      <cv:LabelEntryView x:Name="ExpDate" LabelText="some text 2" ColorBg="White" MaxLength="5" Placeholder="some text 2" Formated="ExpDate" Keyboard="Numeric" >
      </cv:LabelEntryView>

      <cv:LabelEntryView x:Name="SecurNr" LabelText="some text" ColorBg="White" MaxLength="3" Placeholder="some text" Keyboard="Numeric" >
      </cv:LabelEntryView>

    </StackLayout>

  </ContentPage.Content>

and here is my C# code with if statement checking string:

private void Entry_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (Formated == "NameValidation")
            {
                Entry ent = (Entry)sender;
                ToUpper(ent);
            }
            else if (Formated == "CardNrValidation")
            {
                Entry ent = (Entry)sender;
                CardNumberValidation(ent);
            }
            else if (Formated == "ExpDate")
            {
                Entry ent = (Entry)sender;
                ExpDate(ent,e);
            }
}

Maybe I have to assign type for each if check? If yes, how can I do it? Thank you for your reviews.

\$\endgroup\$
1
  • 3
    \$\begingroup\$ As we all want to make our code more efficient or improve it in one way or another, try to write a title that summarizes what your code does, not what you want to get out of a review. Please see How to get the best value out of Code Review - Asking Questions for guidance on writing good question titles. \$\endgroup\$ – BCdotWEB Oct 12 '16 at 9:47
3
\$\begingroup\$

This is not how it should be done in WPF especially that you are not validating the text but just changing it upper case - you validate only the numeric input.

The right way would be to use the Binding Validation

This method would allow you to write specialized validators without ifs or switches.

\$\endgroup\$
0
\$\begingroup\$

I think you could you switch statement

 private void Entry_TextChanged(object sender, TextChangedEventArgs e)
 {
  Entry ent = (Entry)sender;
  switch((Formated )
  {
   case "NameValidation":
   ToUpper(ent);
   break;

   case "CardNrValidation":
   CardNumberValidation(ent);
   break;

   case "ExpDate":
   ExpDate(ent);
   break;
  }
 }
\$\endgroup\$
1
  • 2
    \$\begingroup\$ But all you did is changing if {} else if{} statement into switch. It's not changing anything really. \$\endgroup\$ – Paweł Hemperek Oct 12 '16 at 8:16
0
\$\begingroup\$

C# can switch string.

private void Entry_TextChanged(object sender, TextChangedEventArgs e)
{
    // If it is not Entry, it would be null
    var ent = sender as Entry;
    if(ent == null) return;
    switch (Formated)
    {
        case "NameValidation":
            ToUpper(ent);
            break;
        case "CardNrValidation":
            CardNumberValidation(ent);
            break;
        case "ExpDate":
            ExpDate(ent, e);
            break;
    }
}

However, it is a bit strange than you are not check the condition by parameter. There may be other part of your code can be changed.

\$\endgroup\$
0
\$\begingroup\$

If you are worried about wrong data being written in Formated property (eg. lower case) you could do some string normalization.

private void Entry_TextChanged(object sender, TextChangedEventArgs e)
{
    if(string.IsNullOrEmpty(Formated))
    {
        return;
    }

    string formatted = Formated.ToUpper();
    /* perform your checks */
}

Also if values stored in Formated property are known - there is just some set of acceptable values, you could use enum and map given string to this enum value. This would make your code less error prone, since in your current implementation you could mistype some acceptable value in some of your if clauses.

\$\endgroup\$
1
  • \$\begingroup\$ I also think, that enum would be good idea, now I declared enum FomationType { None, NameValidation, CardNrValidation, ExpDate }; like this, but how can I assign these values in XAML and in C#? \$\endgroup\$ – BinaryTie Oct 12 '16 at 8:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.