How to share embedded resource?

L

Larry Serflaten

I am making a DLL that has a class that will be made into
several similar objects for the user. I have a .gif file that
has all the parts those objects need. I am wondering what
the proper way to share that image across all the objects is
so I don't end up with that single .gif loaded multiple times.

I am thinking I should make a Friend class that has a shared
property that returns the image, but I want to be sure it is
returned ByRef, and not ByVal, or otherwise copied for
every call. The .gif file itself is an embeded resource and
I am thinking if I create it through the GetManifestResourceStream
method each time it is needed, then each time I'll be making
a copy of the same image.

What is the 'pattern' for this situation, and does anyone know
of a link to a VB.NET example?

Thanks!
LFS
 
A

AlexS

Hi, Larry

You can use shared member to keep reference to gif. If it is loaded as Image
or PictureBox, or another object, you will pass only reference. By default
all objects are passed ByRef - you don't need to worry about this. Just load
gif once, for example, during first call - see singleton pattern, it's what
you need.

HTH
Alex
 
R

Rob Teixeira [MVP]

A singleton pattern can be enforced by using a shared property to return the
GIF image.
Property Get statements are really compiled under the covers as a method,
and all method return values are ByRef anyway, so you don't have to worry
about that. But for reference types, even ByVal doesn't copy objects, only
the object reference (pointer). You may end up with several object refrences
in that case, but they all point at the same blob of data.

-Rob Teixeira [MVP]
 
L

Larry Serflaten

Larry Serflaten said:
I am making a DLL that has a class that will be made into
several similar objects for the user. ....

Thanks guys, I'll use the Singleton pattern, I just wanted to
get validation on the proper method so I don't have to waste
time testing for excessive memory use. With GC its all so
unfamiliar, I didn't know if I could tell the difference between
multiple 'streams' or just the GC (garbage collector) falling
behind!

<g>
LFS
 

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

Top