0

We had the need to create the following object, basically a way to pass in integer or strings and fetch these back out as either an integer or a zero-padded string:

Public Class DOC_NUMBER
    Private m_DOC_NUMBER As Integer = 0

    Public Sub New(ByVal DOC_NUMBER As Integer)
        m_DOC_NUMBER = DOC_NUMBER
    End Sub

    Public Sub New(ByVal DEPOT_CODE As String)
        Dim ParseInput As Integer = 0
        If Integer.TryParse(DEPOT_CODE, ParseInput) = False Then
            m_DOC_NUMBER = 0
        Else
            m_DOC_NUMBER = ParseInput
        End If
    End Sub

    Public Overrides Function ToString() As String
        Return Right("0000000000" & m_DOC_NUMBER.ToString(), 10)
    End Function

    Public Function ToInteger() As Integer
        Return m_DOC_NUMBER
    End Function
End Class

An instance of this class can then be set as follows:

Dim DocumentID As New DOC_NUMBER("00123")
Dim DocumentID As New DOC_NUMBER(123)

This can then be assigned to other vars like this:

Dim NewDocumentID as Integer = DocumentID.ToInteger()
Dim NewDocumentID as String = DocumentID.ToString()

99% of the time however we'll be dealing with integers, so we'd like to make the .ToInteger() optional, for example:

Dim NewDocumentID as Integer = DocumentID

Obviously this currently gives the error "Value of type 'X.DOC_NUMBER' cannot be converted to 'Integer'"

How do we modify the class to automatically pass out an integer it's being assigned to an integer?

Examples in any .net language will be fine.

HeavenCore
  • 7,533
  • 6
  • 47
  • 62
  • So you want a function that will *sometimes* return an integer and *sometimes* return a string? I don't think any statically typed language will do that. – David Aug 01 '13 at 15:01
  • I'm still googling but i think i'm after implicit conversion? – HeavenCore Aug 01 '13 at 15:03
  • This is an aside, so I'll make it a comment: the way you're zero-padding your string is weird. Other ways: http://stackoverflow.com/questions/489466/pad-a-number-with-starting-zero-in-net (I'd use `m_DOC_NUMBER.ToString().PadLeft(10, '0')`). – Tim S. Aug 01 '13 at 15:10
  • 90% of the code i write is in SQL hence the use of Right() - but cheers for the tip – HeavenCore Aug 01 '13 at 15:13
  • @HeavenCore Why is this tagged as a C# question when the supplied code is VB.NET? – Security Hound Aug 01 '13 at 15:51
  • @Ramhound Because its just as much a C# question as it is a VB question? The c# answer was perfect? – HeavenCore Aug 01 '13 at 15:55

1 Answers1

4

You can do what you want with implicit conversions. In C# that might look like this:

public static implicit operator string(DOC_NUMBER docNum)
{
    // implement
}
public static implicit operator int(DOC_NUMBER docNum)
{
    // implement
}

The VB.NET equivalent is the Widening Operator.

However, I'll point out that I find ToInteger() and ToString() (or as properties: AsInteger and AsString, perhaps) much clearer and simpler, and therefore better, than implicit conversions.

Tim S.
  • 55,448
  • 7
  • 96
  • 122