Daniel Wong
Daniel Wong

Reputation: 157

Allowing TextBox.Text to accept different types

I am translating a VB program to C#. The VB program has a WinForm control and in the settings portion, the following code is used,

Me.xXXTextBox.Text = Me._settings.XXX

The only thing is that the RHS of the above is not a string but,

Public Property XXX() As Single
    Get
        Return Me._xXX
    End Get
    Set(ByVal value As Single)
        If value <> Me._xXX Then
            Me._xXX = value
            Me._dirty = True
        End If
    End Set
End Property

How do I do the above in C# please? I have translated the code into C# but I cannot get past the type "mis-match".

Just for more background, in the VB program, the TextBox is implemented as a NumericTextBox which inherits from TextBox. I have implemented this in C# too but still no go.

I am missing something but I can't figure out what it is.

Upvotes: 0

Views: 118

Answers (2)

Zohar Peled
Zohar Peled

Reputation: 82524

VB often allows implicit conversions that c# will not allow, therefor in the first glance I would recommend using _settings.XXX.ToString().

However, after reading the last paragraph in your question, I'm not so sure. You need to check the VB source to see if the NumericTextBox.Text should be of type String or Single. If it's a single, you need to implement the same in your c# translation. If it's a string, you need to use the ToString method.

Upvotes: 1

Patrick Hofman
Patrick Hofman

Reputation: 157078

Just convert the single to a string. C# is not that easy on explicit type casting as VB.NET (you should be happy about that):

this.xXXTextBox.Text = Convert.ToString(this._settings.XXX)

Or

this.xXXTextBox.Text = this._settings.XXX.ToString()

Upvotes: 0

Related Questions