Solved

Help for Converting vb6 code to vb.net

Posted on 2004-10-11
13
354 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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 250 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

830 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