473,609 Members | 1,871 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Parse Array

Hello,

I have the following class ( Level is just a simple enumeration ):

public class Theme {
public Subject Subject { get; set; }
public List<LevelLevel s { get; set; }
public string Note { get; set; }
}

And MyThemes is a List<Theme>

I am filling "public string[] Themes" from a form and getting:

Themes[0] = Economy|Base,Su perior|Note 1
Themes[1] = Math|Base|Note 2

Now I need to fill MyThemes with these values:

for (int i = 0; i < this.Themes.Len gth; i++) {

this.Themes = this.Themes[i].Split(new char[] { '|' },
StringSplitOpti ons.RemoveEmpty Entries).Select (t =new Theme { Subject
= ???, Levels = ???, Note = ??? }).ToList();

}

I am a little bit confused about this.

For each Theme, Theme[i] I split it using "|" and then:
- First value is Subject
- Second value are the Levels in a CSV format (needs to be converted
to a list)
- Third value is the note

Can't I integrate Linq and Split to get this instead of using more
loops?

Thanks,
Miguel

Oct 30 '08 #1
2 3548
shapper wrote:
Hello,

I have the following class ( Level is just a simple enumeration ):

public class Theme {
public Subject Subject { get; set; }
public List<LevelLevel s { get; set; }
public string Note { get; set; }
}

And MyThemes is a List<Theme>

I am filling "public string[] Themes" from a form and getting:

Themes[0] = Economy|Base,Su perior|Note 1
Themes[1] = Math|Base|Note 2

Now I need to fill MyThemes with these values:

for (int i = 0; i < this.Themes.Len gth; i++) {

this.Themes = this.Themes[i].Split(new char[] { '|' },
StringSplitOpti ons.RemoveEmpty Entries).Select (t =new Theme { Subject
= ???, Levels = ???, Note = ??? }).ToList();

}

I am a little bit confused about this.

For each Theme, Theme[i] I split it using "|" and then:
- First value is Subject
- Second value are the Levels in a CSV format (needs to be converted
to a list)
- Third value is the note

Can't I integrate Linq and Split to get this instead of using more
loops?

Thanks,
Miguel
You can't put a list of Theme objects in a string array. Besides, you
are making a list containing a single object for every iteration of the
list, so you would overwrite the previous item each time.

Make a constructor for the Theme class that takes a string, then you can
use LINQ to create a list of objects from the string array.

Something like:

public class Theme {
public Subject Subject { get; private set; }
public List<LevelLevel s { get; private set; }
public string Note { get; private set; }

public Theme(string data) {
string[] items = data.Split('|') ;
Subject = Enum.Parse(type of(Subject), items[0]);
Levels = new List<Level>();
foreach (string s in items[1].Split(',')) {
Levels.Add(Enum .Parse(typeof(L evel), s));
}
Note = items[2];
}

}

List<Theme= this.Themes.Sel ect(t =new Theme(t)).ToLis t();

--
Göran Andersson
_____
http://www.guffa.com
Oct 30 '08 #2
On Oct 30, 2:30*pm, Göran Andersson <gu...@guffa.co mwrote:
shapper wrote:
Hello,
I have the following class ( Level is just a simple enumeration ):
* public class Theme {
* * public Subject Subject { get; set; }
* * public List<LevelLevel s { get; set; }
* * public string Note { get; set; }
* }
And MyThemes is a List<Theme>
I am filling "public string[] Themes" from a form and getting:
Themes[0] = Economy|Base,Su perior|Note 1
Themes[1] = Math|Base|Note 2
Now I need to fill MyThemes with these values:
* *for (int i = 0; i < this.Themes.Len gth; i++) {
* *this.Themes = this.Themes[i].Split(new char[] { '|' },
StringSplitOpti ons.RemoveEmpty Entries).Select (t =new Theme { Subject
= ???, Levels = ???, Note = ??? }).ToList();
* *}
I am a little bit confused about this.
For each Theme, Theme[i] I split it using "|" and then:
- First value is Subject
- Second value are the Levels in a CSV format (needs to be converted
to a list)
- Third value is the note
Can't I integrate Linq and Split to get this instead of using more
loops?
Thanks,
Miguel

You can't put a list of Theme objects in a string array. Besides, you
are making a list containing a single object for every iteration of the
list, so you would overwrite the previous item each time.

Make a constructor for the Theme class that takes a string, then you can
use LINQ to create a list of objects from the string array.

Something like:

public class Theme {
* * public Subject Subject { get; private set; }
* * public List<LevelLevel s { get; private set; }
* * public string Note { get; private set; }

* * public Theme(string data) {
* * * *string[] items = data.Split('|') ;
* * * *Subject = Enum.Parse(type of(Subject), items[0]);
* * * *Levels = new List<Level>();
* * * *foreach (string s in items[1].Split(',')) {
* * * * * Levels.Add(Enum .Parse(typeof(L evel), s));
* * * *}
* * * *Note = items[2];
* * }

}

List<Theme= this.Themes.Sel ect(t =new Theme(t)).ToLis t();

--
Göran Andersson
_____http://www.guffa.com
Thanks! It worked fine ... just needed some casting:
Subject = (Subject)Enum.P arse(typeof(Sub ject), items[0]);
and
Levels.Add((Lev el)Enum.Parse(t ypeof(Level), s));
Oct 31 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
10677
by: iop | last post by:
Hello there, I'd like to "parse" an entire multi-dimension array like this : APP APP without knowing "framework" or "config" or anything passed as variables... 'cause it's simple to call APP.length My goal is to simply write a xml file out a javascript array... Thanks for any help !!
24
3154
by: | last post by:
Hi, I need to read a big CSV file, where different fields should be converted to different types, such as int, double, datetime, SqlMoney, etc. I have an array, which describes the fields and their types. I would like to somehow store a reference to parsing operations in this array (such as Int32.Parse, Double.Parse, SqlMoney.Parse, etc), so I can invoke the appropriate one without writing a long switch.
9
5693
by: Danny | last post by:
HI again Is there a nifty function in access that will: 1. return the amount of occurances of a small string within a larger string? this<br>is<br>a<br>test would return 3 for <br>
7
6615
by: memememe | last post by:
I need to be able to parse both of these dates with the same method, and return them as a DateTime object. Is there any methods that would do this blindly or do I need to provide the format? Feb 25 23:11 Dec 1 2002
18
2224
by: DaveLessnau | last post by:
I'm trying to learn C on my own and, apparently, my brain went on vacation somewhere. I just can't figure out how to parse the following function call: "void fillDeck( Card * const wDeck, const char * wFace, const char * wSuit)" Card is aliased before that call with: "typedef struct card Card"
9
14090
by: Python.LeoJay | last post by:
Dear all, i need to parse billions of numbers from a file into float numbers for further calculation. i'm not satisfied with the speed of atof() function on my machine(i'm using visual c++ 6). so i wonder is there any faster way to parse string(char array) into float number.
3
1924
by: deerhide | last post by:
Hi, I was updating my website, well trying to... and I somehow messed it up. I didnt build it, I bought it so I don't know alot about programming. I receive these errors when going to minbiketrader.com : Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /home/content/m/i/n/minibiketrader/html/index.php on line 2 Parse error: parse error, unexpected T_REQUIRE_ONCE in /home/content/m/i/n/minibiketrader/html/index.php on line...
5
64615
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C++ programming. FYI Although I have called this article “How to Parse a File in C++”, we are actually mostly lexing a file which is the breaking down of a stream in to its component parts, disregarding the syntax that stream contains. Parsing is actually including the syntax in order to make...
5
2184
by: Joshr35 | last post by:
I keep getting this same error on my store front page login area. Parse error: syntax error, unexpected '{' Here's the actuall php script: <?php if ( (!strstr($_SERVER,'login.php')) and (!strstr($_SERVER,'create_account.php'))
0
8130
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
8076
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
8541
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...
0
8406
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
7002
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
4021
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1672
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1389
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.