473,413 Members | 1,727 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

control type conversion for function parameter

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?
Nov 20 '05 #1
5 1948
"John A Grandy" <johnagrandy-at-yahoo-dot-com> schrieb
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.
--
Armin
Nov 20 '05 #2
if i use ByVal does it actually clone the control ... re-casting it ?

or is there some other "special magic" involved ?
"Armin Zingler" <az*******@freenet.de> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
"John A Grandy" <johnagrandy-at-yahoo-dot-com> schrieb
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.
--
Armin

Nov 20 '05 #3
"John A Grandy" <johnagrandy-at-yahoo.com> schrieb
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.
--
Armin

Nov 20 '05 #4
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?

"Armin Zingler" <az*******@freenet.de> wrote in message
news:Ou**************@TK2MSFTNGP10.phx.gbl...
"John A Grandy" <johnagrandy-at-yahoo.com> schrieb
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.
--
Armin

Nov 20 '05 #5
"John A Grandy" <johnagrandy-at-yahoo.com> schrieb
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.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: ann | last post by:
Does somebody know why I get a blank string in strA? Last time post the wrong code. " Option Strict On Option Explicit On Public Class Cast Private Sub FuncA()
2
by: seia0106 | last post by:
Hello I am writing a function to read a binary file. Here is a part of code #include <fstream> .. .. BYTE *pData; long lDataLen; pms->GetPointer(&pData); lDataLen = pms->GetSize(); // Read...
16
by: frs | last post by:
See example below: Why does the output of 'a' work and the output of 'b' fails to compile? Is there a way to write class 'something' so that 'b' converts correctly by default? (include iostream,...
4
by: Mark Oliver | last post by:
Hi, I want to put a type conversion in my class, but I don't want the conversion to be usable in a passed parameter because it makes no sense. class cData { string s; public cData(string s)...
3
by: Christopher Crooker | last post by:
We had a function with the signature: Function(Object *, DateTime, DateTime, EnumType) and then changed the class some and there was a 4 parameter function with the signature: ...
7
by: Alphonse Giambrone | last post by:
How can I convert a string to a different type based on another string or other variable? For instance, instead of Dim i as Integer i = ctype("1000", Integer) I would like to do
7
by: Rich Milburn [MVP] | last post by:
Ok I am not a programmer, I copied some code and very painfully got it working in VB6. I can adjust the volume with waveOutSetVolume on winmm.dll. But I could not seem to be able to figure out how...
318
by: jacob navia | last post by:
Rcently I posted code in this group, to help a user that asked to know how he could find out the size of a block allocated with malloc. As always when I post something, the same group of people...
5
by: Fei Liu | last post by:
Hello, I just hit a strange problem regarding SFINAE. The following code causes compile error (void cannot be array element type), I thought SFINA should match test(...) version instead and not...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.