Structure Assignment

  • Thread starter Thread starter Vadim Rapp
  • Start date Start date
V

Vadim Rapp

Hello:

Is there a way to assign a structure from the result of <structure>.ToString
in one statement?

For instance, if such function existed, it might look like this:

Me.Size = OurFunction(Me.Size.Tostring)


thanks,

Vadim
 
* "Vadim Rapp said:
Is there a way to assign a structure from the result of
<structure>.ToString in one statement?


For instance, if such function existed, it might look like this:

Me.Size = OurFunction(Me.Size.Tostring)

There is no general way to do that. You can look if the type has a
'Parse' method and then use this method to construct an object of the
type from the data contained in the string.
 
Vadim,
Have you tried:

Public Function OurFunction(ByVal s As String) As Size
' magic to convert s to a Size.
End Function

Note: I would normally create "OurFunction" as a Shared function called
Parse:

Public Structure MyStruct

Private ReadOnly m_value As Integer

Public Sub New(ByVal value As Integer)
m_value = value
End Sub

Public Overrides Function ToString() As String
Return m_value.ToString()
End Function

Public Shared Function Parse(ByVal s As String) As MyStruct
Return New MyStruct(Integer.Parse(s))
End Function

End Structure

Note: I would consider using String.Split to parse the input string to the
Parse function.

Hope this helps
Jay
 
Vadim,
I should add that normally you do not need to convert structures to strings
to assign them to structure variables, you can simply assign one structure
to another

Dim frm1 As Form
Dim frm2 As Form
frm1.Size = frm2.Size

Hope this helps
Jay
 
Back
Top