Click here to Skip to main content
16,020,877 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
Hi,
I am beginner, and I have a problem, maybe someone can help me.

I have Bitmap images:
C#
private static readonly Bitmap a1 = new Bitmap(WorkFolder4 + "a1.png");
private static readonly Bitmap a2 = new Bitmap(WorkFolder4 + "a2.png");
private static readonly Bitmap a3 = new Bitmap(WorkFolder4 + "a3.png");
//what I need is to call them using crafted variable name (methods first parameter is one of my images):

for (int k = 1; k < 20; k++)
   {
string temp = "a" + Convert.ToString(k);
//at this time temp = "a1",....
method(temp);
}

Please Help,
Posted
Updated 24-Jan-11 3:13am
v3
Comments
Sandeep Mewara 24-Jan-11 9:13am    
Use PRE tags to format code part, it makes the question readable.
shakil0304003 24-Jan-11 9:13am    
Not clear!!!

Though this is possible by using functionality from the .NET System.Reflection API. I beg of you not to do this. Take this route instead by adding all Bitmaps to an array or a list and you can easily iterate over that:

C#
private List<Bitmap> myBitmaps = new List<Bitmap>();

private static readonly Bitmap a1 = new Bitmap(WorkFolder4 + "a1.png");
myBitmap.Add(a1);
private static readonly Bitmap a2 = new Bitmap(WorkFolder4 + "a2.png");
myBitmap.Add(a2);
private static readonly Bitmap a3 = new Bitmap(WorkFolder4 + "a3.png");
myBitmap.Add(a3);
// Iterate over the Bitmaps in the list
foreach(Bitmap bm in myBitmaps)
{
    method(bm);
}

Your method needs to to have a parameter of type Bitmap for this or course. You might also think about having an array / List of the image paths and build a loop over that where you create the bitmaps and call your method:

C#
private List<String> imagePaths = new List<String>();
imagePaths.Add(WorkFolder4 + "a1.png");
imagePaths.Add(WorkFolder4 + "a2.png");
imagePaths.Add(WorkFolder4 + "a3.png");

// iterate over the strings in your list to create and process the Bitmaps
foreach(String path in imagePaths)
{
    Bitmap bm = new Bitmap(path);
    method(bm);
}


Unless your method "method" stores or uses the Bitmaps somehow you'd have to put them into an array or a list so you can still access them later. (Does WorkFolder4 end in a backslash?)

Hope this helps! If you have question leave me a comment.

Regards,
Manfred
 
Share this answer
 
v2
Comments
JF2015 26-Jan-11 7:48am    
Very good answer. 5
Is this what you are looking for?

C#
//what I need is to call them using crafted variable name (methods first parameter is one of my images):

for (int k = 1; k < 20; k++)
{
   string temp = "a" + Convert.ToString(k);

   //First way
   Bitmap bitmap = new Bitmap(WorkFolder4 + temp);
   method(temp);
   //or Second Way
   method( new Bitmap(WorkFolder4 + temp) );
}
 
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