Dear All,
I'm new in C#, It's difiifcult for me to change this code in vb6 to c#
Could anybody help me to convert this code to c#
Private Static Sub ShowData(Data As String)
Dim stx, ret, ret1 As Integer
Dim valcom As Variant
Dim strg As String
strg = strg & Data
stx = InStr(strg, Chr$(2))
If stx Then
ret = InStr(stx + 1, strg, Chr$(13))
Else
ret = 0
End If
If ret Then
If ret - stx <> 16 Then
strg = vbNullString
Exit Sub
End If
If Len(strg) < 17 Then Exit Sub
valcom = Val(Mid(strg, stx + 4, 6))
If Not IsNumeric(valcom) Then
strg = vbNullString
Exit Sub
End If
ret = Asc(Mid(strg, stx + 2, 1)) And 2
ret1 = Asc(Mid(strg, stx + 1, 1)) And 7
strg = vbNullString
If ret1 = 2 Then
lbldaucan = Format(IIf(ret - 2, valcom, valcom * (-1)), "#,#")
ElseIf ret1 = 3 Then
lbldaucan = Format(IIf(ret - 2, valcom / 10, (valcom / 10) * (-1)), "0.0")
ElseIf ret1 = 4 Then
lbldaucan = Format(IIf(ret - 2, valcom / 100, (valcom / 100) * (-1)), "0.00")
ElseIf ret1 = 5 Then
lbldaucan = Format(IIf(ret - 2, valcom / 1000, (valcom / 1000) * (-1)), "0.000")
End If
'lblcom1.Caption = valcom
If lbldaucan = "" Then
lbldaucan = 0
End If
End If
End Sub
Loading
VulpesPosted Aug 31, 2011, 10:58 AM
I'd therefore suggest that you add a reference to Microsoft.VisualBasic.dll to your C# project so that you can use replicas of the original VB6 functions in your code, rather than trying to convert them to the closest .NET equivalents. Even then there are some problems to overcome.
Anyway here's my attempt (I assume lbldaucan is a label):
// ....
Thach LePosted Sep 6, 2011, 2:26 AM
Thanks for your all reply,
i sorted out my problem, i don't use ComEvent as last code,
i use Thread.Sleep;
Private void Com_OnCom(object sender, EventArgs e)
{
Thread.Sleep(200);
if(Com.InBufferCount>0)
{
try
{
string Buffer=(string)Com.Input;
//Call function ShowData
ShowData(Buffer);
}
catch(Exception ex)
{
}
}
}
This project to get weigh from Digital scale (Mettler Toledo)
VulpesPosted Sep 3, 2011, 1:45 PM
Mittal SejpalPosted Sep 3, 2011, 2:12 AM
this link gives convertor which will convert vb6 code csharp
hope it helps you
Mayur GujrathiPosted Sep 3, 2011, 1:09 AM
try this
Thach LePosted Sep 2, 2011, 10:20 PM
Thanks for your reply,
i sorted out the problem with your suggest but when i call this void and have a little problem
this code i used in VB6
Private Sub Comm_OnComm()
Select Case Comm.CommEvent
Case comEvReceive
Dim Buffer As Variant
Buffer = Comm.Input
ShowData (StrConv(Buffer, vbUnicode))
End Select
End Sub
And i tried to migrate to C Sharp
private void Com_OnComm(object sender, EventArgs e)
{
object Buffer;
switch (Com.CommEvent)
{
case MSCommLib.OnCommConstants.comEvReceive:
//"Cannot implicitly convert type 'MSCommLib.OnCommConstants' to 'short'. An explicit conversion exists (are you missing a cast?)"
Buffer = Com.Input;
//string convstr=System.Text.Encoding.GetEncoding(1252).GetString(Buffer);
ShowData(VB.Strings.StrConv(Buffer,Encoding.UTF8.GetBytes(Buffer));
//The best overloaded method match for 'Microsoft.VisualBasic.Strings.StrConv(string, Microsoft.VisualBasic.VbStrConv, int)' has some invalid arguments
break;
}
}
Could you check and fix for me?
Thanks in advance!
Satyapriya NayakPosted Aug 30, 2011, 11:08 PM
There are many converter available in internet use those
One is below
http://www.developerfusion.com/tools/convert/vb-to-csharp/
Thanks