Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm trying to get the result of the ImageDescr List, visible on the whole page.
e. g.: I press a Button and read the index (n) of the ImageDescr List to display the value.

What I have tried:

<pre>protected void Page_Load(object sender, EventArgs e)
 {
    ....
    ImageDescription ImageDescr = new ImageDescription();
   
    //ImageDescr is declared public in the class ImageDescription
    //after execute not see the element ImageDescr = null
    //
 }


Class for full List , read string from Directory

public class ImageDescription
    {
        private string virtualPath;
        private string physicalPath;

        public List<string> ImageDescr { get; set; }

        public ImageDescription()
        {
            // from other class
            PathImages pImg = new PathImages();
            virtualPath = pImg.path1;
            physicalPath = pImg.path2;

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

            try
            {
                var imagesFolder = new DirectoryInfo(physicalPath);
                foreach (var item in imagesFolder.EnumerateFiles())
                {
                    if (item is FileInfo)
                    {
                        //add virtual path of the image to the images list
                        ImageDescr.Add(string.Format("{0}/{1}", virtualPath, item.Name));
                    }
                }

                //the List ImageDescr is is correctly filled
            }
            catch (Exception ex)
            {
                //log exception
                //Logger.Log(ex.Message);
            }

        }        
    } 
Posted
Updated 25-Jan-18 5:39am

Um. You do realize that the version you populated masks the class level version:
C#
public List<string> ImageDescr { get; set; }

        public ImageDescription()
        {
...
            List<string> ImageDescr = new List<string>();

            try
            {
...
                        ImageDescr.Add(string.Format("{0}/{1}", virtualPath, item.Name));
...
            }
         }
And what that means is that the data you filled in is discarded when the constructor exists, leaving the public version of ImageDescr unassigned and null?
I'd suggest that you start by following C# naming conventions, or assign the value instead of using a new variable:
C#
        public List<string> ImageDescr { get; set; }

        public ImageDescription()
        {
...
            ImageDescr = new List<string>();

            try
            {
 
Share this answer
 
Comments
Member 13174280 24-Jan-18 4:36am    
it is clear that I am wrong, otherwise I would not have asked.
a minimal example would have been more constructive, to everyone's blessin
OriginalGriff 24-Jan-18 4:46am    
You mean ... just like the one I posted yesterday?
johannesnestler 25-Jan-18 10:41am    
so you don't know variable scoping rules - a Topic every C# tutorial covers on it's first pages.Then you get a perfect problem description from "the boss" for your specific code - and now you think you need a more minimal example? Sry but WTF!
Member 13174280 24-Jan-18 8:04am    
What you published yesterday does not need to understand which way to go or where the basic error is.
OriginalGriff 24-Jan-18 8:14am    
Pardon? Do you want to try that in English?
public partial class _Default : System.Web.UI.Page
{
    .........
    public List<string> ImgDes = new List<string>();

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
          ............
          GetNameImage();
        }
    }


 protected void GetNameImage()
    {
        PathDesc = MapPath(virtualPath + "/" + "DescrImages.txt");

        if (File.Exists(PathDesc))
        {
            using (var sr = new StreamReader(PathDesc))
            {
                string line = "";
                while ((line = sr.ReadLine()) != null)
                {
                    NameImage.ImgName.Add(line);
                }

                sr.Close();
            }

        ImgDes = NameImage.ImgName.ToList();
    }

public static class NameImage
    {
        public static List<string> ImgName = new List<string>();

    }
}
 
Share this answer
 
my solution is not perfect:
with Scriptmanager + UpdatePanel
the data stored in the List ImgDes is lost when i click on an image of the DataList to perform other operations on the same page

without UpdatePanel ImgDes remaining usable in every part of the page.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900