function object

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

    #1

    function object

    hi All

    in vb function can pass object, like

    Function test(ByVal txt As TextBox)

    txt.Text = "text"

    End Function

    if i want to do this function using C#.
    how can i do?
    thanks.


  • Kevin Spencer

    #2
    Re: function object

    In .Net, EVERYTHING is an object. So, of course, you can pass one as a
    parameter. However, if you want to make a change to the object passed, you
    should pass it by Reference. In .Net, you would do it like so:

    Function test(ByRef txt As TextBox)

    txt.Text = "text"

    End Function

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    All hotels in Benidorm. The best selection of Benidorm hotels with reviews and maps. Book in advance and save.

    Big things are made up of
    lots of little things.

    "yysiow" <yysiow@ytlesol utions.com> wrote in message
    news:%23DMHGcnS DHA.1552@TK2MSF TNGP10.phx.gbl. ..[color=blue]
    > hi All
    >
    > in vb function can pass object, like
    >
    > Function test(ByVal txt As TextBox)
    >
    > txt.Text = "text"
    >
    > End Function
    >
    > if i want to do this function using C#.
    > how can i do?
    > thanks.
    >
    >[/color]


    Comment

    • PJ

      #3
      Re: function object

      That's unnecesssary and incorrect. The byval and byref refer to the pointer
      of the object, not the object itself. Attributes of the TextBox instance
      would still be modified outside of the function they were modified in.

      However, for value ( base ) types such as int32 you would need to pass it
      byref if you want to modify the instance. There is rarely a need for that
      as proper scoping eleminates this need.


      "Kevin Spencer" <kevin@takempis .com> wrote in message
      news:%23yDT8FtS DHA.3796@tk2msf tngp13.phx.gbl. ..[color=blue]
      > In .Net, EVERYTHING is an object. So, of course, you can pass one as a
      > parameter. However, if you want to make a change to the object passed, you
      > should pass it by Reference. In .Net, you would do it like so:
      >
      > Function test(ByRef txt As TextBox)
      >
      > txt.Text = "text"
      >
      > End Function
      >
      > --
      > HTH,
      >
      > Kevin Spencer
      > Microsoft MVP
      > .Net Developer
      > http://www.takempis.com
      > Big things are made up of
      > lots of little things.
      >
      > "yysiow" <yysiow@ytlesol utions.com> wrote in message
      > news:%23DMHGcnS DHA.1552@TK2MSF TNGP10.phx.gbl. ..[color=green]
      > > hi All
      > >
      > > in vb function can pass object, like
      > >
      > > Function test(ByVal txt As TextBox)
      > >
      > > txt.Text = "text"
      > >
      > > End Function
      > >
      > > if i want to do this function using C#.
      > > how can i do?
      > > thanks.
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Kevin Spencer

        #4
        Re: function object

        True, but that sort of information goes over the heads of certain types of
        developers, for example, VB developers that are migrating to VB.Net. As you
        stated, the need is rare, but not non-existent. For example, when you pass
        an object without passing it ByRef, you can not change the assignment of the
        object variable, only its' properties. Understanding the difference between
        reference types and value types is also a more advanced topic. So I
        simplified it. Passing an object ByRef hurts nothing.

        --
        HTH,

        Kevin Spencer
        Microsoft MVP
        ..Net Developer
        All hotels in Benidorm. The best selection of Benidorm hotels with reviews and maps. Book in advance and save.

        Complex things are made up of
        lots of simple things.

        "PJ" <pjwal@hotmail. com> wrote in message
        news:eWPHVF7XDH A.1004@TK2MSFTN GP12.phx.gbl...[color=blue]
        > That's unnecesssary and incorrect. The byval and byref refer to the[/color]
        pointer[color=blue]
        > of the object, not the object itself. Attributes of the TextBox instance
        > would still be modified outside of the function they were modified in.
        >
        > However, for value ( base ) types such as int32 you would need to pass it
        > byref if you want to modify the instance. There is rarely a need for that
        > as proper scoping eleminates this need.
        >
        >
        > "Kevin Spencer" <kevin@takempis .com> wrote in message
        > news:%23yDT8FtS DHA.3796@tk2msf tngp13.phx.gbl. ..[color=green]
        > > In .Net, EVERYTHING is an object. So, of course, you can pass one as a
        > > parameter. However, if you want to make a change to the object passed,[/color][/color]
        you[color=blue][color=green]
        > > should pass it by Reference. In .Net, you would do it like so:
        > >
        > > Function test(ByRef txt As TextBox)
        > >
        > > txt.Text = "text"
        > >
        > > End Function
        > >
        > > --
        > > HTH,
        > >
        > > Kevin Spencer
        > > Microsoft MVP
        > > .Net Developer
        > > http://www.takempis.com
        > > Big things are made up of
        > > lots of little things.
        > >
        > > "yysiow" <yysiow@ytlesol utions.com> wrote in message
        > > news:%23DMHGcnS DHA.1552@TK2MSF TNGP10.phx.gbl. ..[color=darkred]
        > > > hi All
        > > >
        > > > in vb function can pass object, like
        > > >
        > > > Function test(ByVal txt As TextBox)
        > > >
        > > > txt.Text = "text"
        > > >
        > > > End Function
        > > >
        > > > if i want to do this function using C#.
        > > > how can i do?
        > > > thanks.
        > > >
        > > >[/color]
        > >
        > >[/color]
        >
        >[/color]


        Comment

        Working...