Solved

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

Posted on 2009-04-10
6
194 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
  • 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

839 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