472,093 Members | 2,500 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,093 software developers and data experts.

Convert string to Structure

I have a strinf with comma separated field. Is is possible to convert it to
a Structure ?
Thanks
Nov 20 '05 #1
8 4341
* "Carlos" <cp****@yahoo.com> scripsit:
I have a strinf with comma separated field. Is is possible to convert it to
a Structure ?


Please provide an example.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #2
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 ?
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:Oa**************@TK2MSFTNGP11.phx.gbl...
* "Carlos" <cp****@yahoo.com> scripsit:
I have a strinf with comma separated field. Is is possible to convert it to a Structure ?


Please provide an example.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>

Nov 20 '05 #3
In article <#A**************@TK2MSFTNGP11.phx.gbl>, Carlos wrote:
I have a strinf with comma separated field. Is is possible to convert it to
a Structure ?
Thanks


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
Nov 20 '05 #4
* "Carlos" <cp****@yahoo.com> scripsit:
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 ?


You will have to do that by hand because the order of the structure's
members isn't stored anywhere.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #5
Thanks, that works great !!!
"Tom Shelton" <to*@mtogden.com> wrote in message
news:ed*************@tk2msftngp13.phx.gbl...
In article <#A**************@TK2MSFTNGP11.phx.gbl>, Carlos wrote:
I have a strinf with comma separated field. Is is possible to convert it to a Structure ?
Thanks


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

Nov 20 '05 #6
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:
Structure MinuteRecord 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
End Structure
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.

Hope this helps
Jay

"Carlos" <cp****@yahoo.com> wrote in message
news:em***************@TK2MSFTNGP12.phx.gbl... 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 ?
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:Oa**************@TK2MSFTNGP11.phx.gbl...
* "Carlos" <cp****@yahoo.com> scripsit:
I have a strinf with comma separated field. Is is possible to convert it to a Structure ?


Please provide an example.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>


Nov 20 '05 #7
In article <#9**************@TK2MSFTNGP10.phx.gbl>, Jay B. Harlow [MVP - Outlook] wrote:
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:
Structure MinuteRecord

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
End Structure


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.


That does seem to be the way the framework does it... Yet, it always
seems more practicle to just do:

C#:
RecordType record = new RecordType(stringvalue);

I suppose for sake of consistency, I'll have to mend my evil ways :)

--
Tom Shelton [MVP]
OS Name: Microsoft Windows XP Professional
OS Version: 5.1.2600 Service Pack 1 Build 2600
System Up Time: 1 Days, 0 Hours, 9 Minutes, 42 Seconds
Nov 20 '05 #8
Tom,
That does seem to be the way the framework does it... Yet, it always
seems more practicle to just do: I've also seen From*Type* in a handful of cases, but its rare...
(SystemBrushes.FromSystemColor for example).

C#:
RecordType record = new RecordType(stringvalue);
Your method has its usefulness. My concern is one of "containment", I would
normally expect the stringvalue to be a single part of the structure, such
as Name, rather then be Parsed into many parts of the structure.
I suppose for sake of consistency, I'll have to mend my evil ways :) Unless you are writing libraries to be consumed by others, I don't have a
problem with your ways ;-)

Jay

"Tom Shelton" <to*@mtogden.com> wrote in message
news:Ot**************@TK2MSFTNGP11.phx.gbl... In article <#9**************@TK2MSFTNGP10.phx.gbl>, Jay B. Harlow [MVP - Outlook] wrote:
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:
Structure MinuteRecord

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
End Structure


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.
That does seem to be the way the framework does it... Yet, it always
seems more practicle to just do:

C#:
RecordType record = new RecordType(stringvalue);

I suppose for sake of consistency, I'll have to mend my evil ways :)

--
Tom Shelton [MVP]
OS Name: Microsoft Windows XP Professional
OS Version: 5.1.2600 Service Pack 1 Build 2600
System Up Time: 1 Days, 0 Hours, 9 Minutes, 42 Seconds

Nov 20 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Mel WEaver | last post: by
3 posts views Thread by Gee | last post: by
8 posts views Thread by Ken Dopierala Jr. | last post: by
4 posts views Thread by Neal Becker | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.