none
Problem when converting C# to VB.NET RRS feed

  • Question

  • Hi Al 

    I'm trying to convert a C# code to VB.NET

    The code come from this link 

    https://www.codeproject.com/Articles/51889/Summary-DataGridView

    and I wish to use to show grand total in a DataGridview

    The problem I get is with the following C# code

     private List<DataGridViewColumn> SortedColumns
            {
                get
                {
                    List<DataGridViewColumn> result = new List<DataGridViewColumn>();
                    DataGridViewColumn column = dgv.Columns.GetFirstColumn(DataGridViewElementStates.None);
                    if (column == null)
                        return result;
                    result.Add(column);
                    while ((column = dgv.Columns.GetNextColumn(column, DataGridViewElementStates.None, DataGridViewElementStates.None)) != null)
                        result.Add(column);
    
                    return result;
                }
            }

    That is automatically converted in this VB code

    Private ReadOnly Property SortedColumns As List(Of DataGridViewColumn)
                Get
                    Dim result As List(Of DataGridViewColumn) = New List(Of DataGridViewColumn)()
                    Dim column As DataGridViewColumn = dgv.Columns.GetFirstColumn(DataGridViewElementStates.None)
                    If column Is Nothing Then Return result
                    result.Add(column)
    
                    While (CSharpImpl.__Assign(column, dgv.Columns.GetNextColumn(column, DataGridViewElementStates.None, DataGridViewElementStates.None))) IsNot Nothing
                        result.Add(column)
                    End While
    
                    Return result
                End Get
            End Property
    
    
    .....
    ......
    
     Private Class CSharpImpl
                <Obsolete("Please refactor calling code to use normal Visual Basic assignment")>
                Shared Function __Assign(Of T)(ByRef target As T, value As T) As T
                    target = value
                    Return value
                End Function
            End Class

    And this code suggest to refactoe the calling code. I really do not know what to do.

    Whis is the rigth VB conversion for the original c# code ?

    Thank for Help

    Tuesday, July 31, 2018 2:43 PM

Answers

  • Yeah, that is a messy conversion... it tried to do too much line-by-line rather than refactoring to suit VB's syntax.

    Assuming that there is already a field of type DataGridView named dgv in this same class file, here is the VB equivalent of that C# code:

    Private ReadOnly Property SortedColumns As List(Of DataGridViewColumn)
        Get
            Dim result = New List(Of DataGridViewColumn)
            Dim column = dgv.Columns.GetFirstColumn(DataGridViewElementStates.None)
            While column IsNot Nothing
                result.Add(column)
                column = dgv.Columns.GetNextColumn(column, DataGridViewElementStates.None, DataGridViewElementStates.None)
            End While
            Return result
        End Get
    End Property
    


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by Claudio111 Wednesday, August 1, 2018 9:53 AM
    Tuesday, July 31, 2018 6:32 PM
    Moderator
  • Hi Karen

    I just would like to convert in VB to understand how it works since i don't know C#

    Do you think is it  difficult to convert it. ?

    Let's put this into perspective, if I converted the code (just looked at it again), estimated time to convert then tweak things that converters generally guess at (or there is more than once way to do the convert) would be 15 minutes but wait, I would have to create the designer files, another 15 to 40 minutes, no code, just create and save mimicking what was done in the original code. I would ask myself, is it work it, NO because there is truly nothing special to learn from this code so best to use the class project that VB.NET implements. 

    With no disrespect, if you never done a conversion like this it could take hours or more to do and really not learn much from it unless you want to better understand C#. 

    Your time would be better spent on something else. That something else could tie right back into the code from Code Project, study it, get a sense of what it does. Now rather than use a converter, rewrite it yourself, that's a great way to learn. If this is simply to learn and you don't want to study the code, move on to another topic to study.

     

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Claudio111 Wednesday, August 1, 2018 9:53 AM
    Tuesday, July 31, 2018 7:02 PM
    Moderator

All replies

  • If the intent is to convert the code in SummaryDataGridView project then don't, instead include that project in your solution. Do conversions on the code in the form and the DataAccess class unless you are going to modify the code in the SummaryDataGridView.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, July 31, 2018 2:57 PM
    Moderator
  • Hi Karen

    I just would like to convert in VB to understand how it works since i don't know C#

    Do you think is it  difficult to convert it. ?

    Tuesday, July 31, 2018 3:27 PM
  • Yeah, that is a messy conversion... it tried to do too much line-by-line rather than refactoring to suit VB's syntax.

    Assuming that there is already a field of type DataGridView named dgv in this same class file, here is the VB equivalent of that C# code:

    Private ReadOnly Property SortedColumns As List(Of DataGridViewColumn)
        Get
            Dim result = New List(Of DataGridViewColumn)
            Dim column = dgv.Columns.GetFirstColumn(DataGridViewElementStates.None)
            While column IsNot Nothing
                result.Add(column)
                column = dgv.Columns.GetNextColumn(column, DataGridViewElementStates.None, DataGridViewElementStates.None)
            End While
            Return result
        End Get
    End Property
    


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by Claudio111 Wednesday, August 1, 2018 9:53 AM
    Tuesday, July 31, 2018 6:32 PM
    Moderator
  • Hi Karen

    I just would like to convert in VB to understand how it works since i don't know C#

    Do you think is it  difficult to convert it. ?

    Let's put this into perspective, if I converted the code (just looked at it again), estimated time to convert then tweak things that converters generally guess at (or there is more than once way to do the convert) would be 15 minutes but wait, I would have to create the designer files, another 15 to 40 minutes, no code, just create and save mimicking what was done in the original code. I would ask myself, is it work it, NO because there is truly nothing special to learn from this code so best to use the class project that VB.NET implements. 

    With no disrespect, if you never done a conversion like this it could take hours or more to do and really not learn much from it unless you want to better understand C#. 

    Your time would be better spent on something else. That something else could tie right back into the code from Code Project, study it, get a sense of what it does. Now rather than use a converter, rewrite it yourself, that's a great way to learn. If this is simply to learn and you don't want to study the code, move on to another topic to study.

     

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Claudio111 Wednesday, August 1, 2018 9:53 AM
    Tuesday, July 31, 2018 7:02 PM
    Moderator