A faster way to make "if" statements
Is there a faster way to do this? I have 36 different images, when the image changes, I have a line that tracks the image (rotation), image1 - rot = 1, etc., what I need to do is use 36 assertions like this:
if (rotation == 1) //This is picture1
{
}
else if (rotation == 2) //This is picture2
{
}
up to:
else if (rotation == 36) //This is picture36
{
}
Is there a way to figure out what rotation is, with only 1 or 2 lines of code? And anyone who says a check before asking I checked and I didn't find anything to help, if you find anything post it here.
The internals of my if statements are only for changing the image.
Thank.
source to share
For example, say inside yours if you are typing your rotation.
if (rotation == 1) //This is picture1
{
System.out.println(1);
}
else if (rotation == 2) //This is picture2
{
System.out.println(2);
}
else if(rotation==36)
{
System.out.println(36);
}
You can change all the code to one line.
System.out.println(rotation);
source to share
You can use an array WhateverYourPictureClassIs
or operator IDictionary<int,WhateverYourPictureClassIs>
or switch
.
For example, if the image information is a string:
string[] pictures = {
"you might have a blank entry here if the first number is 1 instead of 0",
"picture1",
"picture2",
"picture3",
"picture4",
// ...and so on...
};
Then when viewing the image
if (picture >= 0 && picture < pictures.Length) { // The 0 might be 1 in your case
pictureInfo = pictures[picture];
}
or
IDictionary<int,string> pictures = new Dictionary<int,string>();
pictures.Add(1, "picture1");
// ...and so on...
Finding it looks very similar.
or
switch (picture) {
case 1: pictureInfo = "picture1"; break;
case 2: pictureInfo = "picture2"; break;
// ...and so on...
}
source to share
This is a long shot and I have assumptions that the filename of the images will always match the rotation value as shown below.
rotation = 1 -----> filename = image1.png
rotation = 2 -----> filename = image2.png
If so, you can do this
string fileName = "image" + rotation + ".png";
You can use this to select or display your file however you like.
source to share
Switch statement.
http://www.dotnetperls.com/if-switch-performance
switch(number)
{
case 1:
break;
}
Or, if you have List<T>
- in this case T
- this is your image you can do
List<T> pictures = new List<T>();
T picture = pictures[rotation];
source to share