How to code access to DLL method from 2 Winforms?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: How to code access to DLL method from 2 Winforms?

  1. #1
    Join Date
    Oct 2006
    Posts
    82

    How to code access to DLL method from 2 Winforms?

    I am trying to structure my code in a better way than a pile of classes. I was going to use a DLL, but I am now stuck.

    The code below represents parts of the Windows Forms application and part of a DLL.
    I need to make methods of the AxerDataMgrClass available to the code in frmEnabledApps class and MainForm class.
    How does one do that?
    Code:
    using System;
    using System.Windows.Forms;
    using nsCommonDLL;
    namespace AccessTestForm1
    {
    	public partial class Form1 : Form
    	{
    		CommonDLLClass DEF = new CommonDLLClass();
    		public Form1()
    		{
    			InitializeComponent();
    		}
    		private void Form1_Load(object sender, EventArgs e)
    		{
    
    		}
    	}
    }
    Code:
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using CD = nsCommonDLL.CommonDLLClass;
    
    namespace AccessTestForm1
    {
    	public partial class CommonDLLTestForm2 : Form
    	{
    		public CommonDLLTestForm2()
    		{
    			InitializeComponent();
    		}
    
    		private void CommonDLLTestForm2_Load(object sender, EventArgs e)
    		{
            		I need to access TMC.UpdateAppsLists() here.
    		}
    	}
    }
    Code:
    using System;
    namespace nsCommonDLL
    {
    	public class CommonDLLClass
    	{
    		public string ABC = "aBC";
    		public string XYZ()
    		{
    			return "xyz";
    		}
    		private string QQQ = "QQQ";
    	}
    }
    Last edited by Lou Arnold; April 7th, 2011 at 01:26 PM.

  2. #2
    Join Date
    Mar 2011
    Location
    London
    Posts
    54

    Re: How to code access to DLL method from 2 Winforms?

    Hello. It's not too clear to me what you are asking. Normally, to use a DLL, you add a reference to your DLL and then put the 'using' command in your project as you have done in your examples.

    In your question you talk about AxerDataMgrClass, frmEnabledApps and MainForm but none of your code examples reference these and so I'm a bit confused.

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,480

    Re: How to code access to DLL method from 2 Winforms?

    Just to clarify, where a class gets defined is pretty much irrelevant. You can define a class in your main exe assembly or a dll assembly. As long as you reference the (dll) assembly and include the namespace, your code won't care where the actual class has been defined. In other words, you would have the same problem if you defined the class inside your exe project.

    That being said, it sounds like you have more of a situation with passing an instance of the (AxerDataMgrClass) class between forms.

    One way to solve this problem is create an instance of the class (i.e. new AxerDataMgrClass) and then pass the instance to in the constructor of the second form.

    Code:
    public partial class CommonDLLTestForm2 : Form
    {
      private AxerDataMgrClass _axer;
    
      public CommonDLLTestForm2( AxerDataMgrClass axer )
      {
        InitializeComponent();
        _axer = axer;
      }
    
      private void CommonDLLTestForm2_Load(object sender, EventArgs e)
      {
        _axer.UpdateAppsLists();
      }
    }
    Invoking the second form from the first, you might have
    Code:
    public partial class Form1 : Form
    {
      private AxerDataMgrClass _axer = new AxerDataMgrClass();
      
      public Form1()
      {
        InitializeComponent();
      }
    
      private void OnButton2_Click(object sender, EventArgs e)
      {
        // Open the 2nd form as a do modal form and pass in
        // the instance of the axer class.
        var form2 = new CommonDLLTestForm2( _axer );
        form2.DoModal( ); 
      }
    }
    Another approach is to define the AxerDataMgr class as a singleton. (P.S. dropping the Class part of the name because it's redundant).
    Code:
    public class AxerDataMgr
    {
    
      public string Abc { get; set; }
      public string Xyz { get { return _xyz; } }
    
      private string _xyz = "xyz";
    
    #region Singleton
      private static AxerDataMgr _instance;
      private static Object _lock = new Object( );
    
      private AxerDataMgr( ) {}
    
      public AxerDataMgr Instance
      {
        get
        {
          lock( _lock )
          {
            if( _instance == null )
            {
              _instance = new AxerDataMgr( );
            }
          }
          return _instance;
        }
      }
    #endregion Singleton
    }
    Now, change the example above to use the singleton:

    Code:
    public partial class Form1 : Form
    {
      
      public Form1()
      {
        InitializeComponent();
    
    
      }
    
      private void Form1_OnLoad(object sender, EventArgs e)
      {
        // Do something with the axer data mgr singleton class (access the Abc property)
        var s  = AxerDataMgr.Abc;
    
        AxerDataMgr.Abc = "hey there";
      }
    
      private void OnButton2_Click(object sender, EventArgs e)
      {
        // Open the 2nd form (note we don't pass in the axer class any more.
        var form2 = new CommonDLLTestForm2(  );
        form2.DoModal( ); 
      }
    }
    
    public partial class CommonDLLTestForm2 : Form
    {
      public CommonDLLTestForm2()
      {
        InitializeComponent();
      }
    
      private void CommonDLLTestForm2_Load(object sender, EventArgs e)
      {
        // Do something with the axer data mgr singleton class (access the Abc property - should be "hey there")
        var s  = AxerDataMgr.Abc;
      }
    }
    The choice whether to use a singleton depends on whether you will ever need more than one instance of the AxerDataMgr class at any time in your program. If you only need one instance (and you want to keep that instance around for the life of the program, then a singleton can work well.
    Last edited by Arjay; April 7th, 2011 at 06:32 PM.

  4. #4
    Join Date
    Oct 2006
    Posts
    82

    Re: How to code access to DLL method from 2 Winforms?

    @Arjay
    I understand all that you wrote. But my original example had two non-static data members. You omitted those and your singleton code introduced static members.

    I know from experiments that all public static members can be accessed directly. See CommonDLLTestForm2_Load, "TMC.UpdateAppsLists()". But the point was to do it in a way that didn't use static members at all.

    Passing the reference to the second subform, may work, but I use ShowDialog(owner) (Is that a modal form?). Anyway, how to pass that reference may pose a problem. I will have to look into it.

    So the next question is...can this be dine without static members?
    Last edited by Lou Arnold; April 7th, 2011 at 06:13 PM.

  5. #5
    Join Date
    Oct 2006
    Posts
    82

    Re: How to code access to DLL method from 2 Winforms?

    Quote Originally Posted by RedBully View Post
    Hello. It's not too clear to me what you are asking. Normally, to use a DLL, you add a reference to your DLL and then put the 'using' command in your project as you have done in your examples.

    In your question you talk about AxerDataMgrClass, frmEnabledApps and MainForm but none of your code examples reference these and so I'm a bit confused.
    Gee, now I'm confused...
    I did as you described: created the DLL as a separate project under VC#, and built it. In another project in the same solution, I created a Mainform and a subform. In that project, I also added a reference to the DLL, and in each form's code file, included the "using" line.
    In the code for the main form, I instantiated the DLL class as an object named DEF. I assumed that since the subform was in the same namespace as the mainform, I could access DEF and therefore the [non-static] members of the DLL. But, of course that didn't work, and I don't really understand why not.

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,480

    Re: How to code access to DLL method from 2 Winforms?

    Quote Originally Posted by Lou Arnold View Post
    @Arjay
    I understand all that you wrote. But my original example had two non-static data members. You omitted those and your singleton code introduced static members.
    I wrote the singleton portion of the code - I didn't put in your public fields. The static members that I added are only for the purpose of turning the class into a singleton. Any other fields, properties, or methods probably shouldn't be static.

    I've updated my original singleton code and added two properties. Please note that I've used properties and not public fields.

    Public fields in C# should be avoided. The reason being is there is advanced functionality (such as binding) that doesn't work on public fields.

    Plus, since .Net 3.0, you have what is called an auto-property, so the public field goes from:
    Code:
    public string Abc;
    to
    Code:
    public string Abc { get; set; }
    Auto-properties also allow you to control access differences between the get and set. For example here is a public property with a private setter.
    Code:
    public string Abc { get; private set; }
    Quote Originally Posted by Lou Arnold View Post
    I know from experiments that all public static members can be accessed directly. See CommonDLLTestForm2_Load, "TMC.UpdateAppsLists()". But the point was to do it in a way that didn't use static members at all.
    My intent was to only make the singleton related members static as I said above.

    Quote Originally Posted by Lou Arnold View Post
    Passing the reference to the second subform, may work, but I use ShowDialog(owner) (Is that a modal form?). Anyway, how to pass that reference may pose a problem I will have to look into it.
    The instance of the AxerDataMgr class is passed into the form constructor as I've shown in the first (non singleton) example.

    Quote Originally Posted by Lou Arnold View Post
    So the next question is...can this be dine without static members?
    Yes, and I've already shown you how. Create an instance of the AxerDataMgr class and pass it to the forms via their constructor.

    The question of whether to make the AxerDataMgr class a singleton partly depends are whether there will ever be more than one instance of the AxerDataMgr class in use at any one time. If there isn't, a singleton class can be convenient (because you don't have to pass the class instance around in every forms constructor).

  7. #7
    Join Date
    Oct 2006
    Posts
    82

    Re: How to code access to DLL method from 2 Winforms?

    Quote Originally Posted by Arjay View Post
    I wrote the singleton portion of the code - I didn't put in your public fields. The static members that I added are only for the purpose of turning the class into a singleton. Any other fields, properties, or methods probably shouldn't be static.

    I've updated my original singleton code and added two properties. Please note that I've used properties and not public fields.

    Public fields in C# should be avoided. The reason being is there is advanced functionality (such as binding) that doesn't work on public fields.

    Plus, since .Net 3.0, you have what is called an auto-property, so the public field goes from:
    Code:
    public string Abc;
    to
    Code:
    public string Abc { get; set; }
    Auto-properties also allow you to control access differences between the get and set. For example here is a public property with a private setter.
    Code:
    public string Abc { get; private set; }
    My intent was to only make the singleton related members static as I said above.

    The instance of the AxerDataMgr class is passed into the form constructor as I've shown in the first (non singleton) example.

    Yes, and I've already shown you how. Create an instance of the AxerDataMgr class and pass it to the forms via their constructor.

    The question of whether to make the AxerDataMgr class a singleton partly depends are whether there will ever be more than one instance of the AxerDataMgr class in use at any one time. If there isn't, a singleton class can be convenient (because you don't have to pass the class instance around in every forms constructor).
    About the properties, I understood. I didn't know you could write a public get and a private set. Thank you for that.

    I understand about the design choice for a singleton.

    Several hours after my last post, I had a chance to think about this all again. Its clear that your method of passing the DLL reference in the form2 constructor was the thing to do.

    Another question: When does one want to use static members? What are the criteria for this design decision?

  8. #8
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,480

    Re: How to code access to DLL method from 2 Winforms?

    Quote Originally Posted by Lou Arnold View Post
    Another question: When does one want to use static members? What are the criteria for this design decision?
    Instance members (non static) is data on a per instance basis. In other words each instance has its own data, if you change data it one instance it doesn't effect data in another instance.

    This differs from static fields, because the data is shared across all instances. So if I had a public int RefCount field and incremented in a the constructor, it would increase in valve each time I created a new instance of the class.

    The best advice I can give is to try this stuff out. Write a few simple progams and step through them in a debugger to see what happends between static and non-static data.

  9. #9
    Join Date
    Oct 2006
    Posts
    82

    Re: How to code access to DLL method from 2 Winforms?

    Quote Originally Posted by Arjay View Post
    Instance members (non static) is data on a per instance basis. In other words each instance has its own data, if you change data it one instance it doesn't effect data in another instance.

    This differs from static fields, because the data is shared across all instances. So if I had a public int RefCount field and incremented in a the constructor, it would increase in valve each time I created a new instance of the class.

    The best advice I can give is to try this stuff out. Write a few simple programs and step through them in a debugger to see what happens between static and non-static data.
    In fact, I have been trying using static members for some months now, so I do understand their uses and behavior. But when you know only a little, you also don't know what else is possible. But in all fairness, you don't know what I don't know either, so my questions are rather unreasonable. Its like asking "tell me all you know about static vs non-static ..."

    Anyway thanks. This discussion has cleared up a few things for me.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center