Evaluating strings in C# Code snippets

When checking for two (or more) different values for a property in a code snippet, you can’t simply look at the string values without an error being thrown.  For example:

    Sage.Entity.Interfaces.IAccount acc this.BindingSource.Current as Sage.Entity.Interfaces.IAccount;
    if (acc.type != "Customer" || acc.type != "Prospect")

This will cause an error becuse the or operator (||) can’t be used with string values.  By putting parenthesis around both values, they will evaluate as boolean values and the comparison will work:

         if ((acc.type != "Customer") || (acc.type != "Prospect"))

However, you may still get an “Object reference not set to instance of an object” error if the acc.type value is blank or null.  In order to account for null values, you can use the string.IsNullOrEmpty function.

         if (!string.IsNullOrEmpty(acc.type) && ((acc.type != "Customer") || (acc.type != "Prospect")))

That should now evaluate correctly even if the Type property is blank.

 

Thanks for reading!

 

ABOUT THE AUTHOR

Jason Buss

Jason is a senior application developer with Customer FX.

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe To Our Newsletter

Join our mailing list to receive the latest Infor CRM (Saleslogix) news and product updates!

You have Successfully Subscribed!