C# 检查多个if语句中的值并基于结果存储多个注释
有人能给我举个例子,说明从if语句返回多个注释的最佳方法吗C# 检查多个if语句中的值并基于结果存储多个注释,c#,asp.net,C#,Asp.net,有人能给我举个例子,说明从if语句返回多个注释的最佳方法吗 protected string CheckFacility(int FacilityId) { var cfacility = new List<string>(); BuildingPresenter b = new BuildingPresenter(); FunctionalAreaPresenter f = new FunctionalAreaPresenter(); if (b
protected string CheckFacility(int FacilityId)
{
var cfacility = new List<string>();
BuildingPresenter b = new BuildingPresenter();
FunctionalAreaPresenter f = new FunctionalAreaPresenter();
if (b.GetBuildings(FacilityId) != null)
{
cfacility.Add("There are Functional Areas associated with this facility. ");
}
if (f.GetFunctionalAreas(FacilityId) != null)
{
cfacility.Add("There are Functional Areas associated with this facility. ");
}
var cfacilitystring = string.Join(",", cfacility);
受保护的字符串检查工具(int FacilityId)
{
var cfacility=新列表();
BuildingPresenter b=新建BuildingPresenter();
FunctionalAreaPresenter f=新FunctionalAreaPresenter();
如果(b.GetBuildings(FacilityId)!=null)
{
cfacility.Add(“存在与该设施相关的功能区域”);
}
如果(f.GetFunctionalEAS(FacilityId)!=null)
{
cfacility.Add(“存在与该设施相关的功能区域”);
}
var cfacilitystring=string.Join(“,”,cfacility);
我发现了这些错误
错误3“string.Join(string,string[])”的最佳重载方法匹配具有一些无效参数
错误4参数2:无法从“System.Collections.Generic.List”转换为“string[]”var shirtAttributes=new List();
var shirtAttributes = new List<string>();
if (shirt.IsBlack)
{
shirtAttributes.Add("black");
}
if (shirt.IsLarge)
{
shirtAttributes.Add("large");
}
if (shirt.IsLongSleeve)
{
shirtAttributes.Add("long sleeve");
}
var shirtAttributesString = string.Join(",", shirtAttributes);
如果(衬衫是黑色的)
{
shirtAttributes.添加(“黑色”);
}
如果(衬衫尺寸较大)
{
shirtAttributes。添加(“大”);
}
if(衬衫、长袖)
{
shirtAttributes.添加(“长袖”);
}
var shirtAttributeString=string.Join(“,”,shirtAttributes);
输出类似于:“black,long-sleeve”或“black”或“large,long-sleeve”您有很多方法来处理这个问题,您可以创建一个类并重写
ToString()
方法:
public class Shirt{
public Shirt(string color, string size, string sleeve)
{
Color =color;
Size=size;
Sleeve=sleeve;
}
public string Color {get;set;}
public string Size {get;set}
public string Sleeve {get;set}
public override string ToString(){
return string.Format("shirt is color :{0} , size :{1} and shleeve: {2}",Color,Size,Sleeve )
}
}
因此,当您在使用值初始化类之后运行程序时
Shirt myShirt = new Shirt("black","large","long");
if(myShirt.Color=="black"&& myShirt.Size=="large" && myShirt.Sleeve=="long")
{
return myShirt.ToString();
}
else{
return "no match";//or want you want
}
希望它能有所帮助。嗯?你有一个3条件if语句,这与返回有什么关系?你实际上想实现什么?是的,你可以以各种不同的方式返回多个值。在我们回答这个问题之前,你需要让我们更清楚地了解你在尝试做什么和你尝试了什么。Y你只能从一个方法返回一次。在上面的例子中,听起来你好像在尝试存储选定的选项-因此,除非只有衬衫是黑色、大和长袖才重要(这是一种奇怪的情况),我会创建一个对象来存储所有选择的选项并返回该对象。也许你应该提供你编写的代码,或者尝试编写一些代码并显示它没有做你希望它做的事情。它不需要编译,只需要准确地传达你正试图完成的事情,这样我们就知道如何帮助你。比如说e颜色、大小或衬衫类型无关紧要。我想根据存在的属性向用户发送消息。因此,该消息可以返回颜色和大小等任意组合,也可以只返回衬衫类型。我只是想找出最好的方法。我正在尝试检查这些属性是否存在。我收到了这些错误rs.您能提供帮助吗?谢谢!错误3“string.Join(string,string[])”的最佳重载方法匹配有一些无效参数错误4参数2:无法从“System.Collections.Generic.List”转换为“string[]”请查看