Open Form from Name #3

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • vbdev

    Open Form from Name #3

    Hi,

    This is my 3rd post on the issue, but I am getting closer to a solution:

    I have 1 project with 50 winforms name test01 to test50. These forms have a
    public method named: MyVal

    Right now I have the following code (where fName holds the form name):

    string st = "init";
    if (fName == "Test01")

    {

    Test01 myForm = new Test01();

    myForm.MyVal = st;

    myForm.ShowDial og();

    st = myForm.MyVal;

    }

    else if (fName == "Test02")

    {

    Test02 myForm = new Test02();

    myForm.MyVal = st;

    myForm.ShowDial og();

    st = myForm.MyVal;

    }

    ..... all the way the Test50!!!!!!!!! !!!!!!!!!

    This looks stupid! that's 49 elseif!

    I am trying to make one generic call where I am feeding only the form name:
    TestXX as a string:

    If I have a var fName that holds my current form name, I would like to have

    something(fName ) myForm = new something(fName )

    myForm.MyVal = st;

    myForm.ShowDial og();

    st = myForm.MyVal;

    Any idea?



    Thanks

    vbdev


  • John B

    #2
    Re: Open Form from Name #3

    vbdev wrote:[color=blue]
    > Hi,
    >
    > This is my 3rd post on the issue, but I am getting closer to a solution:
    >
    > I have 1 project with 50 winforms name test01 to test50. These forms have a
    > public method named: MyVal
    >
    > Right now I have the following code (where fName holds the form name):
    >
    > string st = "init";
    > if (fName == "Test01")
    >
    > {
    >
    > Test01 myForm = new Test01();
    >
    > myForm.MyVal = st;
    >
    > myForm.ShowDial og();
    >
    > st = myForm.MyVal;
    >
    > }
    >
    > else if (fName == "Test02")
    >
    > {
    >
    > Test02 myForm = new Test02();
    >
    > myForm.MyVal = st;
    >
    > myForm.ShowDial og();
    >
    > st = myForm.MyVal;
    >
    > }
    >
    > .... all the way the Test50!!!!!!!!! !!!!!!!!!
    >
    > This looks stupid! that's 49 elseif!
    >
    > I am trying to make one generic call where I am feeding only the form name:
    > TestXX as a string:
    >
    > If I have a var fName that holds my current form name, I would like to have
    >
    > something(fName ) myForm = new something(fName )
    >
    > myForm.MyVal = st;
    >
    > myForm.ShowDial og();
    >
    > st = myForm.MyVal;
    >
    > Any idea?
    >
    >
    >
    > Thanks
    >
    > vbdev
    >
    >[/color]
    You should be able to do it using reflection.
    Not quite as fast though, but you probably wont notice it.
    You might need to define an interface that exposes MyVal as well.

    Cheers
    JB

    Comment

    • vbdev

      #3
      Re: Open Form from Name #3

      Yes, I sure that I could. But after wasting hours, and going nowhere, the
      question is how?

      Thanks

      vbdev


      "John B" <jbngspam@yahoo .com> wrote in message
      news:42815e5d$0 $79456$14726298 @news.sunsite.d k...[color=blue]
      > vbdev wrote:[color=green]
      >> Hi,
      >>
      >> This is my 3rd post on the issue, but I am getting closer to a solution:
      >>
      >> I have 1 project with 50 winforms name test01 to test50. These forms
      >> have a public method named: MyVal
      >>
      >> Right now I have the following code (where fName holds the form name):
      >>
      >> string st = "init";
      >> if (fName == "Test01")
      >>
      >> {
      >>
      >> Test01 myForm = new Test01();
      >>
      >> myForm.MyVal = st;
      >>
      >> myForm.ShowDial og();
      >>
      >> st = myForm.MyVal;
      >>
      >> }
      >>
      >> else if (fName == "Test02")
      >>
      >> {
      >>
      >> Test02 myForm = new Test02();
      >>
      >> myForm.MyVal = st;
      >>
      >> myForm.ShowDial og();
      >>
      >> st = myForm.MyVal;
      >>
      >> }
      >>
      >> .... all the way the Test50!!!!!!!!! !!!!!!!!!
      >>
      >> This looks stupid! that's 49 elseif!
      >>
      >> I am trying to make one generic call where I am feeding only the form
      >> name: TestXX as a string:
      >>
      >> If I have a var fName that holds my current form name, I would like to
      >> have
      >>
      >> something(fName ) myForm = new something(fName )
      >>
      >> myForm.MyVal = st;
      >>
      >> myForm.ShowDial og();
      >>
      >> st = myForm.MyVal;
      >>
      >> Any idea?
      >>
      >>
      >>
      >> Thanks
      >>
      >> vbdev
      >>
      >>[/color]
      > You should be able to do it using reflection.
      > Not quite as fast though, but you probably wont notice it.
      > You might need to define an interface that exposes MyVal as well.
      >
      > Cheers
      > JB[/color]


      Comment

      • John B

        #4
        Re: Open Form from Name #3

        vbdev wrote:[color=blue]
        > Yes, I sure that I could. But after wasting hours, and going nowhere, the
        > question is how?
        >[/color]

        Assembly ThisAssembly = Assembly.GetExe cutingAssembly( );
        Form ActivatedForm =
        (Form)ThisAssem bly.CreateInsta nce(<NamespaceQ ualifiedClassNa me>);
        ((IMyVal)Activa tedForm).Value = 10;
        ActivatedForm.S how();

        JB
        <snip>

        Comment

        • Alan Pretre

          #5
          Re: Open Form from Name #3

          "vbdev" <vb6dev2003@hot mail.com> wrote in message
          news:O4KVqZcVFH A.2128@TK2MSFTN GP14.phx.gbl...[color=blue]
          > I have 1 project with 50 winforms name test01 to test50. These forms have
          > a public method named: MyVal
          >
          > I am trying to make one generic call where I am feeding only the form
          > name: TestXX as a string:
          >
          > If I have a var fName that holds my current form name, I would like to
          > have
          >
          > something(fName ) myForm = new something(fName )
          > myForm.MyVal = st;
          > myForm.ShowDial og();
          > st = myForm.MyVal;
          >
          > Any idea?[/color]


          Hi. You should create an interface for the method. For example:

          public interface IFoo {
          string MyVal();
          }



          Then in your window class, implement the interface:

          public class Test01 : System.Windows. Forms.Form, IFoo {
          // Other normal window stuff in the class.

          // Implement IFoo.MyVal
          string IFoo.MyVal() {
          return "xyz"; // Whatever
          }
          }



          Then in your consumer:

          // Instantiate the window by name.
          System.Runtime. Remoting.Object Handle Obj =
          System.Activato r.CreateInstanc eFrom("MyAssemb ly", "Test01");
          object c = Obj.Unwrap();

          // Show the window. This works regardless of the window type instantiated.
          ((System.Window s.Forms.Form) c).ShowDialog() ;

          // Fetch value. This works regardless of which window type is instantiated.
          string st = ((IFoo) c).MyVal();

          // Important! Dispose the form.
          ((IDisposable) c).Dispose();


          I typed this all from memory, so there may be a few typos. But it should be
          generally correct.

          -- Alan


          Comment

          • Christof Nordiek

            #6
            Re: Open Form from Name #3


            "vbdev" <vb6dev2003@hot mail.com> schrieb im Newsbeitrag
            news:O4KVqZcVFH A.2128@TK2MSFTN GP14.phx.gbl...[color=blue]
            > Hi,
            >
            > This is my 3rd post on the issue, but I am getting closer to a solution:
            >
            > I have 1 project with 50 winforms name test01 to test50. These forms have
            > a public method named: MyVal
            >
            > Right now I have the following code (where fName holds the form name):
            >
            > string st = "init";
            > if (fName == "Test01")
            >
            > {
            >
            > Test01 myForm = new Test01();
            >
            > myForm.MyVal = st;
            >
            > myForm.ShowDial og();
            >
            > st = myForm.MyVal;
            >
            > }
            >
            > else if (fName == "Test02")
            >
            > {
            >
            > Test02 myForm = new Test02();
            >
            > myForm.MyVal = st;
            >
            > myForm.ShowDial og();
            >
            > st = myForm.MyVal;
            >
            > }
            >
            > .... all the way the Test50!!!!!!!!! !!!!!!!!!
            >
            > This looks stupid! that's 49 elseif!
            >
            > I am trying to make one generic call where I am feeding only the form
            > name: TestXX as a string:
            >
            > If I have a var fName that holds my current form name, I would like to
            > have
            >
            > something(fName ) myForm = new something(fName )
            >
            > myForm.MyVal = st;
            >
            > myForm.ShowDial og();
            >
            > st = myForm.MyVal;
            >
            > Any idea?
            >
            >
            >
            > Thanks
            >
            > vbdev
            >
            >[/color]

            First i would try to use polymorphy in the right way:

            BaseForm myForm;
            string st = "init";
            switch (fName)
            {
            case "Test01":
            myForm = new Text01();
            break;
            case "Test02";
            myForm = new Test02();
            break;
            ........
            }
            myForm.MyVal = st;

            myForm.ShowDial og();

            st = myForm.MyVal;

            BaseForm would be a Class deriving from System.Windows. Forms.Form, and
            containing the member MyVal.
            All ypur Forms should derive from BaseForm.

            Christof


            Comment

            • Alan Pretre

              #7
              Re: Open Form from Name #3

              "Christof Nordiek" <cn@nospam.de > wrote in message
              news:%23TZJczfV FHA.1384@TK2MSF TNGP09.phx.gbl. ..[color=blue]
              > First i would try to use polymorphy in the right way:
              >
              > BaseForm would be a Class deriving from System.Windows. Forms.Form, and
              > containing the member MyVal.
              > All ypur Forms should derive from BaseForm.[/color]

              What you have will work, but it's better suited for std C++, which lacks
              reflection and interfaces.

              The code I have posted is better for at least 2 reasons:
              - It uses dynamic class instantiation to avoid large switch statements,
              - It uses an interface, which is superior to your inheritance model.

              -- Alan


              Comment

              Working...