Converting VB6 to VB.NET

F

Thread Starter

Fluoronator

<p>I'm having trouble making the transition from VB6 to VB.NET. I have a VB6 application which reads some data using the OPCDaAuto.DLL (OPC Automation 2.0) and then places it in a database. I'm trying to convert the code to VB.NET (for my own education). Here are the first few lines of VB6 code... I have a reference to OPCDaAuto.DLL (OPC Automation 2.0)
<pre>
Option Explicit
Dim OPCSvr As New OPCAutomation.OPCServer
Dim OPCSvrGroups As OPCAutomation.OPCGroups
Private Sub Command1_Click()
OPCSvr.Connect ("OPCDataCtrl.OPCSimServer.1")
Set OPCSvrGroups = OPCSvr.OPCGroups
OPCSvrGroups.DefaultGroupIsActive = True
End Sub
</pre>
<p>Here's the same VB.NET code generated by the upgrade wizard...
<pre>
Dim OPCSvr As New OPCAutomation.OPCServer
Dim OPCSvrGroups As OPCAutomation.OPCGroups
Private Sub Command1_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command1.Click
OPCSvr.Connect("OPCDataCtrl.OPCSimServer.1")
OPCSvrGroups = OPCSvr.OPCGroups
OPCSvrGroups.DefaultGroupIsActive = True
End Sub
</pre>
<p>When I run the VB.NET version I recieve the following error on the line... "OPCSvrGroups.DefaultGroupIsActive = True"
*******
An unhandled exception of type 'System.NullReferenceException' occurred in Project1.exe
Additional information: Object reference not set to an instance of an object.
*******

<p>I don't understand why, I set OPCSvrGroups = OPCSvr.OPCGroups, what is the problem here?
 
Hi Flouro,

From what I understand there is a problem when using the existing OPC automation wrapper with .NET in that it doesn't convert a type in the way it advertises that it will. In .NET you normally get interfaces by casting the object to an object of the type of the interface and all of the underlying CoCreateInstance stuff is done by the .NET interop layer. If the interface can't be supported by the type of object you supplied then you will get a null object and an exception. I don't know the source of you automation wrapper but you might try casting something like...

OPCSvrGroups = CType(OpcSvr, OPCAutomation.OPCGroups)

...but this may not work either.

I think the policy of the OPC foundation is to distribute the automation wrapper source to members, normally server vendors, who will fix, distribute and support the wrapper's use in .NET. If you are a member you can get the source or you should be able to get a working one from your server vendor, assuming they've done the work. (I'm not speaking for OPC here.)

If you just want to see how .NET works with OPC DA you can download our free client which does some stuff like scaling, masking and buffering tags. It's actually useful when debugging, etc. We also have an assembly that you can
code to with lots of examples in C# and VB.NET but this uses our own interop code to the OPC custom interface which you can't get to.

This is probably not the easy code fix you were looking for but I hope this helps some.

[email protected]
 
B
When you declare OPCSvrGroup in the line...

Dim OPCSvrGroups As OPCAutomation.OPCGroups

you are only declare a reference to the object. This means that the object does not exist at present and you need to create the object in memory later with the Set command.

You can create the object in the declaration by

Dim OPCSvrGroups As new OPCAutomation.OPCGroups
or you can create it later by...

set OPCSvrGroups = new....

I hope this helps.
 
The problem is OPC uses COM and .NET doesn't.

There are ways around it. I didn't have time to figure it out, so I bought software from Metadynamics to cure the problem.
 
H
What is the best product to convert VB6 to either VB.Net or C#? How much effort is typically required to fix the converted code so it will work? This VB6 uses SQL/2000 and ADO.

Thanks.
 
<pre>Public ReadOnly Property Modal As Boolean

Private Sub ShowMyNonModalForm()
Dim myForm As New Form()
myForm.Text = "My Form"
myForm.SetBounds(10, 10, 200, 200)

myForm.Show()
' Determine if the form is modal.
If myForm.Modal = False Then
' Change borderstyle and make it not a top level window.
myForm.FormBorderStyle = FormBorderStyle.FixedToolWindow
myForm.TopLevel = False
End If
End Sub
</pre>
 
<pre>
Option Explicit
Dim OPCSvr As New OPCAutomation.OPCServer
Dim OPCSvrGroups As OPCAutomation.OPCGroups
Private Sub Command1_Click()
OPCSvr.Connect ("OPCDataCtrl.OPCSimServer.1")
Set OPCSvrGroups = OPCSvr.OPCGroups
OPCSvrGroups.DefaultGroupIsActive = True
End Sub
</pre>
 
<pre>
Public Function CheckContainerNo(ContNo As String) As Boolean
Dim CharVal, DigSum, CharSum
Dim Chno, I
CheckContainerNo = False
If Len(ContNo) <> 12 Then Exit Function
CharVal = "1012131415161718192021232425262728293031323435363738"
DigSum = 0
CharSum = 0
For I = 1 To 6
DigSum = DigSum + Val(Mid(ContNo, 5 + I, 1)) * 2 ^ (3 + I)
Next

For I = 1 To 4
Chno = Val(Mid(CharVal, (Asc(Mid(ContNo, I, 1)) - Asc("A")) * 2 + 1, 2))
CharSum = CharSum + Chno * (2 ^ (I - 1))
Next

DigSum = DigSum + CharSum
DigSum = DigSum Mod 11
If DigSum = 10 Then DigSum = 0
If DigSum <> Val(Mid(ContNo, 12, 1)) Then
CheckContainerNo = False
Else
CheckContainerNo = True
End If
End Function </pre>
 
F
There is no concept of convert (no matter how much we would like to think of this). Rewrite is the word. Based on number of lines of code,no of form and third party controls used, and backend database would help in getting a good estimate. If you like to process then pls. contact me on [email protected]
 
I would like to get some personal help with my Visual Studio 2005 Express project development.

I'm learning but when I get errors I have a hard time repairing them.

Can you help?
 
Top