is this correct?

  • Thread starter Thread starter Brian Henry
  • Start date Start date
B

Brian Henry

Tell me if this is the correct reasoning for this situation.

I have two objects, both are structures and have identical data in the
structure. I place one into a queue object, now if i do a contains on the
queue of the second object it wont contain the first object because they are
independent memory locations (seperate objects) even though they are
identical and not pointers to each other. What I have is a queue of file
names that I need to process. Each file name has a structure that has a path
and its full file name. Now when They get placed into a queue i need to make
sure they file does not already exist in queue, but since they are
structures, each entry is an independent object, so if i have a file in
queue and then the same file name comes up later its a newly created object.
Even though they are the same file in reality, now they have two objects
representing themselves.
 
Brian Henry said:
Tell me if this is the correct reasoning for this situation.

Rather than what appears to be speculation, why not look in
the Help file and learn what the framework does when comparing
structures?

First, you find from a Queue's Contains method that it expects an
Object as a parameter. This should tell you right away that there
will be added overhead in boxing the structure to make it an Object.

Next you find that Contains uses the .Equals method to determine
if there is a match. And, knowing that structures are Value types,
you can look up the ValueType.Equals method in Help to see
what it does:

--------------- (From its Remarks section)
Remarks
The default implementation of the Equals method uses reflection to compare the
corresponding fields of obj and this instance. Override the Equals method for
a particular type to improve the performance of the method and more closely
represent the concept of equality for the type.
----------------

Also look there for their example of overriding the Equals method (as mentioned
above).

But I have to ask, why not just test it with some code? Then if it doesn't
act as you suspect, find out why, or post a question on that. You stand a
better chance at understanding, and retaining the knowlege gained if you
do the needed research yourself, before asking others to do the research for you....

Finally, (in case you missed it), due to your need for using a structure like an
object, you would be better off to just use a class that has the properties you
need.

HTH
LFS
 
I did test it first, that was why I was asking if I was correct because the
test failed.. two structures trying to match with contains did not return a
match, which was what I expected because they are two independent objects
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top