Reputation: 11
Imports System.Windows.Forms
Module Module1
Sub Main()
Dim TextBox1 As New TextBox
Dim TextBox2 As New TextBox
If (GetType(TextBox1).Equals(GetType(TextBox2))) Then ' Error Here
Console.WriteLine("They are equal.")
End If
End Sub
End Module
I know, GetType for an object returns the Type of it. But here GetType(TextBox1) causes an error. I need to re-write this logic:
If (GetType(TextBox1).Equals(GetType(TextBox2))) Then ' Error Here
Console.WriteLine("They are equal.")
End If
I wrote like this:
If TextBox1.GetType().FullName.Equals(TextBox2.GetType().FullName) Then ' Works fine
Console.WriteLine("They are equal.")
End If
Can you re-write the logic ?
If (GetType(TextBox1).Equals(GetType(TextBox2))) Then ' Error Here
Console.WriteLine("They are equal.")
End If
Upvotes: 0
Views: 640
Reputation: 4322
If You want check are two object same then use :
If TypeName(TextBox1).Equals(TypeName(TextBox2)) Then Console.WriteLine("They are equal.")
Upvotes: 0
Reputation: 2548
Basically, GetType() operator expects a typename, not an object. To get a type of the object, use its GetType() method. IOW, in your code you can:
GetType(TextBox)
but can't:
GetType(TextBox1) ' Won't compile!
and have to:
TextBox1.GetType()
So, the error is expected, your solution is fine and your failed re-write attempt is an error, as expected.
For more detailed explanation, check:
https://msdn.microsoft.com/en-us/library/tay4kywk.aspx
Upvotes: 2
Reputation: 78200
The GetType
operator works on type names, not variable names.
If (GetType(TextBox).Equals(GetType(TextBox))) Then
Console.WriteLine("They are equal.")
End If
If you need to get type of a variable, then what you already have will work, although I don't understand why you needed to call FullName
:
If TextBox1.GetType() Is TextBox2.GetType() Then
Console.WriteLine("They are equal.")
End If
Upvotes: 1