Link to home
Start Free TrialLog in
Avatar of Joe_Shen
Joe_Shen

asked on

Help for Converting vb6 code to vb.net

Hi,

Could anyone help me convert following vb code to vb.net code? This app is to get CIDR list for given ip range(217.224.154.255 to 217.224.155.9 ). listbox named "lstIP" just for display CIDR purpose. Thanks a lot.

Option Explicit

Private Type TByteRec
   B(1 To 8) As Byte
End Type
Private Type TCcyRec
   C As Currency
End Type

Private Sub Command1_Click()
 Call LS_PrintRange("217.224.154.255", "217.224.155.9")
End Sub

Private Sub LS_PrintRange(LoIP$, HiIP$)
   Dim LO As Currency, HI As Currency
   Dim l9 As Currency
   
   LO = LF_IpToCcy(LoIP$)
   HI = LF_IpToCcy(HiIP$)
     
       
   ' --- Currency is an 8 Byte Integer / 10000
   For l9 = LO To HI Step 0.0001
       lstIP.AddItem LF_CcyToIP(l9)
   Next
End Sub

Private Function LF_IpToCcy(IP$) As Currency
   Dim ByteRec As TByteRec
   Dim CcyRec As TCcyRec
   Dim l9&, q&, pos&
   
   ' Fill in the Low 4 bytes [4][3][2][1]
   ' Here one would use Split() in VB6
   q = 1
   For l9 = 1 To 4
       pos = InStr(q, IP$, ".")
       If pos = 0 Then pos = Len(IP$) + 1
       ByteRec.B(5 - l9) = Val(Mid$(IP$, q, pos - q))
       q = pos + 1
   Next
   ' ---
   LSet CcyRec = ByteRec
   LF_IpToCcy = CcyRec.C
   
End Function

Private Function LF_CcyToIP(No As Currency) As String
   Dim ByteRec As TByteRec
   Dim CcyRec As TCcyRec
   Dim l9&, S$
   
   CcyRec.C = No
   LSet ByteRec = CcyRec
   
   ' --- Build 4.3.2.1
   For l9 = 1 To 4
       S$ = CStr(ByteRec.B(l9)) + S$
       If l9 < 4 Then S$ = "." + S$
   Next
   
   LF_CcyToIP$ = S$
     
End Function

Avatar of Bob Learned
Bob Learned
Flag of United States of America image

What happens when you use the upgrade wizard?  What version of VB.NET do you have (2002 or 2003)?

Bob
Avatar of Joe_Shen
Joe_Shen

ASKER

Thanks for response, TheLearnedOne.

I use 2003 version. When I converted them, code " LSet CcyRec = ByteRec" gave an error.
I am beginer of VB.net. Would you help me out if you can?

Thank you very much.

Joe
Delete the LSet and just use CcyRec = ByteRec.

Bob
Thank you, Bob.

I tested use CcyRec = ByteRec but got error " TCcyRec can not be converted to 'byte'". Maybe it results in how I convert "Private Type TByteRec" and "Private Type  TCcyRec". Here are my codes for convertion:

VB6:  
   Private Type TByteRec
     B(1 To 8) As Byte
   End Type

   Private Type TCcyRec
     C As Currency
  End Type

Converted to VB.net:

   Public Structure TByteRec
        Dim B() As Byte
        Sub New(ByVal ArraySize As Integer)
            ReDim B(ArraySize)
        End Sub
    End Structure

    Public Structure TCcyRec
        Dim C As Decimal
    End Structure

So do you know what my bugs are?

Thanks a lot

Joe
If you are trying to convert a byte array to a decimal value:

Dim b() As Byte

Dim d As Decimal = Convert.ToDecimal(b)

You can do this without the structure.  Structures with only 1 member don't make sense to me.  That is just a variable.  You were using user-defined types, since the LSet worked in VB6, but here you need to do something different.

Bob



Thanks, Bob.

I still can not get them right. If you don't mind, could you convert my vb codes below to vb.net in your convenience? I know I must be asking too much but I desperately need to get them converted right.

Very appreciated for your help,..patience.., and more..

Joe

VB6 codes: ("lstIP" is listbox)

Option Explicit

Private Type TByteRec
   B(1 To 8) As Byte
End Type
Private Type TCcyRec
   C As Currency
End Type

Private Sub Command1_Click()
 Call LS_PrintRange("217.224.154.255", "217.224.155.9")
End Sub

Private Sub LS_PrintRange(LoIP$, HiIP$)
   Dim LO As Currency, HI As Currency
   Dim l9 As Currency
   
   LO = LF_IpToCcy(LoIP$)
   HI = LF_IpToCcy(HiIP$)
     
       
   ' --- Currency is an 8 Byte Integer / 10000
   For l9 = LO To HI Step 0.0001
       lstIP.AddItem LF_CcyToIP(l9)
   Next
End Sub

Private Function LF_IpToCcy(IP$) As Currency
   Dim ByteRec As TByteRec
   Dim CcyRec As TCcyRec
   Dim l9&, q&, pos&
   
   ' Fill in the Low 4 bytes [4][3][2][1]
   ' Here one would use Split() in VB6
   q = 1
   For l9 = 1 To 4
       pos = InStr(q, IP$, ".")
       If pos = 0 Then pos = Len(IP$) + 1
       ByteRec.B(5 - l9) = Val(Mid$(IP$, q, pos - q))
       q = pos + 1
   Next
   ' ---
   LSet CcyRec = ByteRec
   LF_IpToCcy = CcyRec.C
   
End Function

Private Function LF_CcyToIP(No As Currency) As String
   Dim ByteRec As TByteRec
   Dim CcyRec As TCcyRec
   Dim l9&, S$
   
   CcyRec.C = No
   LSet ByteRec = CcyRec
   
   ' --- Build 4.3.2.1
   For l9 = 1 To 4
       S$ = CStr(ByteRec.B(l9)) + S$
       If l9 < 4 Then S$ = "." + S$
   Next
   
   LF_CcyToIP$ = S$
     
End Function
In VB6, what is the output of this code?  What is the CIDR list output values for the range shown?

Bob
output will be displayed in lstIP as below:

217.224.154.255
217.224.155.0
217.224.155.1
217.224.155.2
217.224.155.3
217.224.155.4
217.224.155.5
217.224.155.6
217.224.155.7
217.224.155.8
217.224.155.9


Thanks.

Joe
Bob,

To play around above vb6 codes, just add one listbox named "lstIP" and command button. You can change IP range to see different output.

Thanks.

Joe
I could imagine a complete different scenario for this, but I am not sure if I quite have the time yet to investigate.  

Pseudo code:

Split starting IP into elements by '.'

Loop

  Increment element #4

  Assemble IP address

Until ip = last IP in the range

Bob
Sorry, that should be:

Split starting IP into elements by '.'

Loop

  Increment element #4

  If element #4 > 255
      Increment element #3
      Set element #4 = 1
  End

  Assemble IP address

Until ip = last IP in the range

Bob
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Bob,

Thank you for your time and effort. I will investigate your way.

Have a good one.

Joe