Object creation based on string

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

    #1

    Object creation based on string

    I am storing the name of a Crystal Report in a combobox.
    I want to create an instance of this report based on this
    string. Is this possible? Or am I going to have to code
    a Case statement? This is my current code:
    crReportDocumen t = new rptReferredPati ents ();

    But I have rptReferredPati ents stored in
    cboReportsAvail able.ValueMembe r

    Thanks.
  • Jay B. Harlow [MVP - Outlook]

    #2
    Re: Object creation based on string

    Phill,
    I normally use System.Type.Get Type to get a type object based on the string.

    Then I use System.Activato r.CreateInstanc e to create an instance of an
    object based on that type.

    For details & info see:


    Hope this helps
    Jay

    "Phill" <pshaw@proceed. com> wrote in message
    news:8c5801c37e eb$6ab75540$a60 1280a@phx.gbl.. .[color=blue]
    > I am storing the name of a Crystal Report in a combobox.
    > I want to create an instance of this report based on this
    > string. Is this possible? Or am I going to have to code
    > a Case statement? This is my current code:
    > crReportDocumen t = new rptReferredPati ents ();
    >
    > But I have rptReferredPati ents stored in
    > cboReportsAvail able.ValueMembe r
    >
    > Thanks.[/color]


    Comment

    • Phill

      #3
      Re: Object creation based on string

      Thanks Jay...I just don't understand your suggestion.
      Here is my code snippet. The line I want to replace is
      "this.OpenForm( new frmRptParmRefer redPatients()); "
      frmRptParmRefer redPatients is stored in
      this.cboReports Available.Selec tedValue. How can this be
      done?

      //Fill the Reports Available combobox
      string ConnStr =
      System.Configur ation.Configura tionSettings.Ap pSettings
      ["ConnStr"];

      this.cboReports Available.DataS ource= new ReportsCollecti on
      (new SqlConnection(C onnStr));

      cboReportsAvail able.DisplayMem ber = "ReportName ";
      cboReportsAvail able.ValueMembe r = "ReportDefiniti on";

      this.OpenForm(n ew frmRptParmRefer redPatients());


      [color=blue]
      >-----Original Message-----
      >Phill,
      >I normally use System.Type.Get Type to get a type object[/color]
      based on the string.[color=blue]
      >
      >Then I use System.Activato r.CreateInstanc e to create an[/color]
      instance of an[color=blue]
      >object based on that type.
      >
      >For details & info see:
      >http://www.yoda.arachsys.com/csharp/plugin.html
      >
      >Hope this helps
      >Jay
      >
      >"Phill" <pshaw@proceed. com> wrote in message
      >news:8c5801c37 eeb$6ab75540$a6 01280a@phx.gbl. ..[color=green]
      >> I am storing the name of a Crystal Report in a[/color][/color]
      combobox.[color=blue][color=green]
      >> I want to create an instance of this report based on[/color][/color]
      this[color=blue][color=green]
      >> string. Is this possible? Or am I going to have to[/color][/color]
      code[color=blue][color=green]
      >> a Case statement? This is my current code:
      >> crReportDocumen t = new rptReferredPati ents ();
      >>
      >> But I have rptReferredPati ents stored in
      >> cboReportsAvail able.ValueMembe r
      >>
      >> Thanks.[/color]
      >
      >
      >.
      >[/color]

      Comment

      • Tobin Harris

        #4
        Re: Object creation based on string

        "Phill" <pshaw@proceed. com> wrote in message
        news:05f101c37e f0$368fcb30$a40 1280a@phx.gbl.. .[color=blue]
        > Thanks Jay...I just don't understand your suggestion.
        > Here is my code snippet. The line I want to replace is
        > "this.OpenForm( new frmRptParmRefer redPatients()); "
        > frmRptParmRefer redPatients is stored in
        > this.cboReports Available.Selec tedValue. How can this be
        > done?[/color]

        How about this...

        Form toOpen =
        Activator.Creat eInstance(Type. GetType(this.cb oReportsAvailab le.SelectedValu e
        )) as Form;
        this.OpenForm( toOpen );

        Not totally sure if that's right, but could be a start!?

        Tobin Harris


        Comment

        • Jay B. Harlow [MVP - Outlook]

          #5
          Re: Object creation based on string

          Phill,
          The code that Tobin Harris gave should work.

          If not the URL I gave has a number of examples of how to use
          Activator.Creat eInstance.

          Hope this helps
          Jay

          "Phill" <pshaw@proceed. com> wrote in message
          news:05f101c37e f0$368fcb30$a40 1280a@phx.gbl.. .[color=blue]
          > Thanks Jay...I just don't understand your suggestion.
          > Here is my code snippet. The line I want to replace is
          > "this.OpenForm( new frmRptParmRefer redPatients()); "
          > frmRptParmRefer redPatients is stored in
          > this.cboReports Available.Selec tedValue. How can this be
          > done?
          >
          > //Fill the Reports Available combobox
          > string ConnStr =
          > System.Configur ation.Configura tionSettings.Ap pSettings
          > ["ConnStr"];
          >
          > this.cboReports Available.DataS ource= new ReportsCollecti on
          > (new SqlConnection(C onnStr));
          >
          > cboReportsAvail able.DisplayMem ber = "ReportName ";
          > cboReportsAvail able.ValueMembe r = "ReportDefiniti on";
          >
          > this.OpenForm(n ew frmRptParmRefer redPatients());
          >
          >
          >[color=green]
          > >-----Original Message-----
          > >Phill,
          > >I normally use System.Type.Get Type to get a type object[/color]
          > based on the string.[color=green]
          > >
          > >Then I use System.Activato r.CreateInstanc e to create an[/color]
          > instance of an[color=green]
          > >object based on that type.
          > >
          > >For details & info see:
          > >http://www.yoda.arachsys.com/csharp/plugin.html
          > >
          > >Hope this helps
          > >Jay
          > >
          > >"Phill" <pshaw@proceed. com> wrote in message
          > >news:8c5801c37 eeb$6ab75540$a6 01280a@phx.gbl. ..[color=darkred]
          > >> I am storing the name of a Crystal Report in a[/color][/color]
          > combobox.[color=green][color=darkred]
          > >> I want to create an instance of this report based on[/color][/color]
          > this[color=green][color=darkred]
          > >> string. Is this possible? Or am I going to have to[/color][/color]
          > code[color=green][color=darkred]
          > >> a Case statement? This is my current code:
          > >> crReportDocumen t = new rptReferredPati ents ();
          > >>
          > >> But I have rptReferredPati ents stored in
          > >> cboReportsAvail able.ValueMembe r
          > >>
          > >> Thanks.[/color]
          > >
          > >
          > >.
          > >[/color][/color]


          Comment

          Working...