function object

  • Thread starter Thread starter yysiow
  • Start date Start date
Y

yysiow

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.
 
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.
 
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.
 
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.
 
Back
Top