Question Exam Question: Converting Types using Custom Classes

aar0n

Member
Joined
Jan 17, 2008
Messages
19
Location
40 Miles S. of Nashville, TN
Programming Experience
5-10
Hi,

I'm studying for the MCTS exam 70-536 and have come across the following question:

Given the following Visual Basic code sample, which interface must the custom class MyClass implement?

Dim a As MyClass, b As Boolean
a = 42
' Convert using ToBoolean.
b = Convert.ToBoolean(a)
Console.WriteLine("a = {0}, b = {1}", a.ToString, b.ToString)

Before even looking at the possible answers, I can't get past WHY on earth would we be converting the number 42 to a Boolean value?? :confused: I tried this and as I suspected it did NOT compile! Is this a trick question or am I missing something? Thanks!!!!

** edit **
I realized that in order for the code to work, I would need to implement one of the interfaces shown in the multiple choices. STILL, would there EVER be a purpose for this particular conversion?
 
Last edited:
You will find answer to both questions by reading this help page: Convert Class (System)
Btw, "MyClass" is reserved, a class can't have this name :)
The "a = 42" assignment/conversion is not part of this question, this can be done with a CType operator.
 
You will find answer to both questions by reading this help page: Convert Class (System)
Btw, "MyClass" is reserved, a class can't have this name :)
The "a = 42" assignment/conversion is not part of this question, this can be done with a CType operator.

Ok, thanks.

As far as using MyClass...I just copied and pasted the question as presented on the practice exam.

I guess my question is really down to this: What would be an example of a situation where a number, such as 42, would need to be converted to a boolean, such as in the example provided?
 
The question is not about converting a number to a boolean, it is about converting a custom class instance to a base value type, in this case incidentally and specifically a Boolean, which is what one of the methods in the IConvertible interface does. If one is to implement this interface there has to exist a logical connection between the class type and the value type. This connection can be anything, the question is purely academical. When you need to convert one base type to another, or find that some class you are designing have a logical value type representation, you know where to look to. The cue here is "Convert.ToBoolean", which is why I directed you to the help topic for Convert class, it explains what the Convert class does and how it works in conjunction with custom classes by implementing IConvertible.

That there exist conversions from most base types to another should indicate that someone has the need to convert for example an Integer to a Boolean, even if the reasoning may elude us, but as said that has nothing to do with the exam question.
 
That there exist conversions from most base types to another should indicate that someone has the need to convert for example an Integer to a Boolean, even if the reasoning may elude us, but as said that has nothing to do with the exam question.


Thanks, JohnH. I guess I was just having trouble getting past what seemed to me to be an unrealistic example.

I think you are telling me to accept it and move on to the actual subject matter of the question. I'll give that a shot. :)
 
Back
Top