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. Web Development
  3. ASP.NET
  4. Proble with Custom property used to remove duplicates

Proble with Custom property used to remove duplicates

Scheduled Pinned Locked Moved ASP.NET
designhelp
2 Posts 2 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.
  • N Offline
    N Offline
    NetBot
    wrote on last edited by
    #1

    Hi all, I have a Gridview with with 4 columns....Brand,Category,Item,Stock The user wants that the for all items belonging to same Brand and category, the Brand and Category needs to be shown only once. For this i wrote the following code but it doesn`t seems to work...

    Public Class GridItem
    Dim _brand As String
    Dim _cat As String

    Public Sub GridItem()
        'Constructor
        \_brand = ""
        \_cat = ""
    End Sub
    
    Public Property Brand() As String
        Get
            Return \_brand
        End Get
        Set(ByVal value As String)
            \_brand = value
        End Set
    End Property
    
    Public Property Category() As String
        Get
            Return \_cat
        End Get
        Set(ByVal value As String)
            \_cat = value
        End Set
    End Property
    

    End Class

    Sub ItemGrid_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)

        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim GI As New GridItem
            GI.Brand = e.Row.Cells(0).Text
            GI.Category = e.Row.Cells(1).Text
            If uniquearr.Contains(GI) Then
                e.Row.Cells(0).Text = ""
                e.Row.Cells(1).Text = ""
            Else
                uniquearr.Add(GI)
            End If
        End If
    End Sub
    

    uniquearr is declared global.Any help would be appreciated

    G 1 Reply Last reply
    0
    • N NetBot

      Hi all, I have a Gridview with with 4 columns....Brand,Category,Item,Stock The user wants that the for all items belonging to same Brand and category, the Brand and Category needs to be shown only once. For this i wrote the following code but it doesn`t seems to work...

      Public Class GridItem
      Dim _brand As String
      Dim _cat As String

      Public Sub GridItem()
          'Constructor
          \_brand = ""
          \_cat = ""
      End Sub
      
      Public Property Brand() As String
          Get
              Return \_brand
          End Get
          Set(ByVal value As String)
              \_brand = value
          End Set
      End Property
      
      Public Property Category() As String
          Get
              Return \_cat
          End Get
          Set(ByVal value As String)
              \_cat = value
          End Set
      End Property
      

      End Class

      Sub ItemGrid_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)

          If e.Row.RowType = DataControlRowType.DataRow Then
              Dim GI As New GridItem
              GI.Brand = e.Row.Cells(0).Text
              GI.Category = e.Row.Cells(1).Text
              If uniquearr.Contains(GI) Then
                  e.Row.Cells(0).Text = ""
                  e.Row.Cells(1).Text = ""
              Else
                  uniquearr.Add(GI)
              End If
          End If
      End Sub
      

      uniquearr is declared global.Any help would be appreciated

      G Offline
      G Offline
      gnjunge
      wrote on last edited by
      #2

      I will answer in C#, if you don't understand tell me. Your problem is the following: (1) Contains uses Equals. (2)

      GridItem g1 = new GridItem("bla", "bla");
      GridItem g2 = new GridItem("bla", "bla");

      if (g1.Equals(g2))
      Response.Write("equal");
      else
      Response.Write("not equal");

      This returns not equal. Why? If the GridItem does not implement an Equals function, it will check the equality of the HashCode.

      Response.Write(g1.GetHashCode().ToString());
      Response.Write(g2.GetHashCode().ToString());

      Two different hashcodes. So how do we solve this? Add an Equals function to your GridItem:

      public override bool Equals(object obj)
      {
      if (obj == null && this == null)
      return true;
      else if ( (obj == null && this != null ) || (obj != null && this == null))
      return false;
      else if (this.Brand == ((GridItem)obj).Brand && this.Category == ((GridItem)obj).Category)
      return true;
      else
      return false;
      }

      This first checks if the object are null, and then checks if the contents of the objects are the same. Now the above equation of g1.Equals(g2), will return true.

      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