Home All Groups Group Topic Archive Search About

Assigning a Value to a Structure Pointed to by a Tag ?

Author
24 Jan 2006 3:04 PM
eBob.com
I know that this must be a really dumb question but I just can't find an
answer.

I want to associate some information with a RichTextBox.  The Tag property
seems to be the intended way to "hang" some additional information on a
Control.  I've created a structure, rtbAuxInfo, for the information.  I
think that I have found the right syntax for assigning a pointer to the
structure to the Tag property of the RichTextBox, but I can't figure out how
to then assign a value to one of the fields of the structure.

' rtb1 is the RichTextBox

rtb1.Tag = New rtbAuxInfo     ' at least this syntax seems OK

' now I want to set the URL field of the rtbAuxInfo structure ...

rtb1.Tag.URL = url     ' doesn't work but I think that it shows what I am
trying to accomplish

CType(rtb1.Tag, rtbAuxInfo).URL = url    ' doesn't work either

I'd sure appreciate it if someone could help me with this.

Thanks,  Bob

Author
24 Jan 2006 4:19 PM
Jay B. Harlow [MVP - Outlook]
Bob,
a Structure is a value type, which means you get a copy each time you assign
it to an object "variable" (rtbl.Tag) and each time you retrieve it from an
object "variable". As it will be boxed (placed on the heap).

In this case when you access rtbl.Tag.Url you are setting a property on a
copy of your initial structure.

If  rtbAuxInfo "needs" to be a structure (which IMHO obviously doesn't, as
you are using it as a reference type). You need to make a copy, set the
copy, then save the copy. Something like:

| rtb1.Tag = New rtbAuxInfo     ' at least this syntax seems OK

    Dim temp As rtbAuxInfo = DirectCast(rtbl.Tag, rtbAuxInfo)
    temp.URL = url
    rtbl.Tag = temp

If you simply make rtbAuxInfo a Class, then you can simply refer to it in
the Tag property:

    DirectCast(rtb1.Tag, rtbAuxInfo).URL = url

As the Tag property is returning a reference to a single object on the heap,
no copies of values is being done.

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


Show quoteHide quote
"eBob.com" <faken***@totallybogus.com> wrote in message
news:uFesBePIGHA.3460@TK2MSFTNGP12.phx.gbl...
|I know that this must be a really dumb question but I just can't find an
| answer.
|
| I want to associate some information with a RichTextBox.  The Tag property
| seems to be the intended way to "hang" some additional information on a
| Control.  I've created a structure, rtbAuxInfo, for the information.  I
| think that I have found the right syntax for assigning a pointer to the
| structure to the Tag property of the RichTextBox, but I can't figure out
how
| to then assign a value to one of the fields of the structure.
|
| ' rtb1 is the RichTextBox
|
| rtb1.Tag = New rtbAuxInfo     ' at least this syntax seems OK
|
| ' now I want to set the URL field of the rtbAuxInfo structure ...
|
| rtb1.Tag.URL = url     ' doesn't work but I think that it shows what I am
| trying to accomplish
|
| CType(rtb1.Tag, rtbAuxInfo).URL = url    ' doesn't work either
|
| I'd sure appreciate it if someone could help me with this.
|
| Thanks,  Bob
|
|
|
Author
27 Jan 2006 12:36 PM
Phill W.
"eBob.com" <faken***@totallybogus.com> wrote in message
news:uFesBePIGHA.3460@TK2MSFTNGP12.phx.gbl...

> The Tag property seems to be the intended way to "hang" some additional
> information on a Control.  I've created a structure, rtbAuxInfo, for the
> information.  I think that I have found the right syntax for assigning a
> pointer to the structure to the Tag property of the RichTextBox, but I
> can't figure out how to then assign a value to one of the fields of the
> structure.

Start by washing your mouth out - "pointer" indeed.   ;-))

The Tag property is typed "as Object" so that you can put /anything/
into it.  To get that Object "back" to the right Type of variable, you
have to "cast" it before you can use it, as in

' No "New"; Structure is a Value Type, like Integer
rtb1.Tag = rtbAuxInfo

and to retreive it

Dim ai as rtbAuxInfo _
    = DirectCast( rtb1.Tag, rtbAuxInfo )

ai.URL = url

HTH,
    Phill  W.