Solved

Help with VB6 code to VB.NET

Posted on 2011-03-11
5
291 Views
Last Modified: 2012-05-11
In VB6 I could use a collection that had properties to create a collection of the same type of thing.  Now, I'm trying to use List(of T).  There must be an easier way in VB.NET.  Any suggestions?

Here's the VB6 code, where col is the collection:
Dim col as ReportLines
Me.Row = Me.Row + 1
col.Add Me.Row, mCol1, mTotal1
col.Add Me.Row, mCol2, mTotal2

In VB.Net:
Dim ListR as New List(Of ReportLine)
Dim obj as New ReportLine
Me.Row += 1
obj.Add(Me.Row, mCol1, mTotal1)
ListR.Add(obj)
obj = New ReportLine
obj.Add(Me.Row, mCol2, mTotal2)
ListR.Add(obj)
0
Comment
Question by:Agent909
5 Comments
 
LVL 23

Accepted Solution

by:
wdosanjos earned 333 total points
ID: 35111315
If you add a constructor to ReportLine that takes the three variables, you can rewrite the VB.NET code as follows:

Dim ListR as New List(Of ReportLine)
Dim obj as New ReportLine
Me.Row += 1
ListR.Add(New ReportLine(Me.Row, mCol1, mTotal1))
ListR.Add(New ReportLine(Me.Row, mCol2, mTotal2))
0
 
LVL 29

Assisted Solution

by:Paul Jackson
Paul Jackson earned 167 total points
ID: 35111332
Dim ListR as New List(Of ReportLine)
Me.Row+= 1
ListR.Add(new ReportLine(Me.Row, mCol1,mTotal1))
ListR.Add(new ReportLine(Me.Row, mCol2,mTotal2))
0
 
LVL 23

Assisted Solution

by:wdosanjos
wdosanjos earned 333 total points
ID: 35111346
The ReportLine constructor could be something like:

Public Class ReportLine
    Public Sub New()
    End Sub

    Public Sub New(row As Integer, col As Integer, total As Integer)
        Me.Add(row, col, total)
    End Sub

    ...
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35111409
Let me start by saying I agree with wdosanjos' suggestion. However...

If you *really* wanted to mimic the exact behavior of your VB6 code, you could add an extension method that would let you mimic what you are doing in VB6. Create a new method that looks like this (change parameter types accordingly):

Imports System.Runtime.CompilerServices

...

<Extension()> _
Public Sub Add(ByRef source As List(Of ReportLine), ByVal row As Integer, ByVal col As Integer, ByVal total As Integer)
    source.Add(New ReportLine(row, col, total))
End Sub

Open in new window



Then you can mimic what you did in VB6:

ListR.Add(Me.Row, mCol1, mTotal1)

Open in new window

0
 
LVL 1

Author Closing Comment

by:Agent909
ID: 35111416
Thanks to all of you for the great answers!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MailAddress in vb 4 36
System.net.Sockets Error 5 38
Collapse and expand table in Word 2010 2 90
Access/Visual Basic Question 3 41
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question