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

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Thank you Jeff. That is very good information and would be very relevant if
my class library was in the same project as my windows forms. Unfortunately,
my class library is a stand-alone class library and is its own VS 2005
Project. My Windows Forms app is its own project, and my Windows Service is
its own project. All three of these projects are contained in one VS 2005
Solution. Both projects have a reference to my class library project, since
they both use the same objects.

My class library doesn't have the 'My.Forms' namespace available, because it
doesn't have any Windows Forms in it. I can't add a reference to the Windows
Forms project from my Class Library, because that would cause a circular
reference.

So does that leave me with only option 2 from your message; individually
send a SQLConnection reference for each call to my DB Connection object?
Isn't there anyway that I can reference the calling assemblies main form
properties from an instantiated external child class? Something like...

(from my db connection object within my external class library)
Me.GetType.Assembly.GetCallingAssembly.xxx.xxx.MainForm.DBConnection

Where xxx.xxx would be some reference to the main form somehow?

Thanks for your valuable input.
--
Jon Ebersole
Microsoft Certified Solution Developer


"Jeffrey Tan[MSFT]" wrote:

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 Lib
    ... reference to the mainform object. ... You may change the declaration of shared DBConnection object from ... shared object, there is no need to refer it with mainform object reference, ... Microsoft Online Community Support ...
    (microsoft.public.dotnet.framework)
  • RE: Object library feature not supported....
    ... I had to uncheck the WindowsMediaPlayer Reference, save, then re-check the ... It appears that the object model loaded as a result of the software ... > Windows Media Player ...
    (microsoft.public.excel.programming)
  • Re: 16 Bit Windows Subsystem Error S32EVNT1.DLL
    ... Bit Windows Subsystem error, as the Symantec KB provides the same solution ... the reference to Symantec appears in the error ... "16 Bit MS-DOS Subsystem" Error Message When You Install a Program ...
    (microsoft.public.windowsxp.help_and_support)
  • Re: c:windowsinet20001winlogon.exe (unable to find)
    ... Open the Registry Editor... ... How to backup the Windows XP Registry ... Or get Startup Control Panel and use it to disable any reference to ...
    (microsoft.public.windowsxp.help_and_support)
  • Re: AutoCorrect/AutoExpand
    ... depends on the specific reference. ... > distributing from Windows XP to Windows 2000, do I need to create a ... > Windows\system32 directory and place a copy of the referenced files there, ... >> Doug Steele, Microsoft Access MVP ...
    (microsoft.public.access.modulesdaovba)