Solved

Help with VB6 code to VB.NET

Posted on 2011-03-11
5
288 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
Comment Utility
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
Comment Utility
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
Comment Utility
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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
Thanks to all of you for the great answers!
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

728 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now