473,396 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Comparing objects

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
Jan 12 '07 #1
5 884
>My question is, how do I know what type of object is passed in?
You can check it with object.GetType
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jan 13 '07 #2
Roshawn.

Sooner or later you have to test it.

Therefore I prefer in Visual Basic

If TypeOf myObject Is String then.

Cor
"Roshawn" <ra*********@yahoo.comschreef in bericht
news:OG**************@TK2MSFTNGP06.phx.gbl...
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

Jan 13 '07 #3
"Roshawn" <ra*********@yahoo.comschrieb:
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
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Jan 13 '07 #4
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
Jan 13 '07 #5
Roshawn wrote:
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.
Jan 15 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: beliavsky | last post by:
By mistake I coded something like print ("1" > 1) and got the result "True". Comparing an integer and a string seems meaningless to me, and I would prefer to have an exception thrown. Can...
5
by: Skip Montanaro | last post by:
I'd like to compare two xml.dom.minidom objects, but the naive attempt fails: >>> import xml.dom.minidom >>> d1 = xml.dom.minidom.parse("ES.xml") >>> d2 = xml.dom.minidom.parse("ES.xml") >>> d1...
2
by: Raphael Iloh | last post by:
Hi all, I'm having problems comparing array objects. Take a look at this: int array1 = new int{1}; int array2 = new int{1}; Console.Writeln(array1.Equals(array2)); One would expect the above...
5
by: George | last post by:
How do I compare the values of two objects when Option Strict is On? One of the objects is the Tag property from some control (declared as object but holding a value, e.g. an Integer or a String...
19
by: Dennis | last post by:
I have a public variable in a class of type color declared as follows: public mycolor as color = color.Empty I want to check to see if the user has specified a color like; if mycolor =...
3
by: Mark Denardo | last post by:
Hi I have an app that has a number of textboxes (richtextboxes to be exact) that get created dynamically and I add a few at a time to panel controls (so I can flip a group in and out when I want). ...
5
by: ma740988 | last post by:
There's a need for me to move around at specified offsets within memory. As as a result - long story short - unsigned char* is the type of choice. At issue: Consider the case ( test code ) where...
20
by: Bill Pursell | last post by:
This question involves code relying on mmap, and thus is not maximally portable. Undoubtedly, many will complain that my question is not topical... I have two pointers, the first of which is...
25
by: J Caesar | last post by:
In C you can compare two pointers, p<q, as long as they come from the same array or the same malloc()ated block. Otherwise you can't. What I'd like to do is write a function int comparable(void...
1
by: Kevin Blount | last post by:
I'm getting data from an XML file using this method: $name = $xml->user_info->user; $nickname = $xml->user_info->user; & $age = $xml->user_info->user; $shoe_size = $xml->user_info->user;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.