Macro Substitution?

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

    Macro Substitution?

    I think "macro substitution" is the correct term for what I want to do, but,
    to be sure, here is a description of what I'd like to know is possible:

    I want to be able to create a create an object of a type whose name is
    stored in a constant. For example:


    Const FORM_NAME_1 as String = "frmThisFor m"
    Const FORM_NAME_2 as String = "frmThatFor m"

    Dim frmTemp as Form


    If condition then
    frmTemp = CType(frmTemp, FORM_NAME_1)
    frmTemp = New FORM_NAME_1
    else
    frmTemp = CType(frmTemp, FORM_NAME_2)
    frmTemp = New FORM_NAME_2
    end if

    frmTemp.Show

    etc....


    In the pseudo code above, I want to somehow access the string stored in
    FORM_NAME_1 and FORM_NAME_2 in the lines of code where I use CType and New.
    Is it possible to even do this in VB.NET?

    - Don


  • Herfried K. Wagner [MVP]

    #2
    Re: Macro Substitution?

    * "Don" <unknown@oblivi on.com> scripsit:[color=blue]
    > I think "macro substitution" is the correct term for what I want to do, but,
    > to be sure, here is a description of what I'd like to know is possible:
    >
    > I want to be able to create a create an object of a type whose name is
    > stored in a constant. For example:[/color]

    That's not possible with VB.NET.

    --
    Herfried K. Wagner [MVP]
    <http://www.mvps.org/dotnet>

    Comment

    • Jay B. Harlow [MVP - Outlook]

      #3
      Re: Macro Substitution?

      Don,[color=blue]
      > I think "macro substitution" is the correct term for what I want to do,[/color]
      but,[color=blue]
      > to be sure, here is a description of what I'd like to know is possible:[/color]
      I normally reserve "Macro substitution" for the ability replace text
      dynamically during C++ compilation. A feature that VB.NET does not support.
      [color=blue]
      > In the pseudo code above, I want to somehow access the string stored in
      > FORM_NAME_1 and FORM_NAME_2 in the lines of code where I use CType and[/color]
      New.[color=blue]
      > Is it possible to even do this in VB.NET?[/color]
      Not with CType & New.

      Have a look at the System.Activato r.CreateInstanc e method, which allows you
      to dynamically load & create an object based on a string.

      Something like:
      [color=blue]
      > Const FORM_NAME_1 as String = "frmThisFor m"
      > Const FORM_NAME_2 as String = "frmThatFor m"[/color]
      Dim frmTemp As Form[color=blue]
      > If condition then[/color]
      Dim typeForm As Type = Type.GetType(FO RM_NAME_1)
      Dim value As Object = Activator.Creat eInstance(typeF orm)
      frmTemp = DirectCast(valu e, Form)[color=blue]
      > else[/color]

      Dim typeForm As Type = Type.GetType(FO RM_NAME_2)
      Dim value As Object = Activator.Creat eInstance(typeF orm)
      frmTemp = DirectCast(valu e, Form)[color=blue]
      > end if[/color]

      Note for Type.GetType to work above, "frmThisFor m" needs to be in the
      current assembly. If "frmThisFor m" is not in the current assembly, then
      FORM_NAME_1 needs to be in the "namespace.clas s, assembly" instead of simply
      the "class" format.

      Hope this helps
      Jay

      "Don" <unknown@oblivi on.com> wrote in message
      news:6YaYb.5317 65$X%5.223861@p d7tw2no...[color=blue]
      > I think "macro substitution" is the correct term for what I want to do,[/color]
      but,[color=blue]
      > to be sure, here is a description of what I'd like to know is possible:
      >
      > I want to be able to create a create an object of a type whose name is
      > stored in a constant. For example:
      >
      >
      > Const FORM_NAME_1 as String = "frmThisFor m"
      > Const FORM_NAME_2 as String = "frmThatFor m"
      >
      > Dim frmTemp as Form
      >
      >
      > If condition then
      > frmTemp = CType(frmTemp, FORM_NAME_1)
      > frmTemp = New FORM_NAME_1
      > else
      > frmTemp = CType(frmTemp, FORM_NAME_2)
      > frmTemp = New FORM_NAME_2
      > end if
      >
      > frmTemp.Show
      >
      > etc....
      >
      >
      > In the pseudo code above, I want to somehow access the string stored in
      > FORM_NAME_1 and FORM_NAME_2 in the lines of code where I use CType and[/color]
      New.[color=blue]
      > Is it possible to even do this in VB.NET?
      >
      > - Don
      >
      >[/color]


      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: Macro Substitution?

        Addendum:
        [color=blue][color=green]
        >> I think "macro substitution" is the correct term for what I want to do, but,
        >> to be sure, here is a description of what I'd like to know is possible:
        >>
        >> I want to be able to create a create an object of a type whose name is
        >> stored in a constant. For example:[/color]
        >
        > That's not possible with VB.NET.[/color]

        I pressed "Send" before completing my reply...

        <http://www.google.de/groups?selm=bum k7n%24j9mbf%241 %40ID-208219.news.uni-berlin.de>

        --
        Herfried K. Wagner [MVP]
        <http://www.mvps.org/dotnet>

        Comment

        • Don

          #5
          Re: Macro Substitution?

          I figured out how to do what I wanted:


          Private Sub MacroSubstution Example(ByVal blnUseClass1 as Boolean)

          ' These are constants containing the names of my classes (the names must
          include the namespace)
          Const CLASS_NAME_1 as String = "MyNameSpace.My ClassName1"
          Const CLASS_NAME_2 as String = "MyNameSpace.My ClassName2"

          Dim strType As String ' I'll throw the name of the class I want in
          here.
          Dim obj As clsBase ' This is the object with which I will be
          instantiation either MyClassName1 or MyClassName1
          ' NOTE: In my example, clsBase is the parent
          class of MyClassName1 and MyClassName1. Using
          ' Dim obj As Object would work just as well.

          Try

          ' Determine which class I should instantiate
          If blnUseClass1 Then
          strType = CLASS_NAME_1
          Else
          strType = CLASS_NAME_2
          End If

          ' Create new object of the type that we want (** This is it!!! **)
          obj = Activator.Creat eInstance(Type. GetType(strType ))

          ' Show a message which displays the type of the object I just
          instantiated
          MsgBox(TypeName (obj))

          ' Clean up
          obj = Nothing

          Catch ex As Exception

          ' Display error message
          MsgBox(ex.Messa ge)

          End Try

          End Sub


          This worked for me. Thanks for the tips, guys!

          - Don


          Comment

          Working...