JRockFL
asked on
IComparer Object reference not set to an instance of an object.
I'm not familiar with IComparer, but I'm getting a null reference
What am I doing wrong?
Private _comparer As IComparer
If _comparer.Compare(_lastVal ue, e.Item.DataItem) <> 0 Then
'add a header if it was different from the previous item.
Dim item As New GroupHeader()
_groupTemplate.Instantiate In(item)
item.DataItem = e.Item.DataItem
Me.Controls.Add(item)
item.DataBind()
End If
What am I doing wrong?
Private _comparer As IComparer
If _comparer.Compare(_lastVal
'add a header if it was different from the previous item.
Dim item As New GroupHeader()
_groupTemplate.Instantiate
item.DataItem = e.Item.DataItem
Me.Controls.Add(item)
item.DataBind()
End If
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you for the replies
_lastValue is object
e.Item.DataItem is System.Data.DataRowView
Should this class implement the IComparer interface in order to compare those two objects?
_lastValue is object
e.Item.DataItem is System.Data.DataRowView
Should this class implement the IComparer interface in order to compare those two objects?
You can write a custom Comparer to compare objects that have special rules. If you are just comparing the string values, then you can do this as string already implements Comparer.
e.Item(0).ToString().Compa reTo(_last Value)
e.Item(0).ToString().Compa
Private _comparer As IComparer
To fix this normally in a declaration, you would add the New keyword and provide any constructor arguments necessary like so:
Private _comparer As New IComparer 'Invalid for IComparer
However, if you put this in code, you will see this will not work as IComparer is an Interface and cannot be instantiated; therefore, when you go to use it as an object, it is indeed a null reference since you cannot instantiate an object of that type in the first place.
Hopefully that makes sense.
HTH
Kevin