We help IT Professionals succeed at work.

# vb.net count ip range

on
1,068 Views
hello there,
I would like to count the ranges of ip for example

127.0.0.1-127.0.0.20

to give me total.. how can I do that?
Comment
Watch Question

## View Solution Only

Commented:
There might be something built in for this, but if you read them in as a String and then did a .Split() using the Char of ".", then you could just compare the numbers and subtract one from the other.

Commented:
yes indeed that would be the best option.
``````Private Function GetRange(ByVal strIP1 As String, ByVal strIP2 As String) As Integer

Dim arIP1() As String = Split(strIP1, ".")
Dim arIP2() As String = Split(strIP2, ".")

Dim intIP1 As Integer = CInt(arIP1(arIP1.GetUpperBound(0)))
Dim intIP2 As Integer = CInt(arIP2(arIP2.GetUpperBound(0)))
Dim intIP As Integer
If intIP1 > intIP2 Then
intIP = intIP1 - intIP2
Else
intIP = intIP2 - intIP1
End If
Return intIP
End Function
``````

Commented:
That's assuming that the last group is all that's different.  Be sure to account for the fact that you might have something on different subnets, if necessary.  Not sure what your full requirements might be...

Commented:
it could be 1.1.1.1-5.5.5.5 or
it could be 128.65.85.75-220.35.65.8

it all random!!

Commented:
You will want to do String.Compare() to find out what is equal and what isn't, then follow the same logic as Kraeven posted above...

Commented:
A little more on this...Unless you are forcing an IP of XXX.XXX.XXX.XXX, then I would probably start by making sure that the Length of my string arrays are the same...
dim ip1() as string = string.split("100.20.5.10",cchar("."))
dim ip2() as string = string.split("100.20.6.10",cchar("."))
If ip1.Length = ip2.Length then
'some code
End if
That way you know that they have the same number of octets.  Then start comparing each octet to see where the change is...
If you know they both have the same length, then you can check by the following...
For i as integer = 0 to ip1.Length()
If String.Compare(ip1(i),ip2(i),False) <0 then
'ip1(i) < ip2(i)
ElseIf String.Compare(ip1(i),ip2(i),False) =0 then
'They are equal
ElseIf String.Compare(ip1(i),ip2(i),False) >0 then
ip2(i)>ip1(i)
Next

That should get you started...there is a lot more that needs to be added, but that should at least show you the logic of it.

Commented:

http://www.codeproject.com/KB/cs/NetCalc.aspx

For your convenience I've converted his code from C# to VB.NET
``````'IPTOOL.VB

Imports System.Net

Namespace NetCalc
''' <summary>
''' Summary description for IPTool.
''' </summary>
Public Class IPTool
' first (0x00) only for fill - so array starts with bit 1
Shared bit As Byte() = {&H0, &H1, &H2, &H4, &H8, &H10, _
&H20, &H40, CByte(&H80)}

Protected ip As String = Nothing
Protected ipBytes As Byte() = Nothing
'protected byte a,b,c,d;
Protected networkPrefix As Integer
' z.b. 24 for /24
Protected firstSubNetBit As Boolean = False

Public Sub New(ip As String)
Me.setIp(ip)
End Sub

Public Shared Function getBit(bit As Integer) As Byte
Dim r As Byte = 0
If bit >= 1 AndAlso bit <= 8 Then
r = IPTool.bit(bit)
End If

Return r
End Function

Public Shared Function setBit(b As Byte, bit As Integer) As Byte
If bit >= 1 AndAlso bit <= 8 Then
Return CByte(b Or IPTool.bit(bit))
Else
Return 0
End If
End Function

Public Shared Function isBitSet(b As Byte, bit As Integer) As Boolean
Dim r As Boolean = False
If bit >= 1 AndAlso bit <= 8 Then
r = ((b And IPTool.bit(bit)) <> 0)
End If

Return r
End Function

'
'		 * return true if domainName is an IP
'

Public Shared Function isIP(domainName As [String]) As Boolean
'if (domainName.matches("[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}"))
If domainName.Equals("[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}") Then
Return True
Else
Return False
End If
End Function

'
'		 * Converts (unsigned)byte to int
'

Public Shared Function byte2int(b As Byte) As Integer
Dim i As Integer = b
If b < 0 Then
i = b And &H7f + 128
End If

Return i
End Function

' return first byte of IP
Public Function get1Byte() As Byte
Return Me.ipBytes(0)
End Function

'	return second byte of IP
Public Function get2Byte() As Byte
Return Me.ipBytes(1)
End Function

'	return third byte of IP
Public Function get3Byte() As Byte
Return Me.ipBytes(2)
End Function

'	return fourth byte of IP
Public Function get4Byte() As Byte
Return Me.ipBytes(3)
End Function

'	return array with all bytes of IP
Public Function getBytes() As Byte()
End Function

Public Function getIp() As [String]
Return Me.ip
End Function

Public Sub setIp(ip As [String])
Me.ip = ip
Me.ipBytes = getBytes()
Me.setPrefix2NetworkClassPrefix()
End Sub

Public Sub setPrefix2NetworkClassPrefix()
Me.setNetworkPrefix(Me.getNetworkClassPrefix())
End Sub

Public Function getNetworkClassPrefix() As Integer
' set network-prefix with Class-Type
Dim netClass As Integer = getNetworkClass()
Select Case netClass
Case 0
Return 8
' Class A
Case 1
Return 16
' Class B
Case 2
Return 24
' Class C
'default: setNetworkPrefix(-1);	// other Classes have no default Prefix
End Select
Return -1
End Function

'
'		* return -1 if out of range (0-255) or other error
'

Private Function convertIPByte(ipByte As [String]) As Integer
'int res = Integer.valueOf(ipByte).intValue();
Dim res As Integer = Int32.Parse(ipByte)
If res < 0 OrElse res > 255 Then
res = -1
Me.ip = Nothing
End If
Return res
End Function

'*
'		 * @return
'

Public Function getNetworkPrefix() As Integer
Return Me.networkPrefix
End Function

'*
'		 * @param prefix
'

Public Sub setNetworkPrefix(prefix As Integer)
Me.networkPrefix = prefix
End Sub

Public Function getNetworkClassName() As [String]
Select Case Me.getNetworkClass()
Case 0
Return "Class A"
Case 1
Return "Class B"
Case 2
Return "Class C"
Case 3
Return "Class D/Multicast"
Case 4
Return "Class E/not used"
Case Else
Return "error"
End Select
End Function

'
'		 * 0=Class A
'		 * 1=Class B
'		 * 2=Class C
'		 * 3=Class D
'		 * 4=Class E
'		 * -1=error
'

Public Function getNetworkClass() As Integer
' Test Class A: first bit=0
If Not IPTool.isBitSet(Me.get1Byte(), 8) Then
Return 0
' Test Class B: first 2bit=10
ElseIf Not IPTool.isBitSet(Me.get1Byte(), 7) Then
Return 1
'	Test Class C: first 3bit=110
ElseIf Not IPTool.isBitSet(Me.get1Byte(), 6) Then
Return 2
'	Test Class D: first 4bit=1110
ElseIf Not IPTool.isBitSet(Me.get1Byte(), 5) Then
Return 3
Else
'	Test Class E: first 4bit=1111 ( = all other)
'		else if (!IPTool.isBitSet(this.get1Byte(),8)) return 4;
Return 4
End If

'
'					// Test Class A: first bit=0
'					if ((this.get1Byte() | (byte)0x7f) == (byte)0x7f) return 0;
'					//	Test Class B: first 2bit=10
'					if ((this.get1Byte() | 0xbf) == 0xbf) return 1;
'					//	Test Class C: first 3bit=110
'					if ((this.get1Byte() | 0xdf) == 0xdf) return 2;
'					//	Test Class D: first 4bit=1110
'					if ((this.get1Byte() | 0xef) == 0xef) return 3;
'					//	Test Class E: first 4bit=1111
'					if ((this.get1Byte() & 0xf0) == 0xf0) return 4;
'					return -1;
'

End Function

End Function

Public Function getNetworkMaskByPrefix(prefix As Integer) As [String]
Select Case prefix
Case 8
Return "255.0.0.0"
Case 9
Return "255.128.0.0"
Case 10
Return "255.192.0.0"
Case 11
Return "255.224.0.0"
Case 12
Return "255.240.0.0"
Case 13
Return "255.248.0.0"
Case 14
Return "255.252.0.0"
Case 15
Return "255.254.0.0"
Case 16
Return "255.255.0.0"
Case 17
Return "255.255.128.0"
Case 18
Return "255.255.192.0"
Case 19
Return "255.255.224.0"
Case 20
Return "255.255.240.0"
Case 21
Return "255.255.248.0"
Case 22
Return "255.255.252.0"
Case 23
Return "255.255.254.0"
Case 24
Return "255.255.255.0"
Case 25
Return "255.255.255.128"
Case 26
Return "255.255.255.192"
Case 27
Return "255.255.255.224"
Case 28
Return "255.255.255.240"
Case 29
Return "255.255.255.248"
Case 30
Return "255.255.255.252"
Case Else
Return ""
End Select
End Function

Public Function getMaxNetworkHosts() As Integer
' 2^(32-networkprefix)
' -2 ... because .0 and .255
Return CInt(Math.Truncate(Math.Pow(2, (32 - Me.networkPrefix)))) - 2
End Function

Public Function getMaxSubNets() As Integer
' Bits from Subnet = prefix-class_prefix

Dim count As Integer = CInt(Math.Truncate(Math.Pow(2, Me.networkPrefix - Me.getNetworkClassPrefix())))

'	-2 because 1 bit for routing
If Not Me.isFirstSubNetBit() OrElse Me.getNetworkClassPrefix() = Me.networkPrefix Then
count -= 2
End If

If count < 0 Then
count = 0
End If
Return count
End Function

Public Function getNetworkLong() As Long
Dim mask As Long = CLng(Math.Truncate(Math.Pow(2, Me.networkPrefix))) - 1

End Function

Public Function getNetwork() As [String]
Return long2String(getNetworkLong())
End Function

Dim netMask As Long = CLng(Math.Truncate(Math.Pow(2, Me.networkPrefix))) - 1
Dim hostMask As Long = CLng(Math.Truncate(Math.Pow(2, 32 - Me.networkPrefix))) - 1

End Function

End Function

Public Function getNetworkIPRange() As [String]()
Dim result As [String]() = New [String](1) {}
'String from, to;
'*
'			 * Start
'			 * +1 because first = network
'

result(0) = long2String(getNetworkLong() + 1)
'*
'			 * End
'			 * -1 because last = broadcast
'

Return result
End Function

Public Function ip2Long() As Long
Return (CLng(IPTool.byte2int(Me.get1Byte()) * 256 + IPTool.byte2int(Me.get2Byte())) * 256 + IPTool.byte2int(Me.get3Byte())) * 256 + IPTool.byte2int(Me.get4Byte())
End Function

Public Function long2String(ip As Long) As [String]
Dim a As Long = CLng(ip And &Hff000000UI) >> 24
Dim b As Long = CLng(ip And &Hff0000) >> 16
Dim c As Long = CLng(ip And &Hff00) >> 8
Dim d As Long = CLng(ip And &Hff)

Return a & "." & b & "." & c & "." & d
End Function

Public Function isFirstSubNetBit() As Boolean
Return firstSubNetBit
End Function

Public Sub setFirstSubNetBit(b As Boolean)
firstSubNetBit = b
End Sub

Public Shared Function getNextIP(ip As [String]) As IPTool
Dim nextIp As New IPTool(ip)
nextIp.setIp(nextIp.long2String(nextIp.ip2Long() + CLng(1)))

Return nextIp

End Function

Public Function getNextSubNet(numberOfHosts As Long) As IPTool
Dim ip As IPTool = IPTool.getNextIP(Me.getBroadCast())
ip.setIp(Me.long2String(ip.ip2Long() + CLng(1)))
ip.setFirstSubNetBit(Me.isFirstSubNetBit())

Dim lastPrefix As Integer = ip.getNetworkClassPrefix()

Dim prefix As Integer = 30
Do
If prefix < ip.getNetworkClassPrefix() Then
Return Nothing
End If
' no subnet found
ip.setNetworkPrefix(prefix)
prefix -= 1
' ignore subnetbit?
If Not ip.isFirstSubNetBit() AndAlso prefix = lastPrefix + 1 Then
prefix -= 1

End If
Loop While ip.getMaxNetworkHosts() < numberOfHosts

Return ip
End Function
Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, value As T) As T
target = value
Return value
End Function
End Class
End Namespace

'MAINFORM.VB

Imports System.Drawing
Imports System.Collections
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Data

Namespace NetCalc
''' <summary>
''' Summary description for MainForm.
''' </summary>
Public Class MainForm
Inherits System.Windows.Forms.Form
#Region "Declarations"
Protected ip As IPTool = Nothing
Protected fill As Boolean = False
' lock events wenn updating
Protected isIP As Boolean = False
#End Region

Private groupBox1 As System.Windows.Forms.GroupBox
Private groupBox2 As System.Windows.Forms.GroupBox
Private label1 As System.Windows.Forms.Label
Private label2 As System.Windows.Forms.Label
Private label4 As System.Windows.Forms.Label
Private label5 As System.Windows.Forms.Label
Private label6 As System.Windows.Forms.Label
Private label7 As System.Windows.Forms.Label
Private label8 As System.Windows.Forms.Label
Private label9 As System.Windows.Forms.Label
Private label10 As System.Windows.Forms.Label
Private label11 As System.Windows.Forms.Label
Private netClassComboBox As System.Windows.Forms.ComboBox
Private noticeLabel As System.Windows.Forms.Label
Private calcButt As System.Windows.Forms.Button
Private prefixComboBox As System.Windows.Forms.ComboBox
Private firstBitCheckBox As System.Windows.Forms.CheckBox
Private maxCountHosts As System.Windows.Forms.TextBox
Private calcNetwork As System.Windows.Forms.TextBox
Private nextSubnetButt As System.Windows.Forms.Button
Private calcIPRange As System.Windows.Forms.TextBox
Private clacMaxNetCount As System.Windows.Forms.TextBox
Private nextNumOfHosts As System.Windows.Forms.TextBox
Private exitBtn As System.Windows.Forms.Button
Private pictureBox1 As System.Windows.Forms.PictureBox
''' <summary>
''' Required designer variable.
''' </summary>
Private components As System.ComponentModel.Container = Nothing

Public Sub New()
'
' Required for Windows Form Designer support
'

'
' TODO: Add any constructor code after InitializeComponent call
'
InitializeComponent()
End Sub

''' <summary>
''' Clean up any resources being used.
''' </summary>
Protected Overrides Sub Dispose(disposing As Boolean)
If disposing Then
If components IsNot Nothing Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

#Region "Windows Form Designer generated code"
''' <summary>
''' Required method for Designer support - do not modify
''' the contents of this method with the code editor.
''' </summary>
Private Sub InitializeComponent()
Dim resources As New System.Resources.ResourceManager(GetType(MainForm))
Me.groupBox1 = New System.Windows.Forms.GroupBox()
Me.calcButt = New System.Windows.Forms.Button()
Me.noticeLabel = New System.Windows.Forms.Label()
Me.netClassComboBox = New System.Windows.Forms.ComboBox()
Me.label2 = New System.Windows.Forms.Label()
Me.label1 = New System.Windows.Forms.Label()
Me.groupBox2 = New System.Windows.Forms.GroupBox()
Me.nextNumOfHosts = New System.Windows.Forms.TextBox()
Me.clacMaxNetCount = New System.Windows.Forms.TextBox()
Me.calcIPRange = New System.Windows.Forms.TextBox()
Me.calcNetwork = New System.Windows.Forms.TextBox()
Me.maxCountHosts = New System.Windows.Forms.TextBox()
Me.nextSubnetButt = New System.Windows.Forms.Button()
Me.label11 = New System.Windows.Forms.Label()
Me.label10 = New System.Windows.Forms.Label()
Me.label9 = New System.Windows.Forms.Label()
Me.label8 = New System.Windows.Forms.Label()
Me.label7 = New System.Windows.Forms.Label()
Me.firstBitCheckBox = New System.Windows.Forms.CheckBox()
Me.prefixComboBox = New System.Windows.Forms.ComboBox()
Me.label6 = New System.Windows.Forms.Label()
Me.label5 = New System.Windows.Forms.Label()
Me.label4 = New System.Windows.Forms.Label()
Me.exitBtn = New System.Windows.Forms.Button()
Me.pictureBox1 = New System.Windows.Forms.PictureBox()
Me.groupBox1.SuspendLayout()
Me.groupBox2.SuspendLayout()
Me.SuspendLayout()
'
' groupBox1
'
Me.groupBox1.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.groupBox1.Location = New System.Drawing.Point(8, 8)
Me.groupBox1.Name = "groupBox1"
Me.groupBox1.Size = New System.Drawing.Size(480, 100)
Me.groupBox1.TabIndex = 0
Me.groupBox1.TabStop = False
Me.groupBox1.Text = "IP \ Network Type"
'
' calcButt
'
Me.calcButt.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.calcButt.Location = New System.Drawing.Point(432, 72)
Me.calcButt.Name = "calcButt"
Me.calcButt.Size = New System.Drawing.Size(40, 24)
Me.calcButt.TabIndex = 5
Me.calcButt.Text = "Calc"
'
' noticeLabel
'
Me.noticeLabel.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.noticeLabel.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CByte(238))
Me.noticeLabel.Location = New System.Drawing.Point(8, 56)
Me.noticeLabel.Name = "noticeLabel"
Me.noticeLabel.Size = New System.Drawing.Size(424, 40)
Me.noticeLabel.TabIndex = 4
'
' netClassComboBox
'
Me.netClassComboBox.Items.AddRange(New Object() {"Class A: 0.0.0.0-127.255.255.255", "Class B: 128.0.0.0-191.255.255.255", "Class C: 192.0.0.0-223.255.255.255", "Class D: 224.0.0.0-239.255.255.255", "Class E: 240.0.0.0-254.255.255.255"})
Me.netClassComboBox.Location = New System.Drawing.Point(248, 24)
Me.netClassComboBox.Name = "netClassComboBox"
Me.netClassComboBox.Size = New System.Drawing.Size(208, 21)
Me.netClassComboBox.TabIndex = 3
Me.netClassComboBox.Text = "Class A: 0.0.0.0-127.255.255.255"
'
'
'
' label2
'
Me.label2.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.label2.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CByte(238))
Me.label2.Location = New System.Drawing.Point(168, 24)
Me.label2.Name = "label2"
Me.label2.Size = New System.Drawing.Size(88, 23)
Me.label2.TabIndex = 1
Me.label2.Text = "Network type:"
'
' label1
'
Me.label1.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CByte(238))
Me.label1.Location = New System.Drawing.Point(8, 24)
Me.label1.Name = "label1"
Me.label1.Size = New System.Drawing.Size(24, 23)
Me.label1.TabIndex = 0
Me.label1.Text = "IP:"
'
' groupBox2
'
Me.groupBox2.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.groupBox2.Location = New System.Drawing.Point(8, 120)
Me.groupBox2.Name = "groupBox2"
Me.groupBox2.Size = New System.Drawing.Size(480, 256)
Me.groupBox2.TabIndex = 1
Me.groupBox2.TabStop = False
Me.groupBox2.Text = "Results \ Options"
'
' nextNumOfHosts
'
Me.nextNumOfHosts.Location = New System.Drawing.Point(152, 224)
Me.nextNumOfHosts.Name = "nextNumOfHosts"
Me.nextNumOfHosts.Size = New System.Drawing.Size(80, 20)
Me.nextNumOfHosts.TabIndex = 17
Me.nextNumOfHosts.Text = ""
'
' clacMaxNetCount
'
Me.clacMaxNetCount.Location = New System.Drawing.Point(152, 192)
Me.clacMaxNetCount.Name = "clacMaxNetCount"
Me.clacMaxNetCount.Size = New System.Drawing.Size(320, 20)
Me.clacMaxNetCount.TabIndex = 16
Me.clacMaxNetCount.Text = ""
'
' calcIPRange
'
Me.calcIPRange.Location = New System.Drawing.Point(152, 160)
Me.calcIPRange.Name = "calcIPRange"
Me.calcIPRange.Size = New System.Drawing.Size(320, 20)
Me.calcIPRange.TabIndex = 15
Me.calcIPRange.Text = ""
'
'
'
' calcNetwork
'
Me.calcNetwork.Location = New System.Drawing.Point(152, 96)
Me.calcNetwork.Name = "calcNetwork"
Me.calcNetwork.Size = New System.Drawing.Size(320, 20)
Me.calcNetwork.TabIndex = 13
Me.calcNetwork.Text = ""
'
' maxCountHosts
'
Me.maxCountHosts.Location = New System.Drawing.Point(152, 64)
Me.maxCountHosts.Name = "maxCountHosts"
Me.maxCountHosts.Size = New System.Drawing.Size(320, 20)
Me.maxCountHosts.TabIndex = 12
Me.maxCountHosts.Text = ""
'
' nextSubnetButt
'
Me.nextSubnetButt.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.nextSubnetButt.Location = New System.Drawing.Point(240, 224)
Me.nextSubnetButt.Name = "nextSubnetButt"
Me.nextSubnetButt.Size = New System.Drawing.Size(232, 23)
Me.nextSubnetButt.TabIndex = 11
Me.nextSubnetButt.Text = "Show next subnet with this number of hosts"
'
' label11
'
Me.label11.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.label11.Location = New System.Drawing.Point(8, 224)
Me.label11.Name = "label11"
Me.label11.Size = New System.Drawing.Size(144, 23)
Me.label11.TabIndex = 10
Me.label11.Text = "Wanted number of hosts:"
Me.label11.TextAlign = System.Drawing.ContentAlignment.TopRight
'
' label10
'
Me.label10.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.label10.Location = New System.Drawing.Point(8, 192)
Me.label10.Name = "label10"
Me.label10.Size = New System.Drawing.Size(144, 23)
Me.label10.TabIndex = 9
Me.label10.Text = "Maximum number of subnets:"
Me.label10.TextAlign = System.Drawing.ContentAlignment.TopRight
'
' label9
'
Me.label9.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.label9.Location = New System.Drawing.Point(8, 160)
Me.label9.Name = "label9"
Me.label9.Size = New System.Drawing.Size(144, 23)
Me.label9.TabIndex = 8
Me.label9.Text = "IP - range:"
Me.label9.TextAlign = System.Drawing.ContentAlignment.TopRight
'
' label8
'
Me.label8.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.label8.Location = New System.Drawing.Point(8, 128)
Me.label8.Name = "label8"
Me.label8.Size = New System.Drawing.Size(144, 24)
Me.label8.TabIndex = 7
Me.label8.TextAlign = System.Drawing.ContentAlignment.TopRight
'
' label7
'
Me.label7.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.label7.Location = New System.Drawing.Point(8, 96)
Me.label7.Name = "label7"
Me.label7.Size = New System.Drawing.Size(144, 23)
Me.label7.TabIndex = 6
Me.label7.Text = "Network:"
Me.label7.TextAlign = System.Drawing.ContentAlignment.TopRight
'
' firstBitCheckBox
'
Me.firstBitCheckBox.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.firstBitCheckBox.Location = New System.Drawing.Point(352, 24)
Me.firstBitCheckBox.Name = "firstBitCheckBox"
Me.firstBitCheckBox.Size = New System.Drawing.Size(120, 24)
Me.firstBitCheckBox.TabIndex = 5
Me.firstBitCheckBox.Text = "Allow 1st subnet-BIT"
'
'
'
' prefixComboBox
'
Me.prefixComboBox.Location = New System.Drawing.Point(40, 24)
Me.prefixComboBox.Name = "prefixComboBox"
Me.prefixComboBox.Size = New System.Drawing.Size(48, 21)
Me.prefixComboBox.TabIndex = 3
'
' label6
'
Me.label6.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.label6.Location = New System.Drawing.Point(8, 64)
Me.label6.Name = "label6"
Me.label6.Size = New System.Drawing.Size(144, 23)
Me.label6.TabIndex = 2
Me.label6.Text = "Maximum number of Hosts:"
Me.label6.TextAlign = System.Drawing.ContentAlignment.TopRight
'
' label5
'
Me.label5.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.label5.Location = New System.Drawing.Point(96, 24)
Me.label5.Name = "label5"
Me.label5.Size = New System.Drawing.Size(56, 23)
Me.label5.TabIndex = 1
'
' label4
'
Me.label4.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.label4.Location = New System.Drawing.Point(8, 24)
Me.label4.Name = "label4"
Me.label4.Size = New System.Drawing.Size(32, 23)
Me.label4.TabIndex = 0
Me.label4.Text = "Prefix:"
'
' exitBtn
'
Me.exitBtn.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.exitBtn.Location = New System.Drawing.Point(462, 368)
Me.exitBtn.Name = "exitBtn"
Me.exitBtn.Size = New System.Drawing.Size(32, 16)
Me.exitBtn.TabIndex = 6
Me.exitBtn.Text = "Exit"
'
' pictureBox1
'
Me.pictureBox1.Image = DirectCast(resources.GetObject("pictureBox1.Image"), System.Drawing.Image)
Me.pictureBox1.Location = New System.Drawing.Point(466, 2)
Me.pictureBox1.Name = "pictureBox1"
Me.pictureBox1.Size = New System.Drawing.Size(32, 32)
Me.pictureBox1.TabIndex = 18
Me.pictureBox1.TabStop = False
'
' MainForm
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(496, 386)
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow
Me.Icon = DirectCast(resources.GetObject("\$this.Icon"), System.Drawing.Icon)
Me.Name = "MainForm"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "               ::ZamolxeKnowledgeSoftworks: :MicuRadu: :SubNet Calculator: :V1.0:" & ":"
Me.groupBox1.ResumeLayout(False)
Me.groupBox2.ResumeLayout(False)
Me.ResumeLayout(False)

End Sub
#End Region

''' <summary>
''' The main entry point for the application.
''' </summary>
Private Shared Sub Main()
Application.EnableVisualStyles()
Application.DoEvents()
Application.Run(New MainForm())
End Sub

#Region "Events"
Private Sub ipAddress_Leave(sender As Object, e As System.EventArgs)
newIP()
End Sub

Private Sub ipAddress_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs)
If e.KeyCode = Keys.Enter Then
newIP()
End If
End Sub

Private Sub prefixComboBox_SelectedIndexChanged(sender As Object, e As System.EventArgs)
If ip Is Nothing OrElse ip.getIp() Is Nothing OrElse fill Then
Return
End If
If ip.getNetworkClassPrefix() = -1 Then
Return
End If

Dim prefix As Integer = Int32.Parse(DirectCast(prefixComboBox.SelectedItem, String))
ip.setNetworkPrefix(prefix)

fillIPCalc()
End Sub

Private Sub netMaskComboBox_SelectedIndexChanged(sender As Object, e As System.EventArgs)
If ip Is Nothing OrElse ip.getIp() Is Nothing OrElse fill Then
Return
End If
If ip.getNetworkClassPrefix() = -1 Then
Return
End If

Dim [select] As Integer = netMaskComboBox.SelectedIndex
Dim prefix As Integer = [select] + ip.getNetworkClassPrefix()
If [select] <> 0 AndAlso Not ip.isFirstSubNetBit() Then
prefix += 1
End If
ip.setNetworkPrefix(prefix)

fillIPCalc()
End Sub

Private Sub netClassComboBox_SelectedIndexChanged(sender As Object, e As System.EventArgs)
If fill Then
Return
End If
Select Case netClassComboBox.SelectedIndex
Case 0
Exit Select
Case 1
Exit Select
Case 2
Exit Select
Case 3
Exit Select
Case 4
Exit Select
End Select

newIP()
End Sub

Private Sub firstBitCheckBox_CheckedChanged(sender As Object, e As System.EventArgs)
If ip Is Nothing OrElse ip.getIp() Is Nothing OrElse fill Then
Return
End If

ip.setFirstSubNetBit(firstBitCheckBox.Checked)
fillIPCalc()
End Sub

Private Sub nextSubnetButt_Click(sender As Object, e As System.EventArgs)
If ip Is Nothing OrElse ip.getIp() Is Nothing OrElse fill Then
Return
End If
nextNumOfHosts.BackColor = Color.White

Try
Dim hosts As Integer = Int32.Parse(nextNumOfHosts.Text)

Dim newIp As IPTool = ip.getNextSubNet(hosts)
If newIp Is Nothing Then
'ToManyHosts
noticeLabel.Text = "Info stuff: Number of hosts in this network-class not possible!"
Return
End If
ip = newIp
Catch generatedExceptionName As System.FormatException
nextNumOfHosts.BackColor = Color.Red
Return
Catch e1 As Exception
MessageBox.Show(e1.StackTrace)
Return
End Try

fillIPCalc()
End Sub

Private Sub calcButt_Click(sender As Object, e As System.EventArgs)
newIP()
End Sub
#End Region

#Region "Methods Used"
Protected Sub newIP()
Try
fillIPCalc()
Catch e As Exception
MessageBox.Show(e.StackTrace)
End Try
'catch (UnknownHostException e)
'{
'	main.getJp_Calc().getJtf_IP_Calc().setBackground(java.awt.Color.red);
'}
End Sub

Protected Sub fillIPCalc()
If ip Is Nothing OrElse ip.getIp() Is Nothing Then
Return
End If
Me.fill = True

Me.fillComboBox_Prefix()

' Prefix
Dim prefix As Integer = ip.getNetworkPrefix()
If prefix >= 8 AndAlso prefix <= 30 Then
If prefix <> ip.getNetworkClassPrefix() AndAlso Not ip.isFirstSubNetBit() Then
prefix -= 1
End If
prefix = prefix - ip.getNetworkClassPrefix()
Else
prefix = -1
End If
prefixComboBox.SelectedIndex = prefix

' folgendes nur wenn Netzwerkmaske vorhanden
' nicht der Fall bei Class-D oder E
If prefix <> -1 Then
' max Net Hosts
maxCountHosts.Text = ip.getMaxNetworkHosts() + ""
' Net Class
calcNetwork.Text = ip.getNetworkClassName()
'	Network
calcNetwork.Text = ip.getNetwork()
'	Net-Class
netClassComboBox.SelectedIndex = ip.getNetworkClass()
' IP-Range
Dim ipRange As String() = ip.getNetworkIPRange()
calcIPRange.Text = ipRange(0) & " - " & ipRange(1)
' max Sub-Nets
clacMaxNetCount.Text = ip.getMaxSubNets() + ""
Else
' Anzeige lÃ¶schen
' max Net Hosts
maxCountHosts.Text = ""
' Net Class
calcNetwork.Text = ""
'	Network
calcNetwork.Text = ""
' IP-Range
calcIPRange.Text = ""
' max Sub-Nets
clacMaxNetCount.Text = ""
End If
' Description
Dim desc As String = ""
If ip.get1Byte() >= CByte(64) AndAlso ip.getNetworkClass() = CByte(0) Then
' 64.
desc = "reserved for future use"
End If
If ip.get1Byte() = CByte(127) Then
' localhost
desc = "localhost and loopback-Device - your one computer"
End If
If ip.get1Byte() = CByte(10) Then
' private 10.0.0.0
desc = "private net - for internal use only, would not be routed in internet"
End If
If ip.get1Byte() = CByte(192) AndAlso ip.get2Byte() = CByte(168) Then
' private 192. - 168.
desc = "private net - for internal use only, would not be routed in internet"
End If
If ip.get1Byte() = CByte(172) AndAlso (ip.get2Byte() >= CByte(16) AndAlso ip.get2Byte() <= CByte(31)) Then
'private 172.16 - 172.31
desc = "private net - for internal use only, would not be routed in internet"
End If
If (ip.get1Byte() = CByte(169)) AndAlso (ip.get2Byte() = CByte(254)) Then
' 169.254.
desc = "APIPA Automatic Private IP Addressing"
End If
If ip.get1Byte() = CByte(0) Then
' 0.
desc = "0.0.0.0 = default route"
End If
If ip.getNetworkClass() = CByte(4) Then
' Class-E
desc = "reserved for future use"
End If
If ip.getNetworkClass() = CByte(3) Then
' Class-D
desc = "used for multicast"
End If

noticeLabel.Text = "Info stuff: " & desc
Me.fill = False
End Sub

Protected Sub fillComboBox_Prefix()
If ip Is Nothing OrElse ip.getIp() Is Nothing Then
Return
End If

prefixComboBox.Items.Clear()

If ip.getNetworkClassPrefix() = -1 Then
Return
End If

Dim startPrefix As Integer = ip.getNetworkClassPrefix()
' first is Networkclass
startPrefix += 1
If Not ip.isFirstSubNetBit() Then
startPrefix += 1
End If

For i As Integer = startPrefix To 30
Next
End Sub
If ip Is Nothing OrElse ip.getIp() Is Nothing Then
Return
End If

If ip.getNetworkClassPrefix() = -1 Then
Return
End If

Dim startPrefix As Integer = ip.getNetworkClassPrefix()
' first is Networkclass
startPrefix += 1
If Not ip.isFirstSubNetBit() Then
startPrefix += 1
End If

For i As Integer = startPrefix To 30
Next
End Sub
#End Region

Private Sub exitBtn_Click(sender As Object, e As System.EventArgs)
Application.[Exit]()
End Sub
End Class
End Namespace
``````
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)

Commented:
graye, your code looks very interesting but im getting this errors with it

``````Error	1	Type 'StructLayout' is not defined.
Error	2	Type 'FieldOffset' is not defined.
Error	3	Type 'FieldOffset' is not defined.
Error	4	Type 'FieldOffset' is not defined.
Error	5	Type 'FieldOffset' is not defined.
Error	6	Type 'FieldOffset' is not defined.
Error	7	Type 'IPAddress' is not defined.
Error	8	Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class.
Error	9	Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class.
``````

Commented:
So add the following import directives
Imports System.Net
Imports System.Runtime.InteropServices

Commented:
alright now the erros are

Error      1      'None' is not a member of 'System.Windows.Forms.ColumnHeader'.
Error      2      'TryParse' is not a member of 'System.Windows.Forms.ColumnHeader'.

Commented:
its weird because when I put ur code inside a module this is what I get

Error      1      Argument not specified for parameter 'strHost' of 'Public Function IPAddress(strHost As String) As Object'.
Error      2      Argument not specified for parameter 'strHost' of 'Public Function IPAddress(strHost As String) As Object'.

Commented:
TryParse is a part of version 2.0 (and beyond) of the .Net framework.
So, to make this code compatible for v1.0 or v1.1, change:
Return 0
End If

To:

Commented:
im using net framework v4

Commented:
this is what I get just with

Error      1      Argument not specified for parameter 'strHost' of 'Public Function IPAddress(strHost As String) As Object'.

Commented:
OK... you've apparently named your function "IPAddress" which is in conflict with the IPAddress class from the framework.   I'd suggest that you rename your function to something else

Commented:
I havent touch your code at all, its having issues because of the "IPAddress" from here

>>OK... you've apparently named your function "IPAddress" which is in conflict with the IPAddress class from the framework.
>>I'd suggest that you rename your function to something else

Commented:
maybe you forgot to include the "parse" function?

Commented:
Do you have something named IPAddress?  I am pretty sure that what graye was referring to was System.Net.IPAddress, which is why he said to do the imports of System.Net.
In your code, right click on the [IPAddress] and select Find all References or Go to Definition.  The Go To Definiton should take you to the Object Browser and to the System.Net.IPAddress class.  If not, then let us know where it does take you...
Unlock the solution to this question.

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.