如何将此代码从 VB.NET 转换为 C#? (翻译人员不工作)
我在将此代码从 VB.NET 转换为 C# 时遇到问题。这段代码应该从数据库中的列的每个单元格中获取一个值(我们称之为column1,它的数据类型是日期时间,所以,格式如下:12/19/2011 7:42:30 PM),并查找 gridview 控件中每行的 Datetime.Now 和 column1 的值之间的时间跨度。有人给了我这段在 VB.NET 中完美运行的代码:
<asp:TemplateField HeaderText="TimeSpan">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# TimeSpan(IIf(IsDBNull(Eval("column1")), DateTime.Now,Eval("column1"))) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
对于代码隐藏:
Protected Function TimeSpan(ByVal Duration As DateTime) As TimeSpan
Dim date1 As DateTime = Duration
Dim date2 As DateTime = DateTime.Now
Dim ts As TimeSpan = (date2 - date1)
Return ts
End Function
在 VB.NET 中它可以运行,但是当我尝试将其转换为 C# 并运行我的应用程序时,我收到以下两个错误:
-The '_Default.TimeSpan(System.DateTime)' 的最佳重载方法匹配有一些无效参数 -参数 1:无法从“对象”转换为“System.DateTime”
有人可以帮我解决这个问题吗?适用于代码的翻译?或者甚至用另一种方式做我想做的事? 谢谢
编辑:这是代码(在 C# 中,我使用翻译器获得),这实际上是给我前面提到的异常的代码:
<asp:TemplateField HeaderText="TimeSpan">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# TimeSpan((Information.IsDBNull(Eval("column1")) ? DateTime.Now : Eval("column1")))
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
对于后面的代码:
protected TimeSpan TimeSpan(DateTime Duration)
{
DateTime date1 = Duration;
DateTime date2 = DateTime.Now;
TimeSpan ts = (date2 - date1);
return ts;
}
I'm having problems translating this code to C# from VB.NET. This code is supposed to take a value from each cell of a column in a database (let's call it column1, it's data type is datetime, so, the format is like this: 12/19/2011 7:42:30 PM), and find the timespan between the Datetime.Now and that value of column1 for each row in a gridview control. Some guy gave me this code that works perfectly in VB.NET:
<asp:TemplateField HeaderText="TimeSpan">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# TimeSpan(IIf(IsDBNull(Eval("column1")), DateTime.Now,Eval("column1"))) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
And for the codebehind:
Protected Function TimeSpan(ByVal Duration As DateTime) As TimeSpan
Dim date1 As DateTime = Duration
Dim date2 As DateTime = DateTime.Now
Dim ts As TimeSpan = (date2 - date1)
Return ts
End Function
And in VB.NET it works, but when I try to translate it to C#, and run my application, I get these two errors:
-The best overloaded method match for '_Default.TimeSpan(System.DateTime)' has some invalid argument
-Argument 1: cannot convert from 'object' to 'System.DateTime'
Can someone please help me with this? A translation that works for the code? Or even another way of doing what I want to do?
Thanks
Edited: This is the code (in C#, which I get using translators), which is actually the code that gives me the exceptions that I mentioned before:
<asp:TemplateField HeaderText="TimeSpan">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# TimeSpan((Information.IsDBNull(Eval("column1")) ? DateTime.Now : Eval("column1")))
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
And for the code behind:
protected TimeSpan TimeSpan(DateTime Duration)
{
DateTime date1 = Duration;
DateTime date2 = DateTime.Now;
TimeSpan ts = (date2 - date1);
return ts;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为问题是你调用你的方法 TimeSpan。尝试将其命名为其他名称(例如 ToTimeSpan)。
I think the problem is you call your method TimeSpan. Try calling it something else (such as ToTimeSpan).
您似乎对调用代码有问题,而不是函数本身。
该错误表明您尝试将
object
类型的值传递给函数TimeSpan
。但该函数的唯一参数是 DateTime 类型。当您显示调用函数
TimeSpan
的代码时,我可以为您提供更改建议。但正如其他人所建议的,函数和参数的命名非常具有误导性。
编辑
好的,现在我看到了您的调用代码。您必须将
Eval("column1")
的值转换为 DateTime:You seem to have a problem with the calling code not woth the function itself.
The error says that you try to pass a value of type
object
to your functionTimeSpan
. But the only parameter of that function is of typeDateTime
.When you show the code calling the function
TimeSpan
i can give you advice what to change.But as others suggested, the naming of your function and the parameter is very missleading.
EDIT
Ok, now i see your calling code. You have to cast the value of
Eval("column1")
to DateTime:我认为问题可能是 Eval("column1") 返回一个对象,而您的 TimeSpan 方法不接受对象,它接受 DateTimes。转换为日期时间,我认为它可能会开始工作。
IE
I think the problem is probably that Eval("column1") is returning an objet and your TimeSpan method doesn't accept objects, it accepts DateTimes. Do a conversion to a DateTime and I think it may start working.
ie
该代码转换为(具有合理的重命名):
您应该内联执行此操作,不需要 IMO 方法。
The code translates to (with sensible rename):
which you should just do inline, no need for a method IMO.
您需要将输入的文本信息转换为日期时间:)。
否则,你可以使用你的方法 TimeSpan ...它是存根,你是对的;)
you need to cast your input text information to datetime :).
Otherwise, you could use your method TimeSpan ... it s stub and you right ;)