Solved

How do I change the text on an object property of a generic list of objects?

Posted on 2009-04-10
6
197 Views
Last Modified: 2012-05-06
If Ive got a generic list of objects and I want to change the text on one of those objects properties, how do I do it?
 
Gary Davis
class Prod
        {
            public Prod()
            {
            }
 
            public int Id { get; set; }
            public string Title { get; set; }
        }
 
            var prd = new List<Prod>();
            var prd1 = new Prod {Id = 1, Title = "MY TITLE"};
            var prd2 = new Prod { Id = 2, Title = "SOME THING" };
            prd.Add(prd1);
            prd.Add(prd2);
 
  //this doesn't work - i assume because the lambda is trying to change the object, not the property
 
            prd.ForEach(x => x.Title.ToLower()); 
 
            foreach (var p in prd)
                Console.WriteLine(string.Format("id={0} title={1}", p.Id, p.Title));

Open in new window

0
Comment
Question by:Gary Davis
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 22

Expert Comment

by:p_davis
ID: 24117225
try

 prd.ForEach(delegate(Prod p1)
            {
                p1.Title = p1.Title.ToLower();
               
            });
0
 
LVL 22

Accepted Solution

by:
p_davis earned 500 total points
ID: 24117233
or if you wish to stick to the lambda

 prd.ForEach(x =>x.Title = x.Title.ToLower());
0
 
LVL 22

Expert Comment

by:p_davis
ID: 24117241
basically it wasn't modifying it because ToLower() returns a string it doesn't modify the string that it is referencing.
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24117275
Change line 10 to:
prd.ForEach(x => x.Title = x.Title.ToLower()); 

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24117282
Curses! Foiled by p_davis again  :)

Couldn't get my test app built fast enough, lol!
0
 
LVL 22

Expert Comment

by:p_davis
ID: 24117292
happens to me all the time.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question