473,385 Members | 1,730 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,385 software developers and data experts.

Automatic type casting?

How does automatic type casting happen in vb.net? I notice that
databinder.eval "uses reflectoin" to find out the type it's dealing with.
Does vb.net do the same thing behind the scenes when an invisible cast is
made? Is there any reason why one would use databinder.eval while in VB.NET?
I can see why one might use it in C# so as to avoid specifying the type for
the cast but since this is not necessary in VB.NET, I'm not sure I follow.

Thanks...

-Ben
Feb 6 '06 #1
7 4196
Hi Ben,

It is different thing for type casting in databinder.eval and in VB.NET
language. Yes, as you can see the databinder.eval uses reflection to do
the casting. While the VB.NET language automatically cast is a technique
inside the VB.NET compiler, which is build-in for the language.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Feb 6 '06 #2
Hi Kevin,

Not sure I follow. If the vb.net conversion is a compile time conversion,
how is it possible to, in vb.net, omit the databinder.eval and simply use
container.dataitem without a cast? At compile time, how would it make a
casting decision?

-Ben

"Kevin Yu [MSFT]" wrote:
Hi Ben,

It is different thing for type casting in databinder.eval and in VB.NET
language. Yes, as you can see the databinder.eval uses reflection to do
the casting. While the VB.NET language automatically cast is a technique
inside the VB.NET compiler, which is build-in for the language.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Feb 6 '06 #3
Hi Ben,

The databinder.eval is required in data binding and cannot be omitted
despite the language change. The compile time type casting is build-in in
the compiler and we have no idea of how it was implemented.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Feb 7 '06 #4
Hi Kevin,

I'm not sure this is the case. In my ASP.NET (VB.NET) pages, I have code
such as the following:

<ItemTemplate>
<tr>
<td>
<asp:Label ID="themessage" runat="server" Text='<%#
Container.Dataitem("MsgSubject") %>' /></td>
</tr>
</ItemTemplate>
or

<td>
<%# Container.Dataitem("RunDistance")%>
</td>

Note the absence of Databinder.Eval. This is running on ASP.NET 2.0 without
a problem. It also ran on 1.1 without a problem.

I was under the impression that this could only be the case on VB.NET and
not C#. Could you clarify your last post please?

-Ben

"Kevin Yu [MSFT]" wrote:
Hi Ben,

The databinder.eval is required in data binding and cannot be omitted
despite the language change. The compile time type casting is build-in in
the compiler and we have no idea of how it was implemented.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Feb 7 '06 #5
Hi Ben,

The Eval method returns a string from the source column, which is the best
way to use databinding in ASP.NET. However, this can run without problem
because of the VB's automatic data type convertion, Actually, it's also
getting a string from the column. It's better to use Eval here for
compatibility issue. You can check for a best practice in the following
link:

http://msdn2.microsoft.com/en-us/library/ms178366.aspx

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Feb 8 '06 #6
Hi Kevin,
The Eval method returns a string from the source column, which is the best
way to use databinding in ASP.NET. However, this can run without problem
because of the VB's automatic data type convertion,
This isn't exactly' whawt I meant. I was referring to omitting the eval
method entirely, which also works. You were saying that this can run without
a problem because of VB.NET's automatic type conversion. is this the compile
time conversion you mentioned? Or is this late binding? And in either case,
why would one ever use datavinder.eval then? To demonstrate, i created a
simple webpage:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb"
Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<%# Container.Dataitem %>
</ItemTemplate>

</asp:DataList>
</div>
</form>
</body>
</html>
Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Dim myarray() As Integer = {2, 4, 5, 3}
DataList1.DataSource = myarray
DataList1.DataBind()
End Sub
End Class

This runs without problems. Notice that it's pulling an integer and not a
string, and yet the output is correct. So does VB.NET provide late binding
behind the scenes? Or just compile time conversion? And if only compile time,
how am I not seeing an exception?

-Ben
"Kevin Yu [MSFT]" wrote:
Hi Ben,

The Eval method returns a string from the source column, which is the best
way to use databinding in ASP.NET. However, this can run without problem
because of the VB's automatic data type convertion, Actually, it's also
getting a string from the column. It's better to use Eval here for
compatibility issue. You can check for a best practice in the following
link:

http://msdn2.microsoft.com/en-us/library/ms178366.aspx

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Feb 8 '06 #7
Hi

Based on my test, the code below works in a C# 2.0 asp.net page.
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int[] Nums= new int [5]{1,2,3,4,5};
DataList1.DataSource = Nums;
DataList1.DataBind();
}
}

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<%# Container.DataItem %>
</ItemTemplate>
</asp:DataList></div>
</form>
</body>
</html>

I think this is a binding issue. The DataBinding mechanism will help to
call the Int.ToString() on the integer to format it as a string.
This is not a language feature. but a databinding desing.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Feb 9 '06 #8

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

Similar topics

5
by: Suzanne Vogel | last post by:
** Isn't the 'static_cast' operator the same as traditional type casting? ie, Aren't the following ways of getting b1, b2 the same? // 'Derived' is derived from 'Base' Derived* d = new...
4
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...
6
by: Jim Bancroft | last post by:
Hi everyone, I'm having some trouble with the code below. I receive a compile-time error on the second line saying "; expected": private static void myTestFunction(long myLong) { ...
10
by: Bob | last post by:
This has been bugging me for a while now. GetType isn't availble for variables decalred as interface types, I have to DirectCast(somevariable, Object). In example: Sub SomeSub(ByVal...
23
by: René Nordby | last post by:
Hi there, Is there anyone that knows how to do the following? I have a class A and a class B, that 100% inherits from class A (this means that I don't have other code in class B, than...
6
by: crook | last post by:
I have code below and it works properly but when I'm compiling it with "--pedantic" flag, GCC(3.4.2) shows such warning: "ISO C forbids casting nonscalar to the same type". How can I change this...
15
by: shuisheng | last post by:
Dear All, Assume I have a class named Obj. class Obj { }; And a class named Shape which is derived from Obj. class Shape: public Obj
4
by: beena | last post by:
All, I'm new to the concept of automatic storage... I'm looking at the database setup by a vendor. I see few tablespaces showing up with automatic storage - Yes. Tablespace ID ...
2
by: bryars | last post by:
I want to write some SQL which results in an automatic conversion of a datetime to a string in a format suitable for the Language of the connection (either by explicitly setting the Language in the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.