473,378 Members | 1,457 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,378 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 4501
* "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),...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.