1

I have my code like this

private void btnStartAnalysis_Click(object sender, EventArgs e)

            {

        //Checks for the selectedItem in the cmbOpearions dropdown and make call to appropriate functions.
        if((string) (cmbOperations.SelectedItem) == "PrimaryKeyTables")
            {
            //This is the function call for the primary key checking in DB
            GetPrimaryKeyTable();
            }

        //Checks for the selectedItem in the cmbOpearions dropdown and make call to appropriate functions.
        if((string) (cmbOperations.SelectedItem) == "NonPrimaryKeyTables")
            {
            //This is the function call for the nonPrimary key checking in DB
            GetNonPrimaryKeyTables();
            }

        //Checks for the selectedItem in the cmbOpearions dropdown and make call to appropriate functions.
        if((string) (cmbOperations.SelectedItem) == "ForeignKeyTables")
            {
            //This is the function call for the nonPrimary key checking in DB
            GetForeignKeyTables();
            }

        //Checks for the selectedItem in the cmbOpearions dropdown and make call to appropriate functions.
        if((string) (cmbOperations.SelectedItem) == "NonForeignKeyTables")
            {
            //This is the function call for the nonPrimary key checking in DB
            GetNonForeignKeyTables();
            }

        if((string) (cmbOperations.SelectedItem) == "UPPERCASEDTables")
            {
            //This is the function call for the nonPrimary key checking in DB
            GetTablesWithUpperCaseName();
            }

        if((string) (cmbOperations.SelectedItem) == "lowercasedtables")
            {
            //This is the function call for the nonPrimary key checking in DB
            GetTablesWithLowerCaseName();
            }
        }

But here using (string) makes problem in case sensitiveness.So I want to use string.comapare in place of (string).

Can anybody give me any hint how to use it.

4 Answers 4

5

I suggest you use:

// There's no point in casting it in every if statement
string selectedItem = (string) cmbOperations.SelectedItem;

if (selectedItem.Equals("NonPrimaryKeyTables",
                        StringComparison.CurrentCultureIgnoreCase))
{
    ...
}

Choosing the right string comparison can be tricky. See this MSDN article for a lot more information.

I wouldn't suggest using Compare as other people have suggested, simply because it's not the right emphasis - Compare is designed to be used for testing which order strings should appear in when they're sorted. That has the by-product of allowing you to test for equality, but it's not the main aim. Using Equals shows that all you care about is equality - if the two strings aren't equal, you don't care which would come first. Using Compare would work, but it doesn't leave your code expressing itself as clearly as it can.

Sign up to request clarification or add additional context in comments.

Comments

1

try this:

if (string.Compare((string) cmbOperations.SelectedItem, 
        "NonForeignKeyTables", true) == 0) // 0 result means same
    GetNonForeignKeyTables();

Comments

0

For case insensitive comparison:

    string a = "text1";
    string b = "TeXt1";

    if(string.Compare( a, b, true ) == 0) {
        Console.WriteLine("Equal");
    }

Comments

0

MSDN have really good explanation about the string.compare method.. you just have to write

String.Compare (String, String) 

to compare your strings.. used with a Switch-case instruction it will be ok..

Use

String.Compare (String, String, boolean)

To set the case comparaison

1 Comment

See my answer for why I wouldn't use Compare. Also it's not clear how switch/case is relevant here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.