String Comparison - Need Help quickly please!!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DragonLord
    New Member
    • Mar 2007
    • 122

    String Comparison - Need Help quickly please!!

    Here is the situation I am inserting rows into a datagridview and then using a function to group similar rows together based on a column.

    When I call to compare the lastrow with the current row the application passes the information that should be equal. i.e 479 ST-Vieurtur
    as the "Destinatio n" with the call below:


    if (!ColumnEqual(L astSourceRow[Field.FieldName], SourceRow[Field.FieldName]))

    When the comparison function is called, a and b look exactly alike but are said to be false.

    the pattern is that I am using a loop to add the rows to the datagrid based on a quantity selected. i.e. street name | Address | # of packages

    if they say 3 packages I insert three rows of quantity 1 with the streetname and address.

    Then they do the same thing repeatedly until they want to merge the orders.

    the pattern I can see is that when it hits the second group of addresses even if it is the same it evaluates false.

    Below is the comparison function can anyone shed some light please!!

    the second group alwasy

    private bool ColumnEqual(obj ect a, object b)
    {
    /*
    * Compares two values to see if they are equal. Also compares DBNULL.Value.
    *
    * Note: If your DataTable contains object fields, you must extend this
    * function to handle them in a meaningful way if you intend to group on them.
    */
    if ((a is DBNull) && (b is DBNull))
    return true; //both are null
    if ((a is DBNull) || (b is DBNull))
    return false; //only one is null
    return (a == b); //value type standard comparison
    }
  • DragonLord
    New Member
    • Mar 2007
    • 122

    #2
    sorry guys need to bump this can anyone help?

    Comment

    • DragonLord
      New Member
      • Mar 2007
      • 122

      #3
      never mind i figured it out...


      string.Equals(a ,b) or object.Equals(a ,b) is what needs to be done.

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Yeah objects a and b will never be equal unless you declared somewhere a=b; since they are checking the objects themselves not some kind of underlying value for equality.

        string sa="fred";
        string sb="fred";
        sa and sb are equal.

        But for:
        object a=(object)sa;
        object b=(object)sb;
        a != b

        Comment

        Working...