what does this mean please?

  • Thread starter Thread starter garyusenet
  • Start date Start date
G

garyusenet

TaggedString val =
(TaggedString)backgroundComboBox.SelectedItem;
Bitmap bmp = (Bitmap)val.Tag;


Now Taggedstring is a class, and I can gather that

TaggedString val = ....

is defining a variable of type reference to store an instance of
TaggedString.

What i don't understand is what the bracket before a variable means...
like
(TaggedString)background.ComboBox.SelectedItem;

can someone explain...

thanks,
Gary.
 
That means that the value returned from SelectedItem is casted to a
TaggedString object. Pretty much the same thing as the VB.NET ctype()
function.

Clint
 
Oh i see, so it's acting a little bit like .tostring() - but in this
case it's .towhateverclassyouspecify() ?

thanks!

Gary.
 
HI,


Oh i see, so it's acting a little bit like .tostring() - but in this
case it's .towhateverclassyouspecify() ?

NOT AT ALL ! you are not converting you are casting.

It's like saying that even as SelectedItem return an object reference YOU
KNOW that it's an instance of TaggedString.
At this point the runtime check if this is true, if it does it makes the
conversion, if not an exception is throw.

for example you cannot do this:
string val = (string)backgroundComboBox.SelectedItem

even as you could do
string val = backgroundComboBox.SelectedItem.ToString()


Pd:
The TaggedString class should be declared in the source code of the example
you are running
 
Back
Top