?
Solved

Help for Converting vb6 code to vb.net

Posted on 2004-10-11
13
Medium Priority
?
373 Views
Last Modified: 2012-06-27
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

0
Comment
Question by:Joe_Shen
  • 7
  • 6
13 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 12286658
What happens when you use the upgrade wizard?  What version of VB.NET do you have (2002 or 2003)?

Bob
0
 

Author Comment

by:Joe_Shen
ID: 12287659
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
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 12287682
Delete the LSet and just use CcyRec = ByteRec.

Bob
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:Joe_Shen
ID: 12289504
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
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 12289911
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



0
 

Author Comment

by:Joe_Shen
ID: 12290910
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
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 12290967
In VB6, what is the output of this code?  What is the CIDR list output values for the range shown?

Bob
0
 

Author Comment

by:Joe_Shen
ID: 12291680
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
0
 

Author Comment

by:Joe_Shen
ID: 12291751
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
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 12297626
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
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 12297639
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
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 750 total points
ID: 12297658
Thinking about this even more:

Split starting IP into elements by '.'

Loop

  Increment element #4

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

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

  If element #2 > 255
      Increment element #1
      Set element #2 = 1
  End

  Assemble IP address from the 4 elements

Until ip = last IP in the range

Bob
0
 

Author Comment

by:Joe_Shen
ID: 12298017
Bob,

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

Have a good one.

Joe
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month16 days, 12 hours left to enroll

864 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