Comparing objects

  • Thread starter Thread starter Roshawn
  • Start date Start date
R

Roshawn

Hi,

I have a function that returns a string. This function can accept any type of object, which determines the structure of the
string returned. My question is, how do I know what type of object is passed in?

Thanks,
Roshawn
 
Roshawn.

Sooner or later you have to test it.

Therefore I prefer in Visual Basic

If TypeOf myObject Is String then.

Cor
 
Roshawn said:
I have a function that returns a string. This function can accept any
type of object, which determines the structure of the string returned. My
question is, how do I know what type of object is passed in?

\\\
Select Case True
Case TypeOf Param Is <Type 1>
...
Case TypeOf Param Is <Type 2>
...
...
End Select
///
 
Thanks to everyone for their help. I was able to do it another way (supplying another string to the function that names the
object) but nothing like the way you all displayed.

Happy coding, :-)
Roshawn
 
Roshawn said:
I have a function that returns a string. This function can accept any
type of object, which determines the structure of the string returned.
My question is, how do I know what type of object is passed in?

What is this function going to do?

If you intend to examine each object given and return a "representation"
of that object for, say, debugging purposes, then /don't/.

Instead, on each of your Classes, /override/ the ToString method and use
/this/ to define the representation of that Class, something like:

Class Person
Public ReadOnly Property Forenames() as String
...
Public ReadOnly Property Initials() as String
...
Public ReadOnly Property Surname() as String
...

Public Overrides Overloads Function ToString() As String
Return Me.Surname & ", " & Me.Initials
End Function

End Class

That way, when given any of your objects, simply using

? x.ToString()

will get you the representation that you've defined for that particular
Class. To get the Type of the object, you'd use

? x.GetType().ToString()

HTH,
Phill W.
 
Back
Top