Why Me? (Instead of Form1)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chack-o

    #1

    Why Me? (Instead of Form1)

    This seems to be a "thing" with all the VStudio languages. Why do I
    refer to things on the form with the Me qualifier, as in
    Me.txtWaffle.Te xt = "Waffles... "
    as opposed to the name of the form itself, as in
    Form1.txtWaffle .Text = "Waffles... "
    ?

    This is a difficult topic to search for. I'm reading through the new
    Lawrenceville Press VB 2005 textbook, and haven't found an answer
    yet...

    Nick.

  • Charlie Brown

    #2
    Re: Why Me? (Instead of Form1)

    In a class you use "me" as well.

    If you create a class that represents a object, say an automobile, in
    the class you would use Me to refer to itself within the code since you
    wouldn't know what the object is called until it is created as an
    instance. Although the class is called automobile, you couldn't refer
    to itself as automobile.[property] since it would be looking for an
    instance of the class.

    Whoa, hope that isn't too confusing.


    Chack-o wrote:[color=blue]
    > This seems to be a "thing" with all the VStudio languages. Why do I
    > refer to things on the form with the Me qualifier, as in
    > Me.txtWaffle.Te xt = "Waffles... "
    > as opposed to the name of the form itself, as in
    > Form1.txtWaffle .Text = "Waffles... "
    > ?
    >
    > This is a difficult topic to search for. I'm reading through the new
    > Lawrenceville Press VB 2005 textbook, and haven't found an answer
    > yet...
    >
    > Nick.[/color]

    Comment

    • eric.goforth@gmail.com

      #3
      Re: Why Me? (Instead of Form1)

      Nick,

      The Me. syntax will work with any object in .NET, not just Form1.

      -Eric

      Chack-o wrote:[color=blue]
      > This seems to be a "thing" with all the VStudio languages. Why do I
      > refer to things on the form with the Me qualifier, as in
      > Me.txtWaffle.Te xt = "Waffles... "
      > as opposed to the name of the form itself, as in
      > Form1.txtWaffle .Text = "Waffles... "
      > ?
      >
      > This is a difficult topic to search for. I'm reading through the new
      > Lawrenceville Press VB 2005 textbook, and haven't found an answer
      > yet...
      >
      > Nick.[/color]

      Comment

      • Göran Andersson

        #4
        Re: Why Me? (Instead of Form1)

        The Me keyword is a refence to the class itself. If you use the name of
        the reference instead, you are limited to one instance of the class.

        If you create two forms from the same class:

        Dim menu1 as MenuForm = new MenuForm
        Dim menu2 as MenuForm = new MenuForm

        Each form can refer to itself using the Me keyword, without having to
        know if the main form refers to it as menu1 or menu2.


        Chack-o wrote:[color=blue]
        > This seems to be a "thing" with all the VStudio languages. Why do I
        > refer to things on the form with the Me qualifier, as in
        > Me.txtWaffle.Te xt = "Waffles... "
        > as opposed to the name of the form itself, as in
        > Form1.txtWaffle .Text = "Waffles... "
        > ?
        >
        > This is a difficult topic to search for. I'm reading through the new
        > Lawrenceville Press VB 2005 textbook, and haven't found an answer
        > yet...
        >
        > Nick.
        >[/color]

        Comment

        • Jim Wooley

          #5
          Re: Why Me? (Instead of Form1)

          > This seems to be a "thing" with all the VStudio languages. Why do I[color=blue]
          > refer to things on the form with the Me qualifier, as in
          > Me.txtWaffle.Te xt = "Waffles... "
          > as opposed to the name of the form itself, as in
          > Form1.txtWaffle .Text = "Waffles... "
          > ?
          > This is a difficult topic to search for. I'm reading through the new
          > Lawrenceville Press VB 2005 textbook, and haven't found an answer
          > yet...
          >
          > Nick.
          >[/color]

          In this case Form1 refers to a Type (something that has potential to be)
          not an instance of the type. For instance, we can do the following:
          dim frmKerry = new Form1
          frmKerry.txtWaf fle.Text = "Waffles... "
          frmKerry.Show()

          If you were to allow the syntax you mentioned what would happen if you had
          2 instances of the Form1 type as follows:

          dim frmRestaurant = new Form1
          dim frmKerry = new Form1

          Then you made the following statement:
          Form1.txtWaffle .Text = "Waffles... " which instance would change the value
          in the box? We would want to explicity state that it is the restaurant's
          waffles we want to change not the politician's.

          The trick is inside of the instance, we need a way to refer to the class
          that has been instantiated. "Me" was chosen as the best syntax for VB (C#
          uses "this"). As was mentioned previously, "me" applies to the current object.
          Since everything in .Net inherits from System.Object, forms, classes, etc
          can all use the "Me" syntax and call the localized property.

          Jim Wooley

          (In case anyone is interested, I am a liberal and have no problems with politician's
          waffling).


          Comment

          • Cor Ligthert [MVP]

            #6
            Re: Why Me? (Instead of Form1)

            Chack,

            A VB Net mainform is still a crazy thing in VSNet. It has some things behind
            the scene which are not in C#. The same is that some, as by instance
            Herfried won't want to use it that way. The mainform can instance itself to
            an object (as you can set in the project properties).

            Therefore if you are busy with the mainform you are busy with the class. If
            you want to tell inside a class that it has to take things from the class
            itself and not from another class/namespace, than you tell "me". The same as
            you in C derived languages as JavaScript and C# you use for that "This".

            I hope this gives an idea.

            Cor


            "Chack-o" <nchackowsky@gm ail.com> schreef in bericht
            news:1149617348 .877991.142960@ u72g2000cwu.goo glegroups.com.. .[color=blue]
            > This seems to be a "thing" with all the VStudio languages. Why do I
            > refer to things on the form with the Me qualifier, as in
            > Me.txtWaffle.Te xt = "Waffles... "
            > as opposed to the name of the form itself, as in
            > Form1.txtWaffle .Text = "Waffles... "
            > ?
            >
            > This is a difficult topic to search for. I'm reading through the new
            > Lawrenceville Press VB 2005 textbook, and haven't found an answer
            > yet...
            >
            > Nick.
            >[/color]


            Comment

            • Chack-o

              #7
              Re: Why Me? (Instead of Form1)


              Jim Wooley wrote:[color=blue]
              > If you were to allow the syntax you mentioned what would happen if you had
              > 2 instances of the Form1 type as follows:
              >
              > dim frmRestaurant = new Form1
              > dim frmKerry = new Form1
              >
              > Then you made the following statement:
              > Form1.txtWaffle .Text = "Waffles... " which instance would change the value
              > in the box? We would want to explicity state that it is the restaurant's
              > waffles we want to change not the politician's.
              >
              > The trick is inside of the instance, we need a way to refer to the class
              > that has been instantiated. "Me" was chosen as the best syntax for VB (C#
              > uses "this"). As was mentioned previously, "me" applies to the current object.
              > Since everything in .Net inherits from System.Object, forms, classes, etc
              > can all use the "Me" syntax and call the localized property.[/color]

              I think I'm slowly getting it. Correct me if I'm wrong.
              Form1 is a type or a class. We can create instances of Form1
              But why is it called Form1 and not Form?
              Form1 is _also_ (somewhat confusingly) the Name property of the
              default Form1 object that's created when I start a new VB project.
              If I have a project with more than one form, then I would use
              SecondFormName. whatever when referring to the other form.

              Thanks for the help.

              Nick.

              Comment

              • Cor Ligthert [MVP]

                #8
                Re: Why Me? (Instead of Form1)

                > But why is it called Form1 and not Form?

                Simple, because Form exist already as class as

                System.Windows. Forms.Form

                To that is set in your projectproperti es an imports path so it takes it
                direct.

                I hope this helps,

                Cor


                Comment

                Working...