• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 218
  • Last Modified:

help with adding values to generic list - syntax help

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
MrAgile
Asked:
MrAgile
1 Solution
 
gregoryyoungCommented:

  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
 
MrAgileAuthor Commented:
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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now