Solved

help with adding values to generic list - syntax help

Posted on 2007-12-04
2
213 Views
Last Modified: 2013-12-16
HI There,

I'm having trouble with the syntax of adding values to a generic list.I have a class with a public string property and I want to use it to capture some values from a form and add it to a generic list that I have. I'm just not sure of the syntax. could someone help me fix my code?

Sean

 public class MyClass
    {
        public MyClass() { }

 
        protected string _mystringval;
 


        /// <summary>
        /// pros / cons
        /// </summary>
        public string mystringval
        {
            get { return this._mystringval; }
            set { this._mystringval = value; }
        }      

}



  List<MyNamespace.MyClass> newlist = new  List<MyNamespace.MyClass>();


           MyNamespace.MyClass tes = new MyNamespace.MyClass();

tes.mystringval = "mynewstringvalue";

  newlist .Add(tes.mystringval); --- falls over in a crumbling heap


repeater.DataSource = newlist;
            repeater.DataBind();
0
Comment
Question by:MrAgile
[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
2 Comments
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 500 total points
ID: 20401949

  List<MyNamespace.MyClass> newlist = new  List<MyNamespace.MyClass>();


   you declare your list to hold MyClass objects ...

  newlist .Add(tes.mystringval); --- falls over in a crumbling heap

   mystringval is not a MyClass object so it fails ...


so:


if you want to hold strings use:

  List<string> newlist = new  List<string>();
  newlist.Add(tes.mystringval);

or if you want to use MyClass

  List<MyNamespace.MyClass> newlist = new  List<MyNamespace.MyClass>();
  newlist.Add(tes);

Cheers,

Greg
0
 
LVL 8

Author Comment

by:MrAgile
ID: 20402033
HI Gregory,

So I have to use this method even though myclass has a string property?

  List<string> newlist = new  List<string>();
  newlist.Add(tes.mystringval);
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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

707 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