473,626 Members | 3,340 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Casting?

Hello: I am confused about casting...

Option Strict = ON

Example1:
---------------
When I say something like this...

Dim lo_Foo = New Foo
Dim lo_Obj as Object = lo_Foo

Works Fine!
Example2:
---------------
When I say something like this...

Dim lo_Foo = New Foo

OutPutObj(lo_Fo o)

Public Sub OutPutObj(ao_Ob j as Object)
...
End Sub

Compile Error: "Option Strict ON disallows implicit conversion from
System.Object to type Foo"

Isn't there an implicit conversion in the first example when the variable of
type object is set to the instance of Foo???

Thank you,
Shannon Richards
BBA, AIT, MCP
Nov 21 '05 #1
14 1651
"Shannon Richards" <sr**********@h otmail.com> schrieb:
Hello: I am confused about casting...

Option Strict = ON

Example1:
---------------
When I say something like this...

Dim lo_Foo = New Foo
Dim lo_Obj as Object = lo_Foo

Works Fine!
Example2:
---------------
When I say something like this...

Dim lo_Foo = New Foo

OutPutObj(lo_Fo o)

Public Sub OutPutObj(ao_Ob j as Object)
...
End Sub

Compile Error: "Option Strict ON disallows implicit conversion from
System.Object to type Foo"


Where does it say that? It doesn't say that for the code you posted.

The message will be shown when casting from a base type to one of its
derived type, but not when "casting" from the derived type to the base type.
That's why the cast to 'Object' will always work.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 21 '05 #2
All types of sedan automobiles are cars, but not all cars are sedan
automobiles.

You can cast a sedan as a car, but you can't cast a car as a sedan.

Get it?

"Shannon Richards" <sr**********@h otmail.com> wrote in message
news:uU******** *****@TK2MSFTNG P10.phx.gbl...
Hello: I am confused about casting...

Option Strict = ON

Example1:
---------------
When I say something like this...

Dim lo_Foo = New Foo
Dim lo_Obj as Object = lo_Foo

Works Fine!
Example2:
---------------
When I say something like this...

Dim lo_Foo = New Foo

OutPutObj(lo_Fo o)

Public Sub OutPutObj(ao_Ob j as Object)
...
End Sub

Compile Error: "Option Strict ON disallows implicit conversion from
System.Object to type Foo"

Isn't there an implicit conversion in the first example when the variable
of type object is set to the instance of Foo???

Thank you,
Shannon Richards
BBA, AIT, MCP

Nov 21 '05 #3
Hi Shannon,

Option Strict On doesn't allow late-binding. For some reason I believe the
compiler considers your first example either early-binding or allowed
late-binding and your second example illegal late-binding with Option Strict
On. I think that is what is going on but I'm not totally positive. My
point is that maybe this isn't a casting problem but a binding problem.
Some research on binding could give you a definitive answer. Good luck!
Ken.

--
Ken Dopierala Jr.
For great ASP.Net web hosting try:
http://www.webhost4life.com/default.asp?refid=Spinlight
If you sign up under me and need help, email me.

"Shannon Richards" <sr**********@h otmail.com> wrote in message
news:uU******** *****@TK2MSFTNG P10.phx.gbl...
Hello: I am confused about casting...

Option Strict = ON

Example1:
---------------
When I say something like this...

Dim lo_Foo = New Foo
Dim lo_Obj as Object = lo_Foo

Works Fine!
Example2:
---------------
When I say something like this...

Dim lo_Foo = New Foo

OutPutObj(lo_Fo o)

Public Sub OutPutObj(ao_Ob j as Object)
...
End Sub

Compile Error: "Option Strict ON disallows implicit conversion from
System.Object to type Foo"

Isn't there an implicit conversion in the first example when the variable of type object is set to the instance of Foo???

Thank you,
Shannon Richards
BBA, AIT, MCP

Nov 21 '05 #4
Hi Shannon,

Option Strict On doesn't allow late-binding. For some reason I believe the
compiler considers your first example either early-binding or allowed
late-binding and your second example illegal late-binding with Option Strict
On. I think that is what is going on but I'm not totally positive. My
point is that maybe this isn't a casting problem but a binding problem.
Some research on binding could give you a definitive answer. Good luck!
Ken.

--
Ken Dopierala Jr.
For great ASP.Net web hosting try:
http://www.webhost4life.com/default.asp?refid=Spinlight
If you sign up under me and need help, email me.

"Shannon Richards" <sr**********@h otmail.com> wrote in message
news:uU******** *****@TK2MSFTNG P10.phx.gbl...
Hello: I am confused about casting...

Option Strict = ON

Example1:
---------------
When I say something like this...

Dim lo_Foo = New Foo
Dim lo_Obj as Object = lo_Foo

Works Fine!
Example2:
---------------
When I say something like this...

Dim lo_Foo = New Foo

OutPutObj(lo_Fo o)

Public Sub OutPutObj(ao_Ob j as Object)
...
End Sub

Compile Error: "Option Strict ON disallows implicit conversion from
System.Object to type Foo"

Isn't there an implicit conversion in the first example when the variable of type object is set to the instance of Foo???

Thank you,
Shannon Richards
BBA, AIT, MCP

Nov 21 '05 #5
Shannon,

What is IncomeGateway? A Collection or ArrayList, or? Is it strongly type to only contain Income objects?

Same question for List?
--
Mike

Mike McIntyre
Visual Basic MVP
www.getdotnetcode.com
"Shannon Richards" <sr**********@h otmail.com> wrote in message news:OS******** ******@TK2MSFTN GP11.phx.gbl...
Here's what's really happening:

lo_Gateway.Inse rt(ByRef ao_Object As Object) - takes an argument of type object.

Since all reference types derive from object in .Net why is the compiler giving this error? Income is an Object...


Nov 21 '05 #6
Shannon,

What is IncomeGateway? A Collection or ArrayList, or? Is it strongly type to only contain Income objects?

Same question for List?
--
Mike

Mike McIntyre
Visual Basic MVP
www.getdotnetcode.com
"Shannon Richards" <sr**********@h otmail.com> wrote in message news:OS******** ******@TK2MSFTN GP11.phx.gbl...
Here's what's really happening:

lo_Gateway.Inse rt(ByRef ao_Object As Object) - takes an argument of type object.

Since all reference types derive from object in .Net why is the compiler giving this error? Income is an Object...


Nov 21 '05 #7
Shannon,
In addition to the other comments (especially Nick's):

Insert is using ByRef, Do you really need ByRef here? Probably not.

ByVal & ByRef Parameters are independent of Reference & Value Types. All
parameters in VB.NET by default are passed ByVal, you should only pass a
parameter ByRef when you have to, which is when you need to modify the
callers variable.

A Reference Type is an object that exists on the heap. If I have a variable
that is a reference type and assign the variable to another variable. Both
variables will be pointing to the same object on the heap.

Dim x As Person
x = New Person()
Dim y As Person
y = x

Both x & y are the exact same Person object on the heap.

A Value Type does not live on the Heap. If I have a value type variable and
I assign it to another variable, a copy of the value is made.

Dim x As Integer
x = 100
Dim y As Integer
y = x

Although both x & y have the value 100, they are physically different values
as a copy was made.

Now when you pass a variable to a ByVal parameter a copy of the variable is
made. So for a Reference Type a copy of the reference is made, which means
there is still only one object on the heap & two references to that object.
For a Value Type a copy of the value is made.

When you pass a variable to a ByRef parameter a reference to that variable
is made. So for a Reference Type you have a reference to a reference to the
object, for a Value Type you have a reference to the value.

Remember ByVal & ByRef are how parameters are passed. Reference & Value
Types are how quantities are stored.

The following site also does a good job of explaining Reference & Value
types & ByRef & ByVal parameters.

http://www.yoda.arachsys.com/csharp/parameters.html
Hope this helps
Jay

"Shannon Richards" <sr**********@h otmail.com> wrote in message
news:OS******** ******@TK2MSFTN GP11.phx.gbl...
Here's what's really happening:

lo_Gateway.Inse rt(ByRef ao_Object As Object) - takes an argument of type
object.

Since all reference types derive from object in .Net why is the compiler
giving this error? Income is an Object...



Nov 21 '05 #8
Shannon,
In addition to the other comments (especially Nick's):

Insert is using ByRef, Do you really need ByRef here? Probably not.

ByVal & ByRef Parameters are independent of Reference & Value Types. All
parameters in VB.NET by default are passed ByVal, you should only pass a
parameter ByRef when you have to, which is when you need to modify the
callers variable.

A Reference Type is an object that exists on the heap. If I have a variable
that is a reference type and assign the variable to another variable. Both
variables will be pointing to the same object on the heap.

Dim x As Person
x = New Person()
Dim y As Person
y = x

Both x & y are the exact same Person object on the heap.

A Value Type does not live on the Heap. If I have a value type variable and
I assign it to another variable, a copy of the value is made.

Dim x As Integer
x = 100
Dim y As Integer
y = x

Although both x & y have the value 100, they are physically different values
as a copy was made.

Now when you pass a variable to a ByVal parameter a copy of the variable is
made. So for a Reference Type a copy of the reference is made, which means
there is still only one object on the heap & two references to that object.
For a Value Type a copy of the value is made.

When you pass a variable to a ByRef parameter a reference to that variable
is made. So for a Reference Type you have a reference to a reference to the
object, for a Value Type you have a reference to the value.

Remember ByVal & ByRef are how parameters are passed. Reference & Value
Types are how quantities are stored.

The following site also does a good job of explaining Reference & Value
types & ByRef & ByVal parameters.

http://www.yoda.arachsys.com/csharp/parameters.html
Hope this helps
Jay

"Shannon Richards" <sr**********@h otmail.com> wrote in message
news:OS******** ******@TK2MSFTN GP11.phx.gbl...
Here's what's really happening:

lo_Gateway.Inse rt(ByRef ao_Object As Object) - takes an argument of type
object.

Since all reference types derive from object in .Net why is the compiler
giving this error? Income is an Object...



Nov 21 '05 #9
Hello: Thank you all for your fantastic comments. I understand more about
the issue now.

One additional question...
When dealing with reference types as parameters. Is it more efficient to
code the parameter as ByRef since ByVal will end up creating another pointer
to the reference type being passed into the method?

Thank you,
Shannon
"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:eE******** ******@tk2msftn gp13.phx.gbl...
Shannon,
In addition to the other comments (especially Nick's):

Insert is using ByRef, Do you really need ByRef here? Probably not.

ByVal & ByRef Parameters are independent of Reference & Value Types. All
parameters in VB.NET by default are passed ByVal, you should only pass a
parameter ByRef when you have to, which is when you need to modify the
callers variable.

A Reference Type is an object that exists on the heap. If I have a
variable
that is a reference type and assign the variable to another variable. Both
variables will be pointing to the same object on the heap.

Dim x As Person
x = New Person()
Dim y As Person
y = x

Both x & y are the exact same Person object on the heap.

A Value Type does not live on the Heap. If I have a value type variable
and
I assign it to another variable, a copy of the value is made.

Dim x As Integer
x = 100
Dim y As Integer
y = x

Although both x & y have the value 100, they are physically different
values
as a copy was made.

Now when you pass a variable to a ByVal parameter a copy of the variable
is
made. So for a Reference Type a copy of the reference is made, which means
there is still only one object on the heap & two references to that
object.
For a Value Type a copy of the value is made.

When you pass a variable to a ByRef parameter a reference to that variable
is made. So for a Reference Type you have a reference to a reference to
the
object, for a Value Type you have a reference to the value.

Remember ByVal & ByRef are how parameters are passed. Reference & Value
Types are how quantities are stored.

The following site also does a good job of explaining Reference & Value
types & ByRef & ByVal parameters.

http://www.yoda.arachsys.com/csharp/parameters.html
Hope this helps
Jay

"Shannon Richards" <sr**********@h otmail.com> wrote in message
news:OS******** ******@TK2MSFTN GP11.phx.gbl...
Here's what's really happening:

lo_Gateway.Inse rt(ByRef ao_Object As Object) - takes an argument of type
object.

Since all reference types derive from object in .Net why is the compiler
giving this error? Income is an Object...


Nov 21 '05 #10

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

Similar topics

4
3458
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A good way to do this is by example. So I will give an example and please tell me what you think: I have a base class A with a virtual destructor, and a class B that is it inherits publicly from A and defines som extra stuff.
231
23104
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought (perhaps mistakenly) that the purpose of a void pointer was to cast into a legitimate date type. Is this wrong? Why, and what is considered to be correct form?
35
2673
by: ytrama | last post by:
Hi, I have read in one of old posting that don't cast of pointer which is returned by the malloc. I would like to know the reason. Thanks in advance, YTR
7
3662
by: yufufi | last post by:
lets say we have a 'shape' class which doesn't implement IComparable interface.. compiler doesn't give you error for the lines below.. shape b= new shape(); IComparable h; h=(IComparable)b; but it complains for the following lines
7
30769
by: Jim Bancroft | last post by:
Hi everyone, A basic one here, I think. I haven't found the pattern yet, but sometimes when I cast a variable to another type using the "C" style cast operator the compiler refuses to play along. It says it's an invalid cast. However, if I use the Convert.ToInt32() method (for example) things will work. At least, that's how it appears to me. Could someone explain when to use old-style parenthesized casts vs. the Convert() methods?
2
2162
by: Enrique Bustamante | last post by:
Casting arrays that works on watch and command window but not in code. My application is casting arrays in a way it should work. To test if I was doing something invalid, I wrote a test code that has similar structure of the classes in my application. The test worked fine, the casting I want to do must work. I compared the structure of the test with the application to ensure they where similar (inheriting twice, base class implements...
7
13671
by: S. Lorétan | last post by:
Hi guys, Sorry for this stupid question, but I don't know why it isn't working. Here is my (example) code: namespace Test { class A { public string Label1; }
17
2213
by: sophia.agnes | last post by:
Hi , I was going through peter van der linden's book Expert C programming, in this book there is a section named "How and why to cast" the author then says as follows (float) 3 - it's a type conversion and the actual bits change. if you say (float) 3.0 it is a type disambiguation,and the compiler can plant the correct bits in the first place.some people say that
9
3457
by: Taras_96 | last post by:
Hi everyone, I was experimenting with static_cast and reinterpret cast #include <iostream> struct A1 { int a; }; struct A2 { double d; }; struct B : public A1, A2
101
4282
by: Tinkertim | last post by:
Hi, I have often wondered if casting the return value of malloc() (or friends) actually helps anything, recent threads here suggest that it does not .. so I hope to find out. For instance : char *tmp = NULL;
0
8266
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8705
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8638
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8365
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8505
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7196
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6125
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4092
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1811
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.