简体   繁体   中英

How can I call this method for retrieved data in C#?

I'm working on a program that allows you to select a customer ID from a dropdown box. Once the customer ID is selected, the customer's information is pulled from a CSV file and displayed in textboxes.

The phone number information is unformatted, but I want it to be displayed formatted (ex. (800)674-3452). I have written a method for this, but I'm not sure how to call it. Can you please help?

-Sorry if this is a dumb question. I'm still learning.

    private void idBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        try // catch errors
        {
            string selectedCustomer; // variable to hold chosen customer ID
            selectedCustomer = idBox.Text; // retrieve the customer number selected

            chosenIndex = 0;
            bool found = false; // variable if customer ID was found
            while (!found && chosenIndex < allData.Length) // loop through the 2D array
            {
                if (allData[chosenIndex, 0] == selectedCustomer) // make sure it's the right customer
                {
                    found = true; // Yes (true) found the correct customer
                }

                chosenIndex++; // add one row 
            }
            chosenIndex -= 1; // subtract one because add 1 before exiting while

            /* 0 = customer ID
             * 1 = name
             * 2 = address
             * 3 = city
             * 4 = state
             * 5 = zip
             * 6 = phone
             * 7 = email
             * 8 = charge account - yes/no
             * 9 = good standing - yes/no
             */
            nameBox.Text = allData[chosenIndex, 1]; // put name in nameBox
            addressBox.Text = allData[chosenIndex, 2]; // put address in addressBox
            cityBox.Text = allData[chosenIndex, 3]; // put city in cityBox
            stateBox.Text = allData[chosenIndex, 4]; //puts state in stateBox
            zipBox.Text = allData[chosenIndex, 5]; // puts zip in zipBox
            phoneBox.Text = allData[chosenIndex, 6]; // puts phone number in phoneBox
            emailBox.Text = allData[chosenIndex, 7]; // puts email in emailBox
            if (allData[chosenIndex, 8] == "Yes") // check if charge account
            {
                yesChargeRadio.Checked = true; // true if Yes
            }
            else // otherwise
            {
                noChargeRadio.Checked = true; // true if No
            }
            if (allData[chosenIndex, 9] == "Yes") // check for good standing
            {
                yesStandingRadio.Checked = true; // true if Yes
            }
            else // otherwise
            {
                noStandingRadio.Checked = true; // true if No
            }
        }
        catch (Exception errorInfo) // catch error
        {
            MessageBox.Show("errors: " + errorInfo, "Error",
                MessageBoxButtons.OK, MessageBoxIcon.Error); // error message
        }

    }

Here is the method(s) to check the length and format:

    private bool numberCheck(string str)
    {
        const int NUMBER_LENGTH = 10;
        bool valid = true;

        if (str.Length == NUMBER_LENGTH)
        {
            foreach (char ch in str)
            {
                if (!char.IsDigit(ch))
                {
                    valid = false;
                }
            }
        }
        else
        {
            valid = false;
        }
        return valid;
    }
    private void formatPhone(ref string str)
    {
        str = str.Insert(0, "(");
        str = str.Insert(4, ")");
        str = str.Insert(8, "-");
    }

You are almost done with your code. What you need to do is, before you set your phoneBox.Text you can call the method as below:

if(numberCheck(allData[chosenIndex, 6]))
{

    formatPhone(ref allData[chosenIndex, 6]);
}

phoneBox.Text = allData[chosenIndex, 6]; 

As you have your method with ref parameter, the formatted text will be updated in your arary and you can then assign it to your phoneBox

I hope I understand what part specifically you're asking about: you call methods you define just like the static methods IsDigit or MessageBox.Show, except you do not need to prefix the method name with a name and then a period because the method is part of the object calling it.

So, for example if I had a method:

public void ShowSomething()
{
     MessageBox.Show("stuff");
}

From within the class, I could call it like this:

ShowSomething();

To pass parameters, I would list them in the parenthesis, as you do with MessageBox.Show, for example.

You can use the value a method like numberCheck returns as any other boolean, so you could do any of these:

bool b = numberCheck(someString);
if (numberCheck(someString))
{
    //Do something, like displaying the phone number
}

This MSDN document might help you: http://msdn.microsoft.com/en-us/library/ms173114.aspx

Is this what you're looking for? :

.......
phoneBox.Text = numberCheck(allData[chosenIndex, 6]) ? 
                formatPhone(allData[chosenIndex, 6]) : 
                allData[chosenIndex, 6];
.......
private string formatPhone(string str)
{
    str = str.Insert(0, "(");
    str = str.Insert(4, ")");
    str = str.Insert(8, "-");
    return str;
}

Codes above will check validity of phone data, if it is valid set phoneBox.Text to formatted phone number, else set phoneBox.Text to raw unformatted phone data.

For reference in case you're not familiar with ternary operator ( ? ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM