Link to home
Start Free TrialLog in
Avatar of bromy2004
bromy2004Flag for Australia

asked on

IP Addresses in excel VBA

I'm trying to create a class in Excel VBA for IP Addresses.
I've got the absolute basics as i've never needed classes before.
Using help from CPearson's website http://www.cpearson.com/excel/classes.aspx
I've put together the class basics attached.
IPaddress.Point1 to Point4

What i'm now trying to do is "add" to an IP address.
i.e. 10.0.0.128 + 0.0.0.128 = 10.0.1.0

But i'm struggling.
Is there any classes out there?
is there any other suggestions on how i can handle this?
Option Explicit

Private pPoint1 As Byte
Private pPoint2 As Byte
Private pPoint3 As Byte
Private pPoint4 As Byte

'''''''''
'Point 1'
Public Property Get Point1() As Byte
Point1 = pPoint1
End Property
Public Property Let Point1(Value As Byte)
pPoint1 = Value
End Property

'''''''''
'Point 2'
Public Property Get Point2() As Byte
Point2 = pPoint2
End Property
Public Property Let Point2(Value As Byte)
pPoint2 = Value
End Property

'''''''''
'Point 3'
Public Property Get Point3() As Byte
Point3 = pPoint3
End Property
Public Property Let Point3(Value As Byte)
pPoint3 = Value
End Property

'''''''''
'Point 4'
Public Property Get Point4() As Byte
Point4 = pPoint4
End Property
Public Property Let Point4(Value As Byte)
pPoint4 = Value
End Property

'To String
Public Property Get ToString() As String
ToString = pPoint1 & "." & pPoint2 & "." & pPoint3 & "." & pPoint4
End Property

'Convert String to IP
Public Property Let StrToIP(Value As String)
Dim str() As String
str = Split(Value, ".")
If UBound(str) = 3 And LBound(str) = 0 Then
  pPoint1 = str(0)
  pPoint2 = str(1)
  pPoint3 = str(2)
  pPoint4 = str(3)
Else
  pPoint1 = 0
  pPoint2 = 0
  pPoint3 = 0
  pPoint4 = 0
End If
End Property

Open in new window

Avatar of stranger9002
stranger9002
Flag of Czechia image

Could it be something like this? :

If ((pPoint4a+pPoint4b)\256)>1 Then
 pPoint4c=(pPoint4a+pPoint4b) mod 256
 pPoint3c=1

Else
 pPoint4c=(pPoint4a+pPoint4b)
 pPoint3c=0

If ((pPoint3a+pPoint3b)\256)>1 Then
 pPoint3c=pPoint3c+((pPoint3a+pPoint3b) mod 256)
 pPoint2c=1
Else
 pPoint3c=pPoint3a+pPoint3b
 pPoint2c=0
...
...
...

Still needs to continue for pPoint2 and pPoint1, but I think the main idea is there.
//a+b=c
//  "\"means the integer part from division
http://msdn.microsoft.com/en-us/library/0e16fywh%28v=VS.90%29.aspx
// Mod means the reminder
http://msdn.microsoft.com/en-us/library/se0w9esz%28v=VS.90%29.aspx
Avatar of zorvek (Kevin Jones)
When creating classes, one should always consider what object the class represents and how that class will support the handling of that object. In this case it looks like you are not using the class as an object but more as a function. If so, then a couple of functions would probably be more appropriate and easier to create and maintain.

Is there any reason why you can't use two functions?

GetStringFromPoints(P1 As Byte, P2 As Byte, P3 As Byte, P4 As Byte) As String
GetPointsFromString(IPAddress As String) As Variant

Kevin
Avatar of bromy2004

ASKER

I've solved my problem from another approach,
I'll be deleting this question.
Kevin,
I normally would but i completely skipped the class.
I ended up looking from 1 to 255 Step LevelStep
and various other bits and pieces.
That's OK. That is still a solution to the question "How can I do this with a class". The answer is "you really don't need a class." I'm also curious to see how you solved it.

Kevin
ASKER CERTIFIED SOLUTION
Avatar of bromy2004
bromy2004
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial