control type conversion for function parameter

J

John A Grandy

I'm using Option Strict -- because all the gurus say i should

I have a function in a class

MyFunction(ByRef c As Control)


In the calling page I have (for example)

Protected MyHTMLControl As HtmlButton
Protected MyWebControl as Button

If I write the code

MyFunction(MyWebControl)

I get the compile-time error

" Option Strict On disallows implicit conversions from
'System.Web.UI.Control' to 'System.Web.UI.WebControls.Button' "

The code

MyFunction(MyHTMLControl)

gives a similar type conversion error.

Short of writing two versions of MyFunction, how do I get around this
problem?
 
A

Armin Zingler

John A Grandy said:
I'm using Option Strict -- because all the gurus say i should

I have a function in a class

MyFunction(ByRef c As Control)

In the calling page I have (for example)

Protected MyHTMLControl As HtmlButton
Protected MyWebControl as Button

If I write the code

MyFunction(MyWebControl)

I get the compile-time error

" Option Strict On disallows implicit conversions from
'System.Web.UI.Control' to 'System.Web.UI.WebControls.Button' "

The code

MyFunction(MyHTMLControl)

gives a similar type conversion error.

Short of writing two versions of MyFunction, how do I get around
this problem?


If you don't want to change the passed variables, declare the argument
ByVal, not ByRef.


You get the error because otherwise, when writing
c = MyTextbox
inside the sub, it would change the passed variable and make it reference a
Textbox. This is obviously not possible because the type of the passed
variable is Button or HtmlButton, not Textbox.
 
J

John A Grandy

if i use ByVal does it actually clone the control ... re-casting it ?

or is there some other "special magic" involved ?
 
A

Armin Zingler

John A Grandy said:
if i use ByVal does it actually clone the control ... re-casting it
?

or is there some other "special magic" involved ?

No. Passing a variable ByVal means passing a copy of the content of the
variable. The content is the reference (4 Bytes) to the control. It is the
reference because System.Windows.Forms.Control is a reference type. All
classes are reference types.
 
J

John A Grandy

ok, so ByRef causes a reference to a reference variable to come through...

and ByVal causes a copy of the value of a reference variable to come through
....

but why is that necessary for the function to implement that casting
specified in the parameter's declaration?
 
A

Armin Zingler

John A Grandy said:
ok, so ByRef causes a reference to a reference variable to come
through...

and ByVal causes a copy of the value of a reference variable to come
through ...

but why is that necessary for the function to implement that
casting specified in the parameter's declaration?

I'm not sure what you mean. Which casting?

What I meant was:

dim c as control
dim b as button

b = c

The assignment is invalid because not every control is a button. Declaring
the argument ByRef is almost the same.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top