C
Carlos
I have a strinf with comma separated field. Is is possible to convert it to
a Structure ?
Thanks
a Structure ?
Thanks
* "Carlos said:I have a strinf with comma separated field. Is is possible to convert it to
a Structure ?
* "Carlos said:My string :
1, 28, 2004, 23, 59, 6.214, 7.23, 193.9, 26.67, 19.62, 79.7, 0,
.117,-99,-99,-99,-99, 6999, 6999
want to convert to
Structure MinuteRecord
Dim mmonth As Integer
Dim mday As Integer
Dim myear As Integer
Dim mhour As Integer
Dim mminute As Integer
Dim WS As Single
Dim wsmax As Single
Dim WD As Single
Dim steeltemp As Single
Dim airTemp As Single
Dim RH As Single
Dim rain As Single
Dim radiation As Single
Dim ram1 As Single
Dim ram1max As Single
Dim ram2 As Single
Dim ram2max As Single
Dim ram3 As Single
Dim ram3max As Single
End Structure
is that possible ?
I have a strinf with comma separated field. Is is possible to convert it to
a Structure ?
Thanks
Tom Shelton said:private structure RecordType
public field1 as string
public field2 as string
public field3 as string
public sub new(byval record as string)
dim fields() as string = record.split(","c)
field1 = fields(0)
field2 = fields(1)
field3 = fields(3)
end sub
End structure
Dim record as new RecordType("a,b,c")
console.writeline(record.field1)
console.writeline(record.field2)
console.writeline(record.field3)
--
Tom Shelton [MVP]
OS Name: Microsoft Windows XP Professional
OS Version: 5.1.2600 Service Pack 1 Build 2600
System Up Time: 0 Days, 23 Hours, 39 Minutes, 42 Seconds
Public Shared Function Parse(ByVal input As String) As MinuteRecordStructure MinuteRecord
End Structure
Carlos said:My string :
1, 28, 2004, 23, 59, 6.214, 7.23, 193.9, 26.67, 19.62, 79.7, 0,
.117,-99,-99,-99,-99, 6999, 6999
want to convert to
Structure MinuteRecord
Dim mmonth As Integer
Dim mday As Integer
Dim myear As Integer
Dim mhour As Integer
Dim mminute As Integer
Dim WS As Single
Dim wsmax As Single
Dim WD As Single
Dim steeltemp As Single
Dim airTemp As Single
Dim RH As Single
Dim rain As Single
Dim radiation As Single
Dim ram1 As Single
Dim ram1max As Single
Dim ram2 As Single
Dim ram2max As Single
Dim ram3 As Single
Dim ram3max As Single
End Structure
is that possible ?
Carlos,
There is no built-in magic to do what you want per se.
The normal convention is to add a Shared Function called Parse to your
structure that returns a new instance of your Structure.
Something like:
Public Shared Function Parse(ByVal input As String) As MinuteRecord
Dim values() As String = input.Split(","c)
Dim rc As New MinuteRecord
With rc
mmonth = Integer.Parse(values(0))
mday = Integer.Parse(values(1))
myear = Integer.Parse(values(2))
...
WS = Single.Parse(values(...))
...
End With
return rc
End Function
I will sometimes implement MinuteRecord.Parse in terms of the Constructor
that Tom showed, however in this case I will make the constructor Private,
especially when the constructor that accepts a string is only used by Parse.
I've also seen From*Type* in a handful of cases, but its rare...That does seem to be the way the framework does it... Yet, it always
seems more practicle to just do:
Your method has its usefulness. My concern is one of "containment", I wouldC#:
RecordType record = new RecordType(stringvalue);
Unless you are writing libraries to be consumed by others, I don't have aI suppose for sake of consistency, I'll have to mend my evil ways