473,230 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,230 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 4484
* "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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
2
by: Mel WEaver | last post by:
Hello, I have the following delphi structure for a binary file. I'm looking for idea how to read this file. Mel type TMenuDataStruct = packed record exename : string;
3
by: Gee | last post by:
Hi Can anyone help me convert this to C# please? Structure NETRESOURCE Public dwScope As Int32 Public dwType As Int32 Public dwDisplayType As Int32 Public dwUsage As Int32 Public...
8
by: Ken Dopierala Jr. | last post by:
Hi, I'm reading the header file of a PCX image and I need to convert 2 bytes to a short. How would I go about doing this? I know that they are bytes 8 & 9 in my byte array. I'm not sure how to...
3
by: Dan | last post by:
Good Day All, I have the following VB6 Type Type IAListRecordType lID As Long strName As String * IALISTSTRSIZE End Type I need to convert this to VB .NET. How do I do this? I have come...
6
by: Eric | last post by:
.... my eternal gratitude!!! :p Here is the problem. A sample of my original VB6 code : '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Public...
1
by: Peter | last post by:
Does anybody know how to convert a string to a varible? For example, I have defined 2 structures Structure SS dim Name as string dim ID as short end structure Structure TT
7
by: manstey | last post by:
Hi, How do I convert a string like: a="{'syllable': u'cv-i b.v^ y^-f', 'ketiv-qere': 'n', 'wordWTS': u'8'}" into a dictionary: b={'syllable': u'cv-i b.v^ y^-f', 'ketiv-qere': 'n', 'wordWTS':...
4
by: Neal Becker | last post by:
In an earlier post, I was interested in passing a pointer to a structure to fcntl.ioctl. This works: c = create_string_buffer (...) args = struct.pack("iP", len(c), cast (pointer (c),...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.