[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


help with adding values to generic list - syntax help

Posted on 2007-12-04
Medium Priority
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?


 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;
Question by:MrAgile
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
LVL 37

Accepted Solution

gregoryyoung earned 2000 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 ...


if you want to hold strings use:

  List<string> newlist = new  List<string>();

or if you want to use MyClass

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



Author Comment

ID: 20402033
HI Gregory,

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

  List<string> newlist = new  List<string>();

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

656 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