开发者

How to create a Class with List<> properties

开发者 https://www.devze.com 2023-01-19 03:11 出处:网络
The problem: - I want to read filename into a List<> inside a class. How to I read the filename into List<> in foreach Statement?

The problem: - I want to read filename into a List<> inside a class.

  1. How to I read the filename into List<> in foreach Statement?

  2. How to set up the get and set statement in List<> properties in the class.

  3. For this operation, which type to use: static class or normal class?

------revise------------- This is the problem :

sorry for the confusion.

What I wanted is to add the filenames into the List<> in the class. I want to have a list of filename in the List for later use.

1) How do I add the filename into the List g_Photoname in the Class in Click Event or Page Load Event?

protected void Button_Click(object sender, EventArgs e)

{

//-1- Get all the files in Temp directory

       var pe = new PicExample(@"c:\temp\");  



        foreach (string f in pe.FileList) 
        { 

   // Question : How do I add the f in the List<> in this class ?

  //  PhotoNameCollection = f ;  ???  Can do?


        } 

}

2) Create a static开发者_如何学编程 class to hold the list of filename in the List<> in class

a) How do I set up the get ad set statement for the List<> in this class?

b) Is the List<> correcctly used in this class?

public class PhotoNameCollection
{
    private List<string> g_Photoname


    public List<string> PhotoName
    {
        get 
    {

     }

   set
        {
    }

    }

 }


Something like this should help get you started ...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var pe = new PicExample(@"c:\temp\");

            foreach (var p in pe.FileList)
            {
                Console.WriteLine(p);
            }

            Console.ReadLine();
        }
    }

    public class PicExample
    {
        private List<string> _fileNames = new List<string>();

        public PicExample( string directory )
        {
            var files = Directory.EnumerateFiles(directory);

            foreach (var file in files)
            {
                _fileNames.Add(file);
            }
        }

        public List<string> FileList 
        { 
            get { return _fileNames; } 
            set { _fileNames = value; } 
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消