简体   繁体   中英

Null Check before Assignment

Is there a way to check for null before the assignment operation without using the if statement? Is there a way to provide the following functionality in a single line similar to the null coalescing operator( ?? ) or extension method:

if(myVar != null){ obj = myVar;}

I tried to use an extension method but extension methods don't allow the use of the ref or out keywords as the first parameter. I can't seem to find any built in operator that does this.

The point is to avoid the assignment because the obj 's propertyset property is set to true no matter what it is being set to null or otherwise.

meaning obj = myVar ?? null; obj = myVar ?? null; will not work, nor will obj = myVar != null ? myVar : null; obj = myVar != null ? myVar : null;

I could possible use a static method but where should the method live? Utility class? A static method in an object extension class?

Are these the only options single line ifs or static methods?

EDIT: I don't have access to the obj it is one of many third party objects provided to me as is.

EDIT 2: I have a large amount of code that looks similar to the following:

this.Header.InvoiceHeader.InvoiceTypeCode = row.Field<string>("InvoiceTypeCode");
this.Header.InvoiceHeader.PurchaseOrderDate = row.Field<DateTime?>( "PODate" ) ?? DateTime.Now;
this.Header.InvoiceHeader.PurchaseOrderNumber = row.Field<string>( "PONumber" );
this.Header.InvoiceHeader.SellersCurrency = row.Field<string>( "Currency" ) ?? "USD";
this.Header.InvoiceHeader.BuyersCurrency = row.Field<string>( "Currency" ) ?? "USD";
this.Header.InvoiceHeader.TradingPartnerId = this.EDITradingPartner.TradingPartnerID;
this.Header.InvoiceHeader.CustomerAccountNumber = row.Field<string>("CustomerAccountNumber");
this.Header.InvoiceHeader.CustomerOrderNumber = row.Field<string>("CustomerOrderNumber");
this.Header.InvoiceHeader.PromotionDealNumber = row.Field<string>("PromotionDealNumber");

This is mapping my db to a third party xml object that has too many nodes to write a wrapper for each node. The xml object outputs an empty tag if the value of the property is set to null . in other words if I don't want the tag to exist then don't set it.

If you have a smart setter, that actually knows when it's value changed instead of just checking whether it was called , you could do this:

obj = myVar ?? obj;

If you don't have a smart setter, there is no way around an if . Where you want to place that is completely up to your and your coding preferences.

Since I'm assuming that obj is a variable and not a property, this is the equivalent, but personally I find the if much easier to read:

obj = myVar ?? obj;

Note that if obj is just a variable then there are no side-effects when setting it to itself, however if you try this with a property :

obj.MyProperty = myVar ?? obj.MyProperty;

Then you are invoking a getter and setter if myVar is null; either of which may have side effects that would not occur with the original if statement.

I recommend to use some method. If you will use this logic in different classes util class is what you need, otherwise use private method, or inherit from base class with protected implementation.

private void SetValue(ref object property, object value)
{
   if (property != null) {
      property = value;
   }
}

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