List Info

Thread: ByRef question




ByRef question
user name
2006-11-22 00:59:25
AddressOf is indeed only for procedures (or possibly for delagates?)  Used to good effect, for example, when adding event handlers dynamically with AddHandler (like when you want an array or collection of withevent objects, and want  single sub to handle the event from all the objects in the collection).
 
One thing that may get overlooked here: ; ByVal and ByRef are all well and good, but I think that for reference types the distinction gets a little blurry.
 
A reference is like a pointer, in that it is actually the memory address that refers to the 1st byte of the object.... so, while IRRC that there IS SOME difference between referring to referrence types byVal or byRef, I don;t think that it matters that much.... a ByVal argument sends the value of the memory address... a ByRef argument sends... well... I'm not sure, but I guess it must be a reference to the memory address?  They seem to work pretty similarly for refernce types.
 
I think you only need to change to byVal.  (Or give up on Option Strict) -- I tried out the following and it worked perfectly as is.  It also works fine with placecontrol accepting the ctrl argument ByRef with Option Strict Off:
(draw in a textbox on the opening default form as txt1.  Add a button called button1 to a new project.)
 
Option Strict On
' Windows Form code

Private Sub placecontrol(ByVal ctrl As Control, ByVal Loc As Point)

    ctrl.Location = Loc

End Sub

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

    ' place the textbox from design mode

    placecontrol(txt1, New Point(15, 35))

    ' dynamically create one and place it.

    Dim txt2 As New TextBox

    txt2.Parent = Me

    txt2.Size = New Size(100, 10)

    txt2.Visible = True

    txt2.BackColor = Color.Aqua

    placecontrol(txt2, New Point(5, 5))

End Sub



From: helpwithvb@yahoogroups.com [mailto:helpwithvb@yahoogroups.com] On Behalf Of Steve
Sent: Tuesday, November 21, 2006 7:11 PM
To: helpwithvb@yahoogroups.com
Subject: [helpwithvb] ByRef question

I want pass a control to a procedure that will position the control on
the form. I think the control needs to be passed ByRef. But when I
declare the control argument to be ByRef, I get an error on the
calling end that says "Strict Option will not allow the narrowing..."

First, am I correct that I must pass ByRef? If so, what do I have to
do when I call. The "AddressOf" operator says its only for procedures?

Steve

__._,_.___ __,_._,___
[1]

about | contact  Other archives ( Real Estate discussion Medical topics )