469,922 Members | 2,190 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,922 developers. It's quick & easy.

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 4020
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Suzanne Vogel | last post: by
4 posts views Thread by Jacob Jensen | last post: by
23 posts views Thread by René Nordby | last post: by
6 posts views Thread by crook | last post: by
15 posts views Thread by shuisheng | last post: by
2 posts views Thread by bryars | last post: by
reply views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.