tags:

views:

29

answers:

5

My code-behind looks like this

    class Image
    {
        public string tnImg { get; set; }
        public string Name { get; set; }
        public string city { get; set; }
        public string refPlace { get; set; }
        public string refInfo { get; set; }
        public string refInfoDynamic { get; set; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        getImgCarousel();
    }

    public void getImgCarousel()
    {
        List<Image> Images = new List<Image>();

        var carouselImages = new Image();
        carouselImages.Name = "test";

        var carouselImages2 = new Image();
        carouselImages2.Name = "test2";

        Images.Add(carouselImages);
        Images.Add(carouselImages2);
    }

Then I would like to do the following in .aspx

<div class="wrapCarousel">  
 <div>My Images</div>
   <% foreach(var image in Images) { %>
      <div><%=image.Name%></div> <!-- format your markup here -->
   <% } %>

</div>

But it seems like I can't access the list Images inside the .aspx page... Any suggestions?

Thanks M

+2  A: 

You should make Images a field inside your class (local variables can be seen only inside of the scope they were declared in):

List<Image> images = new List<Image>();

protected List<Image> Images
{
    get { return this.images; }
}

public void getImgCarousel()
{
    var carouselImages = new Image();
    carouselImages.Name = "test";

    var carouselImages2 = new Image();
    carouselImages2.Name = "test2";

    Images.Add(carouselImages);
    Images.Add(carouselImages2);
}
Andrew Bezzub
When I try this I get the following error: Error 3 Inconsistent accessibility: property type 'System.Collections.Generic.List<xxx.controls.slideshow.Image>' is less accessible than property 'xxx.controls.slideshow.Images' C:\xxxx\controls\slideshow.ascx.cs
Mikael
Is there some problem because the use of controls?
Mikael
You can make property public if you are exposing it from the control.
Andrew Bezzub
actually I don't think it's exposed from the control. The write out is inside slideshow.ascx which then is loaded inside default.aspx
Mikael
I think you have this error because you declared Image class inside your code-behind. Nested classes are private by default. You should make you Image class protected too.
Andrew Bezzub
+2  A: 

Your Images variable is a local variable to the getImgCarousel method and can't be seen outside of it.

Promote it to a public property (or field) in order to access it outside of the method:

// readonly property - will throw null reference if not initialized
public IList<Image> Images { get;}

public void getImgCarousel()
{
    this.Images = new List<Image>();

    var carouselImages = new Image();
    carouselImages.Name = "test";

    var carouselImages2 = new Image();
    carouselImages2.Name = "test2";

    Images.Add(carouselImages);
    Images.Add(carouselImages2);
}
Oded
To use this method I guess I have to change the code to this, public IList<Image> Images { get; set; }? But then I get stuck on the same thing as for the other solution that was suggested..... Error 3 Inconsistent accessibility: property type 'System.Collections.Generic.List<xxx.controls.slideshow.Image>' is less accessible than property 'xxx.controls.slideshow.Images' C:\xxxx\controls\slideshow.ascx.cs
Mikael
@Mikael - that's because your `Image` class accessibility is the default (`internal`) one. Make it _public_.
Oded
A: 

Look at the accessibility of the Images field. It is a private temporary variable in your method. Make it a public field in your class and you will be able to access it.

Dave White
A: 

At runtime, the aspx page is rendered by creating a new class that inherits from the code-behind class, so because of accessibility rules, any private classes / fields / methods are inaccessible.

You would have to declare anything that should be used by the aspx code as protected, because that enables access for any inherited classes.

In your code, Images is local to the method, so it could not be accessed even by other methods in the same class (i.e. Page_Load), much less by the generated subclass. As most of the other answers pointed out, you'll need to remove it from the method, and move it to a property.

But that would mean that you are exposing Image objects to your subclass, which are defined in your base class without any modifier (class Image) which the compiler treats as private class Image which means that no other class has access to the Image class in any way, shape or form.

So how, to remedy that, you would need to declare the nested class as protected, so it will be accessible to your subclass aspx.

The corrections I made are emphasized:

//accessible to the code-front
protected List<Image> Images {get; private set; }

//nested class also accessible to the code-front
protected class Image
{
    public string tnImg { get; set; }
    public string Name { get; set; }
    public string city { get; set; }
    public string refPlace { get; set; }
    public string refInfo { get; set; }
    public string refInfoDynamic { get; set; }
}

//exactly the reason why by default Page_Load is protected
protected void Page_Load(object sender, EventArgs e)
{
    getImgCarousel();
}

//this should be private as will not be called by anything outside the class
private void getImgCarousel()
{
    //uses the property
    Images = new List<Image>();

    var carouselImages = new Image();
    carouselImages.Name = "test";

    var carouselImages2 = new Image();
    carouselImages2.Name = "test2";

    Images.Add(carouselImages);
    Images.Add(carouselImages2);
}
SWeko
A: 

You're close and Andrew Bezzub's answer helps but you also need to make the Image class accessible.

// Make the list protected
protected List<Image> Images;

// Make the class protected
protected class Image
{
    ...
}

protected void Page_Load( object sender, EventArgs e )
{
    ...
}

public void getImgCarousel()
{
    // Assign to the protected field.
    Images = new List<Image>();

    ...
}
J. Farray