?
Solved

Help for Converting vb6 code to vb.net

Posted on 2004-10-11
13
Medium Priority
?
368 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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!

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

800 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