Erix
Erix

Reputation: 69

C# structure recommendation

As a C# begiginer, I'm coding an UWP app. The app is currently working exactly like expected but I wonder if there is a better approch for the fact that my images arrays are declared as Global for the Class. I searched for an alternative for several hours but didn't find an example that match my need.

Below is a reprentation of my code:

namespace myUWPapp
{
    public sealed partial class MainPage : Page
    {
        string[] imgImagesSourceFileName = { "images1.png", "image2.png", "image3.png", "image4.png", "image5.png" };
        string[] btnImagesSourceFileName = { "images6.png", "image7.png", "image8.png", "image9.png", "image10" };

        public MainPage()
        {
            this.InitializeComponent();
            ...
            ...
        }
        private string GetImageFileName(string[] array, int index)
        {
            string image = array[index];

            return image;
        }
        private void ReverseArray(string[] array)
        {
            Array.Reverse(array);
        }
        private void MethodeUsingGetImagesSourceFileName()
        {
            ...         
            string image = GetImageFileName(myArray, myIndex);
            ...         
        }
        private void MethodeUsingReverseArray()
        {
            ...
            ...
            ReversesArray(myImagesArray);
        }
    }
}   

Looking forward for alternative but don't forget that I'm a relatively new at coding.

Upvotes: 0

Views: 113

Answers (2)

Nico Zhu
Nico Zhu

Reputation: 32775

I checked your code, I think MethodeUsingGetImagesSourceFileName and MethodeUsingReverseArray are inflexible. If you just want to process string array, you could create ListExtension with List<string> for your image array.

public static class ListExtension
{
    public static void InitializeImageArray(this List<string> items, int start,int end)
    {
        for (int i = start; i <= end; i++)
        {
            items.Add($"images{i}.png");
        }
    }
    public static string GetImageFileNameWithIndex(this List<string> items, int index)
    {
        return items[index];
    }
}

Usage

private List<string> _imgImageitems = new List<string>();
private List<string> _btnImageitems = new List<string>();

private void InitializeImages()
{
    _imgImageitems.InitializeImageArray(1, 5);
    _btnImageitems.InitializeImageArray(6, 10);
}

Get image file name with index

string image = _imgImageitems.GetImageFileNameWithIndex(1);

Reverse list

_imgImageitems.Reverse();

Upvotes: 1

Lee McPherson
Lee McPherson

Reputation: 931

Where are your images located? In a folder in your project? If so, you can just do something like this to get all the image file names:

// Get the path to the app's Assets folder.
string root = Windows.ApplicationModel.Package.Current.InstalledLocation.Path;
string path = root + @"\Assets";  // where your image files are located

// Get the folder object that corresponds to this absolute path in the file system.
StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(path);

IReadOnlyList<StorageFile> items = await folder.GetFilesAsync();

List<string> listOfNames = items.Select(x=>x.Name).ToList();
//or you can leave it as IEnumerable... or you can convert ToArray()

Upvotes: 1

Related Questions