473,770 Members | 1,787 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4547
* "Carlos" <cp****@yahoo.c om> 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******** ******@TK2MSFTN GP11.phx.gbl...
* "Carlos" <cp****@yahoo.c om> 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.writeli ne(record.field 1)
console.writeli ne(record.field 2)
console.writeli ne(record.field 3)
--
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.c om> 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.co m> wrote in message
news:ed******** *****@tk2msftng p13.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.writeli ne(record.field 1)
console.writeli ne(record.field 2)
console.writeli ne(record.field 3)
--
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(v alues(0))
mday = Integer.Parse(v alues(1))
myear = Integer.Parse(v alues(2))
...
WS = Single.Parse(va lues(...))
...
End With
return rc
End Function
End Structure
I will sometimes implement MinuteRecord.Pa rse 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.c om> wrote in message
news:em******** *******@TK2MSFT NGP12.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******** ******@TK2MSFTN GP11.phx.gbl...
* "Carlos" <cp****@yahoo.c om> 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(v alues(0))
mday = Integer.Parse(v alues(1))
myear = Integer.Parse(v alues(2))
...
WS = Single.Parse(va lues(...))
...
End With
return rc
End Function
End Structure


I will sometimes implement MinuteRecord.Pa rse 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(stri ngvalue);

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(stri ngvalue);
Your method has its usefulness. My concern is one of "containmen t", 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.co m> wrote in message
news:Ot******** ******@TK2MSFTN GP11.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(v alues(0))
mday = Integer.Parse(v alues(1))
myear = Integer.Parse(v alues(2))
...
WS = Single.Parse(va lues(...))
...
End With
return rc
End Function
End Structure


I will sometimes implement MinuteRecord.Pa rse 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(stri ngvalue);

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
9768
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 numeric value according to an arbitrary regular expression.
2
12198
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
4752
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 lpLocalName As String
8
16396
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 take those two and convert them into a short though. In C I would just use a union and assign them accordingly. Thanks! Ken.
3
3367
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 up with
6
4258
by: Eric | last post by:
.... my eternal gratitude!!! :p Here is the problem. A sample of my original VB6 code : '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Public Type INFO name(1 To 3) As String * 41 address As String * 41 End Typ ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
1
1147
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
3146
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': u'8'} Thanks, Matthew
4
8508
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), c_void_p).value) err = fcntl.ioctl(eos_fd, request, args) Now to do the same with ctypes, I have one problem.
0
9617
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10099
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10037
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9904
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8931
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.