Re: Accessing Shared Properties of a Parent Windows App from Class Lib



Hi Jon,

Based on my understanding, your winform application is creating some other
class library objects to implement business logic. All these class library
objects need the SQLConnection object to manipulate the database. So you
want to find a way to create the SQLConnection once and share it with all
the class library objects for efficiency. If I have misunderstood you,
please feel free to tell me, thanks.

Yes, this is a common task. The keypoint in this task is that how to get the
reference to the mainform object. There are several ways to get it done. For
example:
1. You may change the declaration of shared DBConnection object from
"Protected" to "Public". Then in each MyLibrary.MyObject constructor, you
may refer this shared DBConnection object with
"[mainForm_classname].DBConnection". This is because the "DBConnection" is a
shared object, there is no need to refer it with mainform object reference,
the mainform class name is enough to refer the public shared field/member.

2. In declaring the MyLibrary.MyObject constructor, you may add an extra
parameter of type DBConnection. Then whenever you are creating the
MyLibrary.MyObject in man form, you may pass the DBConnection object
reference in it. In this approach, there is no need to mark the
"DBConnection" as "shared".

3. If you are using VB2005, the VB2005 compiler provided a new featured
named "My Namespace". This feature allows us to refer a lot of useful
members staticly without the object references. So you may just use
"My.Forms[0]" to get the mainform reference in the MyLibrary.MyObject
constructor, then you may cast the reference into the mainform type(since
the reference you have got is of base type "Form"). Finally, you may use the
"mainform_reference.DBConnection" to get the DBConnection object now. In
this approach, there is no need to mark the "DBConnection" as "shared".
Please see the "The My Namespace" section in the article below for more
information regarding this new feature:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvs05/html/vb2005_overview.asp

Additionally, if you are curious about how VB2005 compiler implement this
cool "My Namespace" feature, I have provided some insight in the discussion
thread below, just for your reference:
http://groups.google.com/group/microsoft.public.vsnet.debugging/browse_thread/thread/d08b27dbd8ffe82e/cf909a3a4963b1e7?lnk=st&q=VB2005+MY+msdn+microsoft+%22Jeffrey+Tan%22&rnum=1&hl=zh-CN#cf909a3a4963b1e7

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support Engineer
within 1 business day is acceptable. Please note that each follow up
response may take approximately 2 business days as the support professional
working with you may need further investigation to reach the most efficient
resolution. The offering is not appropriate for situations that require
urgent, real-time or phone-based interactions or complex project analysis
and dump analysis issues. Issues of this nature are best handled working
with a dedicated Microsoft Support Engineer by contacting Microsoft Customer
Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.


.



Relevant Pages

  • Re: Accessing Shared Properties of a Parent Windows App from Class
    ... My Windows Forms app is its own project, ... I can't add a reference to the Windows ... shared object, there is no need to refer it with mainform object reference, ... Microsoft Online Community Support ...
    (microsoft.public.dotnet.framework)
  • 2 section mismatches for v2.6.28-rc3-54-g75fa677
    ... WARNING: vmlinux.o: Section mismatch in reference from the ... # Device Drivers ... # PCI IDE chipsets support ...
    (Linux-Kernel)
  • Re: 2.6.20-rc4-mm1 -- WARNING: "profile_hits" [drivers/kvm/kvm-intel.ko] undefin
    ... WARNING: vmlinux - Section mismatch: reference to ... # ACPI Support ...
    (Linux-Kernel)
  • Re: twin patadox question
    ... >>> it your own personal opinion. ... >> that it is not without support. ... >> reference does not mean that it is without support. ... >> will not look at evidence that your statement is naive and wrong does ...
    (sci.physics.relativity)
  • Re: suncomm, drm, intel
    ... If you have a message from Rob before today regarding the support of this software, you will need to check again, as Rob now has new ... Tiger "broke" the installation of the MediaMax software, and the devs are working to fix this. ... The installation does not make a reference to Tiger not being supported. ... The brochure on SunnComm's MediaMax specifically states "Mac Protection and Compatibility" stating that "For the first time, content can be played and protected on a Mac with the same experience enjoyed on a PC..." ...
    (comp.sys.mac.advocacy)