Reputation: 2893
I have been trying to solve this for the past couple of days with no success. What I basically have is 4 database fields that can store user's picture so user's can have up to 4 pictures. The field names are picture1, picture2, picture3 and picture4. What I am trying to do is when a user uploads a picture, check if picture1 is NULL if not then save the image filename there then for the next picture go to picture2 if not NULL then save there and so on til picture4.
What is have is this:
if (openhouse.picture1 == null)
{
openhouse.picture1 = changename;
}
// if picture2 is NULL and picture1 is not NULL then insert picture
if (openhouse.picture2 == null && openhouse.picture1 != null)
{
openhouse.picture2 = changename;
}
// if picture3 is NULL and picture2 is not NULL then insert picture
if (openhouse.picture3 == null && openhouse.picture2 != null)
{
openhouse.picture3 = changename;
}
// if picture4 is NULL and picture3 is not NULL then insert picture
if (openhouse.picture4 == null && openhouse.picture3 != null)
{
openhouse.picture4 = changename;
}
As you can see my issue is that as soon as you upload a picture that same picture is uploaded into all 4 fields since the IF statement has no breaks my question is: is there some way I can transfer this if statement into a switch statement that uses BREAKS that way as soon as a condition is true it stops evaluating the rest.
Upvotes: 0
Views: 282
Reputation: 102783
No need for a switch statement -- just use else if
.
if ( condition #1 )
{
// block #1
}
else if ( condition #2 )
{
// block #2
}
Block #2 doesn't get executed if the first condition is true.
if (openhouse.picture1 == null)
openhouse.picture1 = changename;
else if (openhouse.picture2 == null)
openhouse.picture2 = changename;
// etc.
Upvotes: 2