Connecting Tech Pros Worldwide Forums | Help | Site Map

function object

yysiow
Guest
 
Posts: n/a
#1: Nov 17 '05
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
Guest
 
Posts: n/a
#2: Nov 17 '05

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
http://www.takempis.com
Big things are made up of
lots of little things.

"yysiow" <yysiow@ytlesolutions.com> wrote in message
news:%23DMHGcnSDHA.1552@TK2MSFTNGP10.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]


PJ
Guest
 
Posts: n/a
#3: Nov 17 '05

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:%23yDT8FtSDHA.3796@tk2msftngp13.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@ytlesolutions.com> wrote in message
> news:%23DMHGcnSDHA.1552@TK2MSFTNGP10.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]


Kevin Spencer
Guest
 
Posts: n/a
#4: Nov 17 '05

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
http://www.takempis.com
Complex things are made up of
lots of simple things.

"PJ" <pjwal@hotmail.com> wrote in message
news:eWPHVF7XDHA.1004@TK2MSFTNGP12.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:%23yDT8FtSDHA.3796@tk2msftngp13.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@ytlesolutions.com> wrote in message
> > news:%23DMHGcnSDHA.1552@TK2MSFTNGP10.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]


Closed Thread