Is Object Datatype right?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Aads
    New Member
    • Mar 2008
    • 48

    Is Object Datatype right?

    Hi there,

    I've two projects - one is an exe & the other is a dll. I add a reference of the dll project to the exe so that it can access dll project's types. Now in my dll project I've code which requires reference to the exe project; this is not possible as it would result in circular dependency. Hence all I'm doing is I'm passing exe's type say for example the MDIForm of the exe project to the dll project with datatype being Object. Now I can happily access all the MDIForm's members in the dll project.

    My question:
    1. Is it okay to declare the MDIForm as Object datatype in the dll project?
    2. The disadvantage of using the Object datatype is that I cannot see its members while coding i.e. intellisense doesn't shows the MDIForm's members. Is there a way to typecast it to the exact type?


    Thanks,
    Aads
  • balame2004
    New Member
    • Mar 2008
    • 142

    #2
    There is a way to typecase your MDI form that you have passed as object.

    You just mention the object with the type of Form. So intellisense will show the members of the MDI form object.

    Eg:
    C#:
    string formName=((Form )mdiFormObj).Na me;

    Comment

    • Aads
      New Member
      • Mar 2008
      • 48

      #3
      Thanks for your quick reply.

      If I typecast it to Form, It would only show the standard form members not the one which I have written. For example: I might have written a public function by name AddTwoNumbers, which would not appear in the intellisense as it is not a member of a standard form but the member of my class. I want this member to be displayed in the intellisense. In fact it could be any user defined class for that matter.

      Hope now its clear.

      Thanks,
      Aads

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Perhaps you should consider making all of your custom functions declared as an interface(or something) and keep that in the DLL.
        So you can reference the functions that way?

        Comment

        • madankarmukta
          Contributor
          • Apr 2008
          • 308

          #5
          Originally posted by Aads
          Thanks for your quick reply.

          If I typecast it to Form, It would only show the standard form members not the one which I have written. For example: I might have written a public function by name AddTwoNumbers, which would not appear in the intellisense as it is not a member of a standard form but the member of my class. I want this member to be displayed in the intellisense. In fact it could be any user defined class for that matter.

          Hope now its clear.

          Thanks,
          Aads
          Could you please once chec for the accessibilityof the members which you are calling..?

          Are the member , you are expecting to be there in intellisence list ,accessible in
          called environment ?

          Thanks!

          Comment

          • Aads
            New Member
            • Mar 2008
            • 48

            #6
            Originally posted by Plater
            Perhaps you should consider making all of your custom functions declared as an interface(or something) and keep that in the DLL.
            So you can reference the functions that way?
            That means it will be available to me superficially.. Having said that is there any way that I can typecast it to my exact type?

            Thanks,
            Aads

            Comment

            • Aads
              New Member
              • Mar 2008
              • 48

              #7
              Originally posted by madankarmukta
              Could you please once chec for the accessibilityof the members which you are calling..?

              Are the member , you are expecting to be there in intellisence list ,accessible in
              called environment ?

              Thanks!
              Yeah the accessibility of my methods are all okay i.e. they are all public.Since I'm not able to typecast it to the exact type & instead using Object datatype as a solution, I'm not able to see the members in the intellisense.

              Thanks,
              Aads

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                I'm pretty sure my terminology is wrong, as I haven't actually done this, but I think it can be done.
                What I was thinking was that all your custom functions where in an interface "myIFace" say that is stored in your DLL. Your EXE program has a form that implements that interface. Now your EXE side works as normal.
                Then your DLL could be told to use myIFace instead of Object as the datatype, and all your custom functions should appear in the intelisense?

                Comment

                • Frinavale
                  Recognized Expert Expert
                  • Oct 2006
                  • 9749

                  #9
                  Originally posted by Aads
                  Now in my dll project I've code which requires reference to the exe project
                  Perhaps you should consider redesigning your application.

                  A DLL (a class library) is supposed to be a library of classes that can be used in other applications. It should not be concerned with the code that is using it.

                  Why does your DLL need to reference something in the application (the EXE) that is using it?

                  Does it just need a reference to a method?
                  In that case consider using Delegates (or Function Pointers) so that the class within the DLL can reference the method.

                  What does the DLL have to reference and Why?

                  Comment

                  • Aads
                    New Member
                    • Mar 2008
                    • 48

                    #10
                    Originally posted by Frinavale
                    Perhaps you should consider redesigning your application.

                    A DLL (a class library) is supposed to be a library of classes that can be used in other applications. It should not be concerned with the code that is using it.

                    Why does your DLL need to reference something in the application (the EXE) that is using it?

                    Does it just need a reference to a method?
                    In that case consider using Delegates (or Function Pointers) so that the class within the DLL can reference the method.

                    What does the DLL have to reference and Why?
                    Well that's a good question. You are absolutely right that a dll project should be independent of the exe project.

                    Well I'm working on a mechanical project containing intensive mathematical calculations - the UI is just an exe containing MDI form & the other dll projects contains only mathematical calculations. I have a ToolStripProgre ssBar control on the MDI form of the exe project which needs to be updated from the dll project to report the progress to the user as the calculation progresses. Hence I need to access the exe project from the dll project.

                    Secondly there are three language options in the MDI form namely French, English & German. Based on the language selected my dll project's mathematical sub routines vary slightly. Again in this case, I need to access the exe project's members.

                    Hope its clear now.

                    By the way, could you please tell me how delegates can solve this problem?

                    Thanks,
                    Aads

                    Comment

                    • Frinavale
                      Recognized Expert Expert
                      • Oct 2006
                      • 9749

                      #11
                      In your case you should probably look into using Events.

                      After a "milestone" has been reached in your math calculations you should raise an event indicate it's progress....

                      Your windows application will implement a method that handles this event and updates the progress bar :)

                      As for the cultural settings....you could create a Property for the class that's doing the calculation which can be set to indicate which calculation should be preformed....

                      Or you could look at using Globalization (I've never used this with a class library though so you'll have to look into this)

                      Comment

                      • Aads
                        New Member
                        • Mar 2008
                        • 48

                        #12
                        Originally posted by Plater
                        I'm pretty sure my terminology is wrong, as I haven't actually done this, but I think it can be done.
                        What I was thinking was that all your custom functions where in an interface "myIFace" say that is stored in your DLL. Your EXE program has a form that implements that interface. Now your EXE side works as normal.
                        Then your DLL could be told to use myIFace instead of Object as the datatype, and all your custom functions should appear in the intelisense?
                        That's great! Good logic! and guess what it worked! As you said, I declared an Interface in the dll project & then implemented the same in both exe project's MDI Form & in the classes of the dll project & then used the interface as the datatype instead of object. Now I can see the members in the intellisense. Cheers buddy I appreciate it.

                        Thanks,
                        Aads

                        Comment

                        • Aads
                          New Member
                          • Mar 2008
                          • 48

                          #13
                          Originally posted by Frinavale
                          In your case you should probably look into using Events.

                          After a "milestone" has been reached in your math calculations you should raise an event indicate it's progress....

                          Your windows application will implement a method that handles this event and updates the progress bar :)

                          As for the cultural settings....you could create a Property for the class that's doing the calculation which can be set to indicate which calculation should be preformed....

                          Or you could look at using Globalization (I've never used this with a class library though so you'll have to look into this)
                          Thanks for your quick reply. Using Events is not a bad idea, however, I need to do bit of R & D on that.

                          Thanks,
                          Aads

                          Comment

                          • Bassem
                            Contributor
                            • Dec 2008
                            • 344

                            #14
                            No it isn't, I was addressing the same situation and I reached to events by my self .. I was glad for that. Now I'm glad again to see an expert recommend my solution.

                            Why don't you pass a value like an enum to which class in the DLL as an input to choose between your choices instead of reading it bottom from DLL up to the UI?

                            Thanks,
                            Bassem

                            Comment

                            • Frinavale
                              Recognized Expert Expert
                              • Oct 2006
                              • 9749

                              #15
                              Originally posted by Bassem
                              No it isn't, I was addressing the same situation and I reached to events by my self .. I was glad for that. Now I'm glad again to see an expert recommend my solution.

                              Why don't you pass a value like an enum to which class in the DLL as an input to choose between your choices instead of reading it bottom from DLL up to the UI?

                              Thanks,
                              Bassem
                              I really think that using Globalization would be even easier than passing this information.... what ever culture the the code is being run under would determine the calculation to do.

                              Comment

                              Working...