C# 将IF语句转换为SWITCH语句
在过去的几天里,我一直试图解决这个问题,但没有成功。我基本上有4个数据库字段,可以存储用户的图片,这样用户最多可以有4张图片。字段名为picture1、picture2、picture3和picture4我试图做的是当用户上传图片时,检查picture1是否为NULL,如果不是,则将图像文件名保存在那里,然后对于下一张图片,转到picture2如果不是NULL,则保存在那里,依此类推直到picture4 这是什么C# 将IF语句转换为SWITCH语句,c#,database,asp.net-mvc-3,switch-statement,C#,Database,Asp.net Mvc 3,Switch Statement,在过去的几天里,我一直试图解决这个问题,但没有成功。我基本上有4个数据库字段,可以存储用户的图片,这样用户最多可以有4张图片。字段名为picture1、picture2、picture3和picture4我试图做的是当用户上传图片时,检查picture1是否为NULL,如果不是,则将图像文件名保存在那里,然后对于下一张图片,转到picture2如果不是NULL,则保存在那里,依此类推直到picture4 这是什么 if (openhouse.picture1 == null)
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;
}
正如你所看到的,我的问题是,一旦你上传一张图片,同一张图片就会被上传到所有4字段中,因为IF语句没有中断,我的问题是:有什么方法可以将这个IF语句转换成一个switch语句,一旦条件为真,它就会以这种方式使用中断停止对其余部分的求值。不需要switch语句——如果
,只需使用else即可
if ( condition #1 )
{
// block #1
}
else if ( condition #2 )
{
// block #2
}
如果第一个条件为true,则不会执行块2
if (openhouse.picture1 == null)
openhouse.picture1 = changename;
else if (openhouse.picture2 == null)
openhouse.picture2 = changename;
// etc.