with statement in C-sharp?

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

    #1

    with statement in C-sharp?

    Hi,
    is there equivalent statement in C# as WITH ... END in VB?

    instead of:

    frm.dataGridVie w1.AutoGenerate Columns = true;
    frm.dataGridVie w1.DataSource = l_oDataset;
    frm.dataGridVie w1.DataMember = this.DataObject Name ;

    I can write in VB:

    with frm.dataGridVie w1
    .AutoGenerateCo lumns = true;
    .DataSource = l_oDataset;
    .DataMember = this.DataObject Name ;
    end with

    This code is better...

    Thanks
    Nhan



  • Mark Rae [MVP]

    #2
    Re: with statement in C-sharp?

    "Nhan" <le.nhan@freene t.dewrote in message
    news:%2308S7Te1 HHA.5992@TK2MSF TNGP02.phx.gbl. ..

    is there equivalent statement in C# as WITH ... END in VB?
    No, thank God!



    --
    Mark Rae
    ASP.NET MVP


    Comment

    • MikeJ

      #3
      Re: with statement in C-sharp?

      I Come From Win32 oop programming
      lots of companies have a lot of legacy code in vb6
      and the with statement in my opinion is usless...but vb needs things like
      this for performance reasons...

      myobj = new someobj()
      mystruct
      mytype
      etc
      all have members, properties, fields, etc and so should be denoted by the
      object.member

      MJ



      "Nhan" <le.nhan@freene t.dewrote in message
      news:%2308S7Te1 HHA.5992@TK2MSF TNGP02.phx.gbl. ..
      Hi,
      is there equivalent statement in C# as WITH ... END in VB?
      >
      instead of:
      >
      frm.dataGridVie w1.AutoGenerate Columns = true;
      frm.dataGridVie w1.DataSource = l_oDataset;
      frm.dataGridVie w1.DataMember = this.DataObject Name ;
      >
      I can write in VB:
      >
      with frm.dataGridVie w1
      .AutoGenerateCo lumns = true;
      .DataSource = l_oDataset;
      .DataMember = this.DataObject Name ;
      end with
      >
      This code is better...
      >
      Thanks
      Nhan
      >
      >
      >

      Comment

      • Brian Gideon

        #4
        Re: with statement in C-sharp?

        On Aug 3, 10:43 am, "Nhan" <le.n...@freene t.dewrote:
        Hi,
        is there equivalent statement in C# as WITH ... END in VB?
        >
        instead of:
        >
        frm.dataGridVie w1.AutoGenerate Columns = true;
        frm.dataGridVie w1.DataSource = l_oDataset;
        frm.dataGridVie w1.DataMember = this.DataObject Name ;
        >
        I can write in VB:
        >
        with frm.dataGridVie w1
        .AutoGenerateCo lumns = true;
        .DataSource = l_oDataset;
        .DataMember = this.DataObject Name ;
        end with
        >
        This code is better...
        >
        Thanks
        Nhan
        Sure...it's elegantly implemented like this.

        DataGridView a = frm.dataGridVie w1;
        a.AutoGenerateC olumns = true;
        a.DataSource = I_oDataset;
        a.DataMember = this.DataObject Name;

        ....which is both more concise and more readable.

        Comment

        • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

          #5
          Re: with statement in C-sharp?

          Nhan wrote:
          is there equivalent statement in C# as WITH ... END in VB?
          There are not.

          http://msdn2.microsoft.com/en-us/vcsharp/Aa336816.aspx

          gives MS's explanation of why.

          Arne

          Comment

          • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

            #6
            Re: with statement in C-sharp?

            MikeJ wrote:
            I Come From Win32 oop programming
            lots of companies have a lot of legacy code in vb6
            and the with statement in my opinion is usless...but vb needs things like
            this for performance reasons...
            Actually Pascal and Modula-2 has it as well. And not for
            performance reasons.

            Arne

            Comment

            • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

              #7
              Re: with statement in C-sharp?

              Brian Gideon wrote:
              On Aug 3, 10:43 am, "Nhan" <le.n...@freene t.dewrote:
              >Hi,
              >is there equivalent statement in C# as WITH ... END in VB?
              >>
              >instead of:
              >>
              >frm.dataGridVi ew1.AutoGenerat eColumns = true;
              >frm.dataGridVi ew1.DataSource = l_oDataset;
              >frm.dataGridVi ew1.DataMember = this.DataObject Name ;
              >>
              >I can write in VB:
              >>
              >with frm.dataGridVie w1
              > .AutoGenerateCo lumns = true;
              > .DataSource = l_oDataset;
              > .DataMember = this.DataObject Name ;
              >end with
              >
              Sure...it's elegantly implemented like this.
              >
              DataGridView a = frm.dataGridVie w1;
              a.AutoGenerateC olumns = true;
              a.DataSource = I_oDataset;
              a.DataMember = this.DataObject Name;
              >
              ...which is both more concise and more readable.
              I find it somewhat difficult to see the elegance in the
              introduction of a new local variable.

              Arne

              Comment

              • Brian Gideon

                #8
                Re: with statement in C-sharp?

                On Aug 3, 5:03 pm, Arne Vajhøj <a...@vajhoej.d kwrote:
                Brian Gideon wrote:
                Sure...it's elegantly implemented like this.
                >
                DataGridView a = frm.dataGridVie w1;
                a.AutoGenerateC olumns = true;
                a.DataSource = I_oDataset;
                a.DataMember = this.DataObject Name;
                >
                ...which is both more concise and more readable.
                >
                I find it somewhat difficult to see the elegance in the
                introduction of a new local variable.
                >
                Arne
                Elegant, because there is no need for a superfluous keyword. Concise,
                because it requires fewer lines of code. I do see your point about
                the additonal variable, but I don't think that warrants an additional
                keyword.

                Comment

                • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

                  #9
                  Re: with statement in C-sharp?

                  Brian Gideon wrote:
                  On Aug 3, 5:03 pm, Arne Vajhøj <a...@vajhoej.d kwrote:
                  >Brian Gideon wrote:
                  >>Sure...it's elegantly implemented like this.
                  >>DataGridVie w a = frm.dataGridVie w1;
                  >>a.AutoGenerat eColumns = true;
                  >>a.DataSourc e = I_oDataset;
                  >>a.DataMembe r = this.DataObject Name;
                  >>...which is both more concise and more readable.
                  >I find it somewhat difficult to see the elegance in the
                  >introduction of a new local variable.
                  >
                  Elegant, because there is no need for a superfluous keyword. Concise,
                  because it requires fewer lines of code. I do see your point about
                  the additonal variable, but I don't think that warrants an additional
                  keyword.
                  It don't save a statement. It is 1 extra assignment + the 3 original
                  statements instead of 1 with + the 3 original statements.

                  The extra line/lines you are talking about is curly brackets.

                  And if you were to limit the scope of the new local variable
                  similar to the with statement, then you would need those too.

                  Pascal programmer has both options and I think most experienced
                  Pascal programmers choose to use WITH.

                  Arne

                  PS: What will happen if you use your method on a struct instead
                  of a class ?

                  Comment

                  • Jon Skeet [C# MVP]

                    #10
                    Re: with statement in C-sharp?

                    Arne Vajhøj <arne@vajhoej.d kwrote:
                    PS: What will happen if you use your method on a struct instead
                    of a class ?
                    The same as in VB, I believe - as far as I'm aware, VB just introduces
                    a local variable in the compiled code anyway.

                    Note that C# 3 has this in a more limited fashion for object
                    initializers - I can see the value there, but I'm glad C# doesn't have
                    With itself.

                    --
                    Jon Skeet - <skeet@pobox.co m>
                    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                    If replying to the group, please do not mail me too

                    Comment

                    • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

                      #11
                      Re: with statement in C-sharp?

                      Jon Skeet [C# MVP] wrote:
                      Arne Vajhøj <arne@vajhoej.d kwrote:
                      >PS: What will happen if you use your method on a struct instead
                      > of a class ?
                      >
                      The same as in VB, I believe - as far as I'm aware, VB just introduces
                      a local variable in the compiled code anyway.
                      No. At least not in VB.NET (I don't know about VB6).

                      Module Program
                      Public Class Foo
                      Public Dim v As Integer
                      End Class
                      Public Structure Bar
                      Public Dim v As Integer
                      End Structure
                      Sub Main()
                      Dim f As Foo = New Foo
                      f.v = 123
                      Console.WriteLi ne(f.v)
                      With f
                      .v = 456
                      End With
                      Console.WriteLi ne(f.v)
                      Dim ftmp As Foo = f
                      ftmp.v = 789
                      Console.WriteLi ne(f.v)
                      Dim b As Bar = New Bar
                      b.v = 123
                      Console.WriteLi ne(b.v)
                      With b
                      .v = 456
                      End With
                      Console.WriteLi ne(b.v)
                      Dim btmp As Bar = b
                      btmp.v = 789
                      Console.WriteLi ne(b.v)
                      Console.Write(" Press any key to continue . . . ")
                      Console.ReadKey (True)
                      End Sub
                      End Module

                      gives:

                      123
                      456
                      789
                      123
                      456
                      456

                      Arne

                      Comment

                      • Jon Skeet [C# MVP]

                        #12
                        Re: with statement in C-sharp?

                        Arne Vajhøj <arne@vajhoej.d kwrote:
                        Jon Skeet [C# MVP] wrote:
                        Arne Vajhøj <arne@vajhoej.d kwrote:
                        PS: What will happen if you use your method on a struct instead
                        of a class ?
                        The same as in VB, I believe - as far as I'm aware, VB just introduces
                        a local variable in the compiled code anyway.
                        No. At least not in VB.NET (I don't know about VB6).
                        Ah, I see what you mean. Yes, introducing an extra local doesn't work
                        in that case. (One day I'll investigate what it does when you use
                        "with" on something other than a local variable in that kind of
                        situation.)

                        Of course, mutable structs are generally to be avoided anyway...

                        --
                        Jon Skeet - <skeet@pobox.co m>
                        http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                        If replying to the group, please do not mail me too

                        Comment

                        Working...