Solved

Help for Converting vb6 code to vb.net

Posted on 2004-10-11
13
326 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
Comment Utility
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
Comment Utility
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
Comment Utility
Delete the LSet and just use CcyRec = ByteRec.

Bob
0
 

Author Comment

by:Joe_Shen
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Bob,

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

Have a good one.

Joe
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now