Share via


How to add item into List<> in static Class

Question

Sunday, October 10, 2010 8:49 AM

Hi, as above. Here the code.

 

1)Note :  Pictures is a collection of picture, PicNames is collection of names of pictures.

     MediaLibrary ml = new MediaLibrary();

            var ChkPics = ml.Pictures;
            var PicNames = from p in ChkPics
                           where p.Name.Contains("s")
                           select p;

 

  1. Static Class

public static class PhotoNames
    {
        private static List<string> m_Photoname = new List<string>();

        public static List<string> PhotoFileNames
            {
            get
            {
                return m_Photoname;
            }
            set
            {
                m_Photoname = value;
            }

        }

 

Q 3.1) How to I add the PicNames into static Class PhotoNames ?

 

     MediaLibrary ml = new MediaLibrary();

            var ChkPics = ml.Pictures;
            var PicNames = from p in ChkPics
                           where p.Name.Contains("s")
                           select p;

    PhotoNames.PhotoFileNames = PicNames < Error

    What I need to do ?

Q3.2)  Can I use foreach statement to add one by one into Static Class PhotoNames??

    foreach (var pic in PicNames)

  Photonames.PhotoFileNames = pic.Name;

      I have Error here. Is my static Class PhotoNames Correct?

 

Thanks

 

 

All replies (11)

Wednesday, October 13, 2010 4:54 AM âś…Answered

The errors are :

List<PhotoNames> plist = new List<PhotoNames>();

  > PhotoNames : Static type cant be used as type argument

plist.Add(new PhotoNames(){ PhotoFileNames = pic.Name,});

> PhotoNames : Can not create instance of the static class

The error it self is explaining you

1) Static type cant be used as type argument

Your PhotoNames class is a static class and you can not create list of any object which is marked as a static as static has a single instance only

2) Can not create instance of the static class

As I said static has a single instance only you can not create any instance of it


Sunday, October 10, 2010 9:25 AM

when looping, you'd need to Add the strings to your List.

try it like this:

    Photonames.PhotoFileNames.Add(pic.Name); 


Monday, October 11, 2010 10:52 AM

try this:

 

using System.Collections.Generic;

List<PhotoNames> plist = new List<PhotoNames>();
foreach (var pic in PicNames)
   plist.Add(new PhotoNames(){ PhotoFileNames = pic.Name,});


Wednesday, October 13, 2010 2:18 AM

What is the error you are getting, please do post it


Wednesday, October 13, 2010 2:46 AM

using System.Collections.Generic;

List<PhotoNames> plist = new List<PhotoNames>();
foreach (var pic in PicNames)
   plist.Add(new PhotoNames(){ PhotoFileNames = pic.Name,});

 

The errors are :

List<PhotoNames> plist = new List<PhotoNames>();

  > PhotoNames : Static type cant be used as type argument

 

plist.Add(new PhotoNames(){ PhotoFileNames = pic.Name,});

> PhotoNames : Can not create instance of the static class

 


Wednesday, October 13, 2010 5:20 AM

Hi,

If you make the class as normal class (by removing the keyword static), you should be able to add items into List<> but not otherwise. 


Wednesday, October 13, 2010 5:59 AM

Harshad,

Yes by removing static keyword from class we can create instance of it,

but remember the class has m_Photoname list which is marked as static, so creating instance of a class will not affect the value of m_Photoname


Wednesday, October 13, 2010 8:22 PM

 

 

1) Change it to Normal class :

public class PhotoNames
    {
        private List<string> m_Photoname = new List<string>();

        public  List<string> PhotoFileNames
            {
            get
            {
                return m_Photoname;
            }
            set
            {
                m_Photoname = value;
            }

        }

using System.Collections.Generic;

List<PhotoNames> plist = new List<PhotoNames>();
foreach (var pic in PicNames)
   plist.Add(new PhotoNames(){ PhotoFileNames = pic.Name,});

 

-- The error is here:

plist.Add(new PhotoNames(){ PhotoFileNames = pic.Name,});

 

Something like this:

PhotoFilesNames cant conver to List<>

 

Thanks

 

 

 

 

 

 


Thursday, October 14, 2010 1:20 AM

List<PhotoNames> plist = new List<PhotoNames>();
foreach (var pic in PicNames)
   plist.Add(new PhotoNames(){ PhotoFileNames = pic.Name,});

-- The error is here:

plist.Add(new PhotoNames(){ PhotoFileNames = pic.Name,});

Something like this:

PhotoFilesNames cant conver to List<>

Thanks

First of all PhotoFileNames takes a List<string> and i think you are assigning a string i.e. pic.Name to it, insted do pic.Name.ToList();


Thursday, October 14, 2010 2:00 AM

Hi Deven,

I did not realize that the list m_Photoname was also marked as static.

Thanks for pointing out  


Thursday, October 14, 2010 2:07 AM

Hi Deven,

I did not realize that the list m_Photoname was also marked as static.

Thanks for pointing out  

You are always welcome