简体   繁体   中英

Using Testing a object property of type String in if c# not working

I am still very new to c# wpf

This seems strange that I can not do this. Could someone explain this mistake to me please everything I read says this SHOULD work!

I have a class (called OSMI) which I show through a DataGrid in WPF, based on the Class property OsmiOverride = "Y" based on the Selected Items of the Grid I want to total up the Column of the grid based on the class property InvValue . I take the Dat\\grid.SelectedItems and put them in a List and send to my method and I loop through testing, but no matter how I do the String.Compare or String.Equals I can not get inside my If statement. Below is my current version of my method and class (excuse the crude MessageBox debug method)

 public static Nullable<decimal> GetOsmiSubtractions(List<OSMI> dgselecteditems)
    ///Get efffect of selected items subtracking to OSMI
    {
        Nullable<decimal> sum = 0;
        String Test = @"Y";
        foreach (OSMI p in dgselecteditems)
        {
            MessageBox.Show(p.InvValue.ToString());
            MessageBox.Show(p.OsmiOverride);
            if (p.OsmiOverride.Equals(Test))
            {
                sum += p.InvValue;
                MessageBox.Show("Inside");
            }

        }
        MessageBox.Show(sum.ToString());
        return decimal.Round((decimal)sum, 2);

    }

So far I have tried

if (p.OsmiOverride=="Y")

if (p.OsmiOverride==@"Y")

Then I read use the String.Equals method

 String Test = @"Y";
if (p.OsmiOverride.Equals(Test))

But nothing is working, can someone please explain

Many Thanks

Ian

For completeness my class below

(I also think I should do something with a LINQ query to reduce the need for the loop because I could use the like [here][1] but I am not sure of the filter / where statement)

Something like

Nullable<decimal> total = dgselecteditems.Sum(item => item.InvValue where item.OsmiOverride=="Y" );

For completeness my class is

using System;
using System.Collections.Generic;
using System.Windows;
using System.ComponentModel;
using System.Collections.ObjectModel;

public partial class OSMI:INotifyPropertyChanged
{
    private string osmioverride;
    private Nullable<decimal> osmi1;

    public Nullable<long> Id { get; set; }
    public string StockCode { get; set; }
    public string Description { get; set; }
    public string Warehouse { get; set; }
    public Nullable<decimal> UnitCost { get; set; }
    public Nullable<decimal> QtyOnHand { get; set; }
    public Nullable<decimal> InvValue { get; set; }
    public string OsmiOverride
    {
        get { return this.osmioverride; }
        set
        {
            if (this.osmioverride != value)
            {
                this.osmioverride = value;
                this.NotifyPropertyChanged("OsmiOverride");
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void NotifyPropertyChanged(string propOsmiOverride)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propOsmiOverride));
    }

    public string NRVOverride { get; set; }
    public Nullable<decimal> OSMI1
    {
        get { return this.osmi1; }
        set
        {
            if (this.osmi1 != value)
            {
                this.osmi1 = (decimal)value;
                this.NotifyPropertyChanged("Osmi1");
            }
        }
    }


}

}

Try string.Compare ... if the strings are the same the result is 0

if (string.Compare(p.OsmiOverride, Test, true, StringComparer.InvariantCultureIgnoreCase) == 0)
{
    sum += p.InvValue;
    MessageBox.Show("Inside");
}

Also you can use string.Equals:

if (string.Equals(Test, p.OsmiOverride, StringComparison.InvariantCultureIgnoreCase))
{
    sum += p.InvValue;
    MessageBox.Show("Inside");
}

Also, as for your implementation, the code like this

p.OsmiOverride.Equals(Test))

may lead to NullReferenceException because according to your OSMI class, OsmiOverride property can be null.

Just to complete the solution here is the code from above now working, with thanks to @PaulZahra and @dbColombo. I also added from this example here to get the First Character of my string using the answer Here

To be clear my Mistake was not the WAY I was comparing my String but the fact I did not understand what was really in my String due to the mapping to SQL Database

The new method is:

public static Nullable<decimal> GetOsmiSubtractions(List<OSMI> dgselecteditems)
    ///Get efffect of selected items subtracking to OSMI
    {
        Nullable<decimal> sum = 0;
        String Test = "Y";


        foreach (OSMI p in dgselecteditems)
        {
            String OverRide = new string(p.OsmiOverride.Take(1).ToArray());

            if ((string.Compare(OverRide, Test, true) == 0))
            {
                sum += p.InvValue;
            }

        }
        return decimal.Round((decimal)sum, 2);

    }

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