2

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.SelectedItemsand 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");
            }
        }
    }


}

}

Ian W
  • 385
  • 2
  • 10
  • 30

3 Answers3

0

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");
}
Paul Zahra
  • 9,522
  • 8
  • 54
  • 76
  • Hi there I had to use `string.Compare(p.OsmiOverride, Test, true) ` as describe [here](https://msdn.microsoft.com/en-us/library/zkcaxw5y(v=vs.110).aspx) because I got an error, you had an extra arguement. However, the version of `string.Compare` I used it did not return 0 rather -1, which I guess is false, could there be any "padding" in my strings? – Ian W Jan 14 '16 at 10:15
  • @IanW Just before you test for 'equality' perform a Test.length, what is it? – Paul Zahra Jan 14 '16 at 10:23
  • and @drColombo It now makes Sense! When I compare the lenghts the lengh of OsmiOverride is 100! - The Why is because actually the class OSMI is from a EF DbContext model of an EF data Model and the OsmiOverride is a column in a SQL database defined as `Char(100)` so I can understand why it would have a Length 100! - Brilliant Guys many thanks for the pointers – Ian W Jan 14 '16 at 11:09
0

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.

drcolombo
  • 194
  • 6
  • Yes it can be null, so that is a good to know as well, see comment above to the answer to my problem, but many thanks for your help – Ian W Jan 14 '16 at 11:04
0

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);

    }
Community
  • 1
  • 1
Ian W
  • 385
  • 2
  • 10
  • 30