Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Having issues with Object comparison [modified]

Having issues with Object comparison [modified]

Scheduled Pinned Locked Moved C#
databasecomquestion
10 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    myNameIsRon
    wrote on last edited by
    #1

    Hi, I'm tying to use code from the MS web site (Implement a DataSet GROUP BY Helper Class ): http://support.microsoft.com/kb/326145 The code works well with the DataTable sample they show, but when I use a DataTable populated from an Access database, the object comparison does not work. It only works if I convert to string... any idea why? ******************************************************************************** if (!ColumnEqual( LastSourceRow[Field.FieldName, SourceRow[Field.FieldName] )) {// yada } private bool ColumnEqual(object 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 } ********************************************************************************* I did this to make it work: if (!ColumnEqual( LastSourceRow[Field.FieldName].ToString(), SourceRow[Field.FieldName].ToString() )) {//yada } private bool ColumnEqual(string a, string 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 == "") && (b == "")) return true; //both are null if ((a == "") || (b == "")) return false; //only one is null return (a == b); //value type standard comparison } thanks, Ron -- modified at 3:07 Wednesday 21st March, 2007

    J P 2 Replies Last reply
    0
    • M myNameIsRon

      Hi, I'm tying to use code from the MS web site (Implement a DataSet GROUP BY Helper Class ): http://support.microsoft.com/kb/326145 The code works well with the DataTable sample they show, but when I use a DataTable populated from an Access database, the object comparison does not work. It only works if I convert to string... any idea why? ******************************************************************************** if (!ColumnEqual( LastSourceRow[Field.FieldName, SourceRow[Field.FieldName] )) {// yada } private bool ColumnEqual(object 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 } ********************************************************************************* I did this to make it work: if (!ColumnEqual( LastSourceRow[Field.FieldName].ToString(), SourceRow[Field.FieldName].ToString() )) {//yada } private bool ColumnEqual(string a, string 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 == "") && (b == "")) return true; //both are null if ((a == "") || (b == "")) return false; //only one is null return (a == b); //value type standard comparison } thanks, Ron -- modified at 3:07 Wednesday 21st March, 2007

      J Offline
      J Offline
      joon vh
      wrote on last edited by
      #2

      it works here, with datatable and regular cells. DataTable dt = new DataTable("temp"); dt.Columns.Add("col1"); dt.Rows.Add("row1"); dt.Rows.Add("row2"); dt.Rows.Add("row3"); Console.WriteLine(ColumnEqual(dt.Rows[1][0], dt.Rows[1][0])); Console.Read(); --> This outputs True (can test it in an empty console app). You should check it with the debugger, using breakpoints, to see what values are actually compared.


      Visual Studio can't evaluate this, can you? public object moo { __get { return moo; } __set { moo = value; } }

      M 1 Reply Last reply
      0
      • M myNameIsRon

        Hi, I'm tying to use code from the MS web site (Implement a DataSet GROUP BY Helper Class ): http://support.microsoft.com/kb/326145 The code works well with the DataTable sample they show, but when I use a DataTable populated from an Access database, the object comparison does not work. It only works if I convert to string... any idea why? ******************************************************************************** if (!ColumnEqual( LastSourceRow[Field.FieldName, SourceRow[Field.FieldName] )) {// yada } private bool ColumnEqual(object 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 } ********************************************************************************* I did this to make it work: if (!ColumnEqual( LastSourceRow[Field.FieldName].ToString(), SourceRow[Field.FieldName].ToString() )) {//yada } private bool ColumnEqual(string a, string 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 == "") && (b == "")) return true; //both are null if ((a == "") || (b == "")) return false; //only one is null return (a == b); //value type standard comparison } thanks, Ron -- modified at 3:07 Wednesday 21st March, 2007

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        The comparison is not right. Having an empty string is not the same as having a null string. What you want to do is expand your checks for this:

        private bool ColumnEqual(string a, string b)
        {
        if (string.IsNullOrEmpty(a) && string.IsNullOrEmpty(b))
          return true;
        if (string.IsNullOrEmpty(a) || string.IsNullOrEmpty(b))
          return false;
        return (a == b);
        }
        

        Deja View - the feeling that you've seen this post before.

        M 1 Reply Last reply
        0
        • P Pete OHanlon

          The comparison is not right. Having an empty string is not the same as having a null string. What you want to do is expand your checks for this:

          private bool ColumnEqual(string a, string b)
          {
          if (string.IsNullOrEmpty(a) && string.IsNullOrEmpty(b))
            return true;
          if (string.IsNullOrEmpty(a) || string.IsNullOrEmpty(b))
            return false;
          return (a == b);
          }
          

          Deja View - the feeling that you've seen this post before.

          M Offline
          M Offline
          myNameIsRon
          wrote on last edited by
          #4

          Thanks Pete, Yes, I was also worried about null strings too, but I did a test to see what output I would get. When you convert a DBNull.Value to string, the result is String.Empty. I'll use IsNullOrEmpty, just to be safe though. Ron if (DBNull.Value.ToString() == null) { MessageBox.Show("null"); } else if (DBNull.Value.ToString() == "") { MessageBox.Show("empty string"); } else { MessageBox.Show("not null or empty"); }

          1 Reply Last reply
          0
          • J joon vh

            it works here, with datatable and regular cells. DataTable dt = new DataTable("temp"); dt.Columns.Add("col1"); dt.Rows.Add("row1"); dt.Rows.Add("row2"); dt.Rows.Add("row3"); Console.WriteLine(ColumnEqual(dt.Rows[1][0], dt.Rows[1][0])); Console.Read(); --> This outputs True (can test it in an empty console app). You should check it with the debugger, using breakpoints, to see what values are actually compared.


            Visual Studio can't evaluate this, can you? public object moo { __get { return moo; } __set { moo = value; } }

            M Offline
            M Offline
            myNameIsRon
            wrote on last edited by
            #5

            Hi joo, I used MessageBox to show me the comparison, row by row, and they do have the same string in the object (object.ToString()). They don't compair to true if left in the object (This only happends if the DataTable is populated by ADO). If building the DataTable by code (as in your example), this works fine. Ron

            J 1 Reply Last reply
            0
            • M myNameIsRon

              Hi joo, I used MessageBox to show me the comparison, row by row, and they do have the same string in the object (object.ToString()). They don't compair to true if left in the object (This only happends if the DataTable is populated by ADO). If building the DataTable by code (as in your example), this works fine. Ron

              J Offline
              J Offline
              joon vh
              wrote on last edited by
              #6

              It still works when I use ADO... Your problem appears to be very strange. SqlConnection conn = new SqlConnection("connstring"); SqlDataAdapter da = new SqlDataAdapter("select * from [table]",conn); DataSet ds = new DataSet(); da.Fill(ds); dataGridView1.DataSource = ds; Console.WriteLine(ds.Tables[0].Rows[1][1] == ds.Tables[0].Rows[1][2]); //returns false Console.WriteLine(ds.Tables[0].Rows[1][1] == ds.Tables[0].Rows[1][1]); //returns true


              Visual Studio can't evaluate this, can you? public object moo { __get { return moo; } __set { moo = value; } }

              M 1 Reply Last reply
              0
              • J joon vh

                It still works when I use ADO... Your problem appears to be very strange. SqlConnection conn = new SqlConnection("connstring"); SqlDataAdapter da = new SqlDataAdapter("select * from [table]",conn); DataSet ds = new DataSet(); da.Fill(ds); dataGridView1.DataSource = ds; Console.WriteLine(ds.Tables[0].Rows[1][1] == ds.Tables[0].Rows[1][2]); //returns false Console.WriteLine(ds.Tables[0].Rows[1][1] == ds.Tables[0].Rows[1][1]); //returns true


                Visual Studio can't evaluate this, can you? public object moo { __get { return moo; } __set { moo = value; } }

                M Offline
                M Offline
                myNameIsRon
                wrote on last edited by
                #7

                Yes, I spent hours trying to figure out why it was not grouping. Finally, I tried changing the object to a string right at the compairison (a.ToString() == b.ToString()) and magically the grouping worked. Damn, I guess I have to go to work (day job). Ron

                J 1 Reply Last reply
                0
                • M myNameIsRon

                  Yes, I spent hours trying to figure out why it was not grouping. Finally, I tried changing the object to a string right at the compairison (a.ToString() == b.ToString()) and magically the grouping worked. Damn, I guess I have to go to work (day job). Ron

                  J Offline
                  J Offline
                  joon vh
                  wrote on last edited by
                  #8

                  Yes, it is probably a good solution. But I still think it's strange that it didn't work before, because here in my sandboxapp it does. Weird. Have fun working ;)


                  Visual Studio can't evaluate this, can you? public object moo { __get { return moo; } __set { moo = value; } }

                  M 2 Replies Last reply
                  0
                  • J joon vh

                    Yes, it is probably a good solution. But I still think it's strange that it didn't work before, because here in my sandboxapp it does. Weird. Have fun working ;)


                    Visual Studio can't evaluate this, can you? public object moo { __get { return moo; } __set { moo = value; } }

                    M Offline
                    M Offline
                    myNameIsRon
                    wrote on last edited by
                    #9

                    Hi again joon, I've been playing around with the stock code again... I've noticed that if you try and Group By the other columns (OrderID",Int32) [I did change a few rows so some are the same value], it will not group. The stock code only seems to group on a string column? Could you check it our with your sandboxapp? thanks, Ron

                    1 Reply Last reply
                    0
                    • J joon vh

                      Yes, it is probably a good solution. But I still think it's strange that it didn't work before, because here in my sandboxapp it does. Weird. Have fun working ;)


                      Visual Studio can't evaluate this, can you? public object moo { __get { return moo; } __set { moo = value; } }

                      M Offline
                      M Offline
                      myNameIsRon
                      wrote on last edited by
                      #10

                      OK, I see now... I'm a fairly new programmer (with no formal training), so I'm not familiar with object comparison. The other methods (min, max) use IComparable, but they don't bother using it for the ColumnEqual method. I've changed: return (a==b); //value type standard comparison to: if (((IComparable)a).CompareTo(b) == 0) return true; else return false; works great now, on the different data types and my OleDbCommand string results. Ron

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups