locked
VB to C# Conversion Problem RRS feed

  • Question

  • Hi,

    I wasn’t sure where to ask this question, but I thought this would be the best section. If it isn’t, I’m sorry for any inconvenience and feel free to move this thread to where ever my question fits best.

    I am in the process of learning ASP.Net programming with C#. I am reading a book and going along with it. The primary programming language the book uses is Visual Basic, but, up until now, has not been a problem since I’m fairly familiar with both VB and C#. One thing that is new to me in both languages is LINQ, which leads me to my question. The books is asking me to set up and bind a DataSource manually to a GridView using LINQ, which I thought shouldn’t be very hard. Looking at the example in the book (in VB) I cannot figure out what the code should be in C#. This should have not been that big of an issue since, according to the book, all examples and source code are provided on the internet for me to download for free, which I did, however, most, if not all, of the examples in C# are blank (well, a page load skeleton and nothing else). If anyone can help me, I will appreciate it greatly and thanks in advanced. I really don’t want to skip this, cause I’d like see how this works since LINQ seems extremely useful and I’m sure I will be able to use this in the future. 

    CODE EXAMPLE:

    Partial Class _Default
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not IsPostBack Then
                Dim dataContext As New AdventureWorksDataContext()
                Dim products = From p In dataContext.Products _
                               Order By p.Name _
                               Select p.ProductID, p.Name, p.ProductNumber, p.MakeFlag, _
                                  p.SafetyStockLevel, p.ReorderPoint
                GridView2.DataSource = products
                GridView2.DataBind()
            End If
    
        End Sub
    End Class
    
    Tuesday, January 26, 2010 9:37 PM

Answers

  • The main difference, here, is how anonymous types are created.  In C#, you do:
        new { property1, property2 };

    This routine, using that technique, would be:

    protected void Page_Load(sender As Object, e As System.EventArgs)
    {
        if(!IsPostBack)
        {
            var dataContext = new AdventureWorksDataContext();
            var products = from p in dataContext.Products
                   order by p.Name
                   select new { p.ProductID, p.Name, p.ProductNumber, p.MakeFlag, p.SafetyStockLevel, p.ReorderPoint };
    
            GridView2.DataSource = products;
            GridView2.DataBind();
        }
     }
    You'll have to also add the page load event handler in manually, instead of relying on "Handles" - but that  should be familiar if you're used to converting VB.NET into C#.


    Reed Copsey, Jr. - http://reedcopsey.com
    • Marked as answer by pippett2 Tuesday, January 26, 2010 9:57 PM
    Tuesday, January 26, 2010 9:45 PM

All replies

  • The main difference, here, is how anonymous types are created.  In C#, you do:
        new { property1, property2 };

    This routine, using that technique, would be:

    protected void Page_Load(sender As Object, e As System.EventArgs)
    {
        if(!IsPostBack)
        {
            var dataContext = new AdventureWorksDataContext();
            var products = from p in dataContext.Products
                   order by p.Name
                   select new { p.ProductID, p.Name, p.ProductNumber, p.MakeFlag, p.SafetyStockLevel, p.ReorderPoint };
    
            GridView2.DataSource = products;
            GridView2.DataBind();
        }
     }
    You'll have to also add the page load event handler in manually, instead of relying on "Handles" - but that  should be familiar if you're used to converting VB.NET into C#.


    Reed Copsey, Jr. - http://reedcopsey.com
    • Marked as answer by pippett2 Tuesday, January 26, 2010 9:57 PM
    Tuesday, January 26, 2010 9:45 PM
  • Wow, thanks so much for your quick response. You're right, I had everything execpt new and the brackets.
    Tuesday, January 26, 2010 9:57 PM