even length strings.

The application I am writing has a data structure called a UID.
A UID must be even in length.
If odd a trailing null must be added.
Essentially the class has one property which is a string.

This is a pretty simple class until you need to start comparing UIDS.

Private Sub Constructor(v as String) dim l as integer l = v.Len UID = v if (l mod 2 <> 0) then UID = UID + chr(0) end if End Sub

This is the issue is that sometimes the data is a string and sometimes it’s a UID.
Sometimes the string has the tailing null sometimes it doesn’t.

dim x as new UID("ABC") dim y as string = "ABC" dim z as string = "ABC"+chr(0) if x = y then ... end if if x = z then ... end if

The data comes from a binary stream and should have the null when read.
If the string is odd and is written to the stream then an additional null has to be written.

I could simply maintain a regular string that is null free but then I have no control over the users strings and if they do or do not have trailing nulls.

I thought of implementing operator compare and convert but this all sounds like overkill.

Operator compare and convert is exactly what you need.

Convert seems easy… Take a string and add a null if odd, or return as string without null at the end.
but compare is my issue…

Two versions of Compare… one against another UID and another against string. Within the same class, functions of object 1 can access protected properties of object 2.

Post what you have so far and we can revise.

(BTW, if the null is required, I’m not sure why you would strip it when converting to a string. Wouldn’t you want it written just that way to, for example, a file?)

This data gets stored to a file and the structure is such that it must be padded to be rounded to the nearest even length.
but some apps strip it while others leave it and making comparisons gets difficult.
Will post hang on.

[code]Class MyUID

Private UID as string

Private Sub Constructor(v as String)
fromString(v)
End Sub

Function Operator_Compare(v as String) As Integer
dim s as string
s = self
s = trim(s)
v = trim(v)
if s > v then return 1
if s = v then return 0
if s < v then return -1
End Function

Private Sub fromString(v as String)
dim l as integer
l = v.Len
UID = v
if (l mod 2 <> 0) then
UID = UID + chr(0)
end if
End Sub

End Class
[/code]

How about this: instead of comparing to a String, use Operator_Convert(s As String), then Operator_Compare(other As UID). When comparing to a string, Xojo will automatically convert to a UID for you, which will pad the string if needed, and then it would be a simple StrComp( self.UID, other.UID ).

Lot’s of string copying going on…

[code]Private Sub fromString(v as String)
dim l as integer
l = v.Len
UID = v
if (l mod 2 <> 0) then
UID = UID + chr(0)
end if
End Sub

Sub Operator_Convert() as String
return UID.Trim
End Sub

Sub Operator_Convert(v As String)
fromString(v)
End Sub

Function Operator_Compare(_uid as UID) As Integer
return StrComp(self.UID, _uid.UID, 1)
End Function[/code]