Home > Back-end >  How to use return statement inside a class in c#
How to use return statement inside a class in c#

Time:01-14

I am new to c#. I have I code block for different textboxes. I decided to create class for this code to use in different windows forms.

here is my windows form code

using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace POS.screens
{
    public partial class sell : MetroFramework.Forms.MetroForm
    {
        ConnectionClass cons = new ConnectionClass();
        FillComboBox fcb = new FillComboBox();
        {
            InitializeComponent();
        }



        public void PriceTextBox_TextChanged(object sender, EventArgs e)
        {

            if (String.IsNullOrEmpty(QuantityTextBox.Text) || String.IsNullOrEmpty(PriceTextBox.Text))
            {
                TotalTextBox.Text = 0.ToString();
                
            }

            else
            {
                fcb.CheckNumbersOnly(PriceTextBox);

                int Quantity = Convert.ToInt32(QuantityTextBox.Text);
                decimal Price = Convert.ToDecimal(PriceTextBox.Text);
                decimal Total = Price * Quantity;
                TotalTextBox.Text = Total.ToString();
            }
        }
    }
}

And Here is Class Code

using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace POS
{
    class FillComboBox
    {
        ConnectionClass cons = new ConnectionClass();

        public void CheckNumbersOnly(dynamic TextBoxName)
        {
            if (!Regex.IsMatch(TextBoxName.Text, @"[0-9] (\.[0-9][0-9]?)?") && (TextBoxName.Text != ""))
            {
                MessageBox.Show(TextBoxName.Text);
                return;
            }
            if (!decimal.TryParse(TextBoxName.Text, out decimal PriceValue))
            {
                MessageBox.Show("Please Enter Correct Number");
                TextBoxName.Clear();
                return;
            }
        }
    }
}

Everything is working fine But return statement is not working. If user inserts a letter or incorrect number. Program should show message and stop further working but It does not stop. It shows message and run next statement and give error.

When I use this code directly without creating class It works fine. I think, my way to use return statement in class is wrong. I am new learner. Thanks in advance.

CodePudding user response:

There are a number of ways to do this, a simple one would be for your CheckNumbersOnly method to return a boolean. Probably better to rename it too (for readability), and then in the PriceTextBox_TextChanged method, you'd check that the return is true and continue only if it is.

Putting that together:

Validation code:

public bool NumbersAreValid(string value)
{
   if (!Regex.IsMatch(value, @"[0-9] (\.[0-9][0-9]?)?") && (value != ""))
   {
      //Note: this isn't a very good error message, and the above Match 
      // check seems to redo the work of `TryParse` bellow
      MessageBox.Show(value);
      //Let caller know this failed validation
      return false;
   }
   if (!decimal.TryParse(value, out decimal PriceValue))
   {
      MessageBox.Show("Please Enter Correct Number");
      return false;
   }
   return true;
}

Form code:

public void PriceTextBox_TextChanged(object sender, EventArgs e)
{
   if (String.IsNullOrEmpty(QuantityTextBox.Text) || String.IsNullOrEmpty(PriceTextBox.Text))
   {
      TotalTextBox.Text = 0.ToString();
   } 
   else 
   {
      //When NumbersAreValid returns false, this if statement ends processing
      if (!fcb.NumbersAreValid(PriceTextBox.Text)) return;

      int Quantity = Convert.ToInt32(QuantityTextBox.Text);
      decimal Price = Convert.ToDecimal(PriceTextBox.Text);
      decimal Total = Price * Quantity;
      TotalTextBox.Text = Total.ToString();
   }
}

There are some other considerations, your PriceTextBox_TextChanged method duplicates the efforts of the validation method (by re-parsing the number).. so the validation method could instead return a decimal or throw an exception - the content of the exception would be the message to display in a MessageBox. You'd then need to wrap the validation attempt in a try {} catch{} block. You also seem to use QuantityTextBox without any validation - but perhaps that's just for this example.

  •  Tags:  
  • Related