How to check both the objects are same in Vb.net - asp.net

I am trying to compare to two objects of same type and determine they identical. I have
objA IS objB
also
objA.Equals(objB)
but every time I am getting false. Here is the code I am trying
Public Class RowsDetails
Property RelatedEmployee As String = String.Empty
Property RelatedNumberAs String = String.Empty
Property Type As String = String.Empty
Property ReportType As String = String.Empty
Property status As String = String.Empty
Property Term As String = String.Empty
Property Currency As String = String.Empty
End Class
All the properties have same values in objA and objB. I have gone over some of the articles that explained about implementing IEquatable(of T).
But they mostly looked like custom logic.
I was wondering is there a simple way to do this ?
Thanks In advance.

Any time you compare two Objects a method for determining equivalence must be established.
there are 3 main forms of equivalence:
Type equivalence: done with the TypeOf operator. who's behavior can be found here
Reference equivalence: done with the is operator. found here
Value Equality (which seems to be what your trying to do) requires that a comparement method is defined. Other wise it will take its best guess at doing so, by using the default Equals() method.
If none of these work for you then you HAVE to define your own method of comparing.
the only other way of doing so, other than implementing IEquatable() or overloading Equals(), would be taking a hash of both objects and comparing those.

Object.Equals(Object) is what is called when you call objA.Equals(objB). What is compared is, are they pointing to the same object.
Dim objA as New RowDetails()
Dim objB = objA
Now they are both pointing to the same object. So...
objA.Equals(objB)
returns True
But using the New keyword on both will create 2 different references so they will not be equal with the default implementation of .Equals
Add something like the following to you RowDetails class.
Public Overrides Function Equals(obj As Object) As Boolean
Dim rowDet As RowsDetails = TryCast(obj, RowsDetails)
If rowDet Is Nothing Then
Return False
'The AndAlso will short circuit as soon as it finds a false
ElseIf RelatedEmployee = rowDet.RelatedEmployee AndAlso RelatedNumber = rowDet.RelatedNumber AndAlso Type = rowDet.Type Then 'and the rest of the properties
Return True
End If
Return False
End Function

Related

How to obtain values from list

Hi Guys
Can anyone help me out with this problem i have a list of object that is formatted in the image attached above and i have to get the IDNO, Affected Id and the date values
Since deserialization will return anonymous type(in your case), you have two options: Either deserialize it to a strong type (by defining a type) or else fetch the value using Reflection since types and properties are not known.
In your case you can get the value through reflection like this:-
Dim _data As List(Of Object) = ...
Dim firstObject = _data.FirstOrDefault()
Dim type As Type = firstObject.GetType()
Dim idmoValue = type.GetProperty("IDMO").GetValue(firstObject)
Sample Fiddle.

Understanding how Public Properties are set in VB.NET

I am trying to understand how a value in a Public property is set. There does not seem to be anything passed or any kind of lookup.
Public Shared Property IsUser() As String
Get
If HttpContext.Current.Session("IsUser") IsNot Nothing Then
Return HttpContext.Current.Session("IsUser").ToString()
Else
Return Nothing
End If
End Get
Set(value As String)
HttpContext.Current.Session("IsUser") = value
End Set
End Property
I have tried searching for the ClassName i.e. UserSession.IsUser = and also the only place the session value is set is in the property. HttpContext.Current.Session("IsUser") = value
What do I need to understand better.. I mean at the end of the day the User has to be True or False so where is the Lookup or passed parameter?

add dataitem(from string array) to listview

To preface this, I've looked through several of the postings containing listviews and nothing really is comparing to what I'm trying to do.
I'm trying to take values determined through a loop that has several If Statements similar to what follows:
If Convert.ToInt32(GetData(ds.Tables("HRIS").Rows(i), "ABS_CO_RULE_DAYS", DefaultValue)) > 0 Or _
Convert.ToInt32(GetData(ds.Tables("HRIS").Rows(i), "ABS_CO_RULE_HRS", DefaultValue)) > 0 Or _
Convert.ToInt32(GetData(ds.Tables("HRIS").Rows(i), "ABS_CO_RULE_MINS", DefaultValue)) > 0 Then
msChargeDays = GetData(ds.Tables("HRIS").Rows(i), "ABS_CO_RULE_DAYS", DefaultValue)
msChargeHours = GetData(ds.Tables("HRIS").Rows(i), "ABS_CO_RULE_HRS", DefaultValue)
msChargeMins = GetData(ds.Tables("HRIS").Rows(i), "ABS_CO_RULE_MINS", DefaultValue)
msHowPaid = "CR DAYS"
AbsenceLine() 'calls sub
End If
This block of code returns valid results from the dataset that it calls from. Now in the following code block, I am trying to assimilate values that are determined by the main code block which is about 40 if statements similar in structure to the block above, all contained within a For loop.
In the following code block I am trying to insert an object of ListViewItem type into the ListView. This is where I'm getting my error denoted by comment string following it. The function that fills the ListViewItem is at the bottom, all of the variables returned are all class variables and return valid values to the ListViewItem. I have double checked this via the debugger.
Private Sub AbsenceLine()
Dim sTypeReason As String
If msType = "" Then
sTypeReason = Left(msReason, 25)
Else
sTypeReason = msType & "--" & msReason
End If
Dim item As ListViewItem
item = New ListViewItem(ListViewItemType.DataItem)
item.DataItem = FillListView(sTypeReason)
absence_lv.Items.Add(item) 'this line here is what is givine me issues
End Sub
Private Function FillListView(typeReason As String) As IEnumerable(Of String)
Return {msDate, msVoid, msCont, msDays, msHours, msMinutes, typeReason, msChargeDays, msChargeHours,
msChargeMins, msHowPaid} 'all values returned are of String type
End Function
Now with the background:
Am I completely off base with what I'm trying to do?
Is there an easier way to do this with a gridview instead?
I think you're misusing the DataItem property of the ListViewItem class. That's really used to get the underlying object that the ListViewItem represents when the ListView itself is bound.
Since your FillListView function is returning an IEnumerable(Of String), you can just set the ListView's DataSource property to the function call.
absence_lv.Datasource = FillListView(sTypeReason)
absence_lv.Databind()
As you can see, there is no need to manually add each item.

Simple bind value to textbox in code behind using Telerik OpenAccess

I cannot find a complete example. Found tons on grid and combobox, but not textbox. This test is to lookup a “PhoneTypeName” from a UserPhoneType table with TypeCode = “0” and assign that first value to a asp.net textbox.
Currently, I am getting “Object reference not set to an instance of an object” when setting the text box to "phonetype.FirstOrDefault.PhoneTypeName.ToString"
Using dbContext As New EntitiesModel()
Dim phonetype As IEnumerable(Of User_PhoneType) = dbContext.User_PhoneTypes.Where(Function(c) c.PhoneTypeCode = "O")
mytextbox.Text = phonetype.FirstOrDefault.PhoneTypeName.ToString
End Using
----EDIT----
I changed as suggested. I ALSO successfully bound the entire list of PhoneTypes to a droplist control...to confirm the data is accessible. It must be the way I am going about querying the table for a single record here.
I get the same error, but at "Dim type = phonetype.First..."
The record is in the table, but it does not appear to be extracted with my code.
Dim phonetype As IEnumerable(Of User_PhoneType) = dbContext1.User_PhoneTypes.Where(Function(c) c.PhoneTypeCode = "M")
Dim type = phonetype.FirstOrDefault
If Object.ReferenceEquals(type, Nothing) = False And Object.ReferenceEquals(type.PhoneTypeName, Nothing) = False Then
mytextbox.Text = type.PhoneTypeName.ToString
End If
In general there are the following two possible reasons for getting this exception:
1) The phonetype list is empty and the FirstOrDefault method is returning a Nothing value.
2) The PhoneTypeName property of the first element of the phonetype list has a Nothing value.
In order to make sure that you will not get the Object reference not set to an instance of an object exception I suggest you add a check for Nothing before setting the TextBox value. It could be similar to the one below:
Dim type = phonetype.FirstOrDefault
If Object.ReferenceEquals(type, Nothing) = False And Object.ReferenceEquals(type.PhoneTypeName, Nothing) = False Then
mytextbox.Text = type.PhoneTypeName.ToString
End If
Fixed it.
I was able to view the SQL string being generated by using this:
mytextbox.text = phonetype.tostring
I saw that the SQL contained "NULL= 'O'"
I did it like the example?!? However, when I added .ToString to the field being queried, it worked.
So the final looks like this:
Using dbContext As New EntitiesModel()
Dim phonetype As IEnumerable(Of User_PhoneType) = dbContext.User_PhoneTypes.Where(Function(c) c.PhoneTypeCode.**ToString** = "O")
mytextbox.Text = phonetype.FirstOrDefault.PhoneTypeName.ToString
End Using
BTW, Dimitar point to check for null first is good advice (+1). The value was nothing as he said.

updating properties of a class with dynamic references

Sorry for what is probably a very basic question. I have a vb.net class with properties defined as follows:
Private m_Property1 As String
Public Property Property1() As String
Get
Return m_Property1
End Get
Set(ByVal value As String)
If IsNothing(value) Then
m_Property1 = String.Empty
Else
m_Property1 = value
End If
End Set
End Property
I can then set the values as follows:
classname.Property1 = "myvalue"
How do I set the value of a property that is defined dynmically eg
Dim strPropertyName As String = "Property1"
Hope that makes sense.
Thanks,
Josh
You would use reflection
Dim strPropertyName as string = "Property1"
Dim pi As PropertyInfo = myClass.GetType().GetProperty(strPropertyName)
pi.SetValue(myClass.GetType(), "some string", Nothing)
You want to use Reflection in order to do this. VB.NET provides a way to do this if you know the value at compile-time, but for run-time operations, you need to use the GetType keyword in order to get the type of your class (or, use the GetType method on an instance of it if you don't know it).
Then, with that Type instance, you would call GetProperty, passing the string with the name of the property. It will return an PropertyInfo instance which you then call GetValue on, passing the instance of the object in, which will return an Object which you have to cast back to a type you wish to use (if you are).
VB.NET makes a lot of this easier with the CallByName function.
Also, if you know at compile-time what the name of the property is, you can always cast to object and use VB.NET's inherent late binding:
Dim o As Object = <your object>
o.Property1 = ...
VB.NET will perform the late-binding for you.

Resources