此代码行的帮助:从VB到C#.请 [英] Help with This Line of Code: from VB to C#. PLEASE

查看:83
本文介绍了此代码行的帮助:从VB到C#.请的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将示例应用程序从VB.net重写为C#,它是有关在n层应用程序中使用数据集的示例教程.我成功完成了除了这行代码之外的所有代码:

I am re-writing a sample application from VB.net to C#, its a sample tutorial about using DataSets in n-tier applications. I succeded in all but this line of code:

Dim changes = CType(Me.OrdersDataSet.GetChanges, NorthwindEntities.OrdersDataSet)



我对VB中使用的CType的理解是将一个对象投射到另一个对象.如何在C#中强制转换?

Sub的完整代码为:




My understanding of CType as used in VB is to cast one object to another. How do i cast this in C#?

The complete code for the Sub is:


Private Sub OrdersBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OrdersBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.OrdersBindingSource.EndEdit()

        Using proxy As New NorthwindService.ServiceClient
            Dim changes = CType(Me.OrdersDataSet.GetChanges, NorthwindEntities.OrdersDataSet)

            If changes IsNot Nothing Then
                If proxy.SaveOrders(changes) Then

                    Dim addedrows = From row In Me.OrdersDataSet.Orders _
                                    Where row.RowState = DataRowState.Added

                    For Each row In addedrows.ToArray
                        row.Delete()
                    Next

                    Me.OrdersDataSet.Merge(changes)
                    Me.OrdersDataSet.AcceptChanges()

                    MsgBox("saved")
                Else
                    MsgBox("Not saved")
                End If
            End If
        End Using
    End Sub





Thank you in advance.

推荐答案

在C#中,您像在C ++中一样强制转换-位图b =(位图)n;或者,您可以使用"as",位图b = n作为位图.区别在于as如果失败将返回null,并且失败的强制转换将引发异常.

In C# you cast as in C++ - Bitmap b = (Bitmap)n; Or you can use ''as'', Bitmap b = n as Bitmap. the difference is that as will return null if it fails and a failed cast will throw an exception.

NorthwindEntities.OrdersDataSet changes = this.OrdersDataSet.GetChanges() as NorthwindEntities.OrdersDataSet;



会是我的猜测.



would be my guess.


我尚未测试过,但您应该尝试以下操作:

I haven''t tested, but you should try this:

NorthwindEntities.OrdersDataSet changes = this.OrdersDataSet.GetChanges() as NorthwindEntities.OrdersDataSet;



这使您可以在进入下一个块之前测试null.

干杯.

(哎呀,应该是这个)



This allows you to test for null before entering the next block.

Cheers.

(oops on the Me, should be this)


谢谢詹姆斯和克里斯蒂安.非常感谢您的快速回复.您的回答是正确的.

但是,如果您不介意,请允许我提出另一个问题.实际上,我实际上是想从此处将VB教程重新编码为C#,以便在n层体系结构中使用数据集: http://windowsclient.net/learn/video.aspx?v=14625 [ ].一切正常,但是即使可以获取,我也无法获得更新.

请您看看并帮助我进行更新吗?

谢谢你.谢谢你.感谢在座的每个人在我们提出问题时都可以提供答复.谢谢大家.

等待并希望有人会有所帮助.
Thank you James and Christian. Thank you so very much for the quick response. Your answers were right.

But if you dont mind let me just pose another question. I was actually trying to re-code a tutorial from VB to C# about using DataSets in an n-tier architecture from here: http://windowsclient.net/learn/video.aspx?v=14625[]. Everything works fine, but I have not been able to get Updates, even though I can fetch.

Please will you take a look and help me with what I need to do to make it update?

Thank you. Thank you. Thank everyone here who helps reply when we ask questions. Thank you all.

Waiting and hoping someone will help.


这篇关于此代码行的帮助:从VB到C#.请的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆