昨天我在玩jQGrid插件和ASP.NET.一切都很好,我的网格现在正在工作,但我有两种方法,让我的代码闻起来.
臭方法:
private IOrderedEnumerable<Employee> GetOrderedEmployees(Column sortColumn, bool ascending)
{
switch (sortColumn)
{
case Column.Name:
{
return GetOrderedEmployees(e => e.Name, ascending);
}
case Column.Salary:
{
return GetOrderedEmployees(e => e.Salary, ascending);
}
default:
{
return GetOrderedEmployees(e => e.ID, ascending);
}
}
}
private IOrderedEnumerable<Employee> GetOrderedEmployees<TSortKey>(Func<Employee, TSortKey> func, bool ascending)
{
return ascending ? Context.Employees.OrderBy(func) : Context.Employees.OrderByDescending(func);
}
Run Code Online (Sandbox Code Playgroud)
我无法找到,如何正确地重构它们.似乎最好的解决方案是return e=>e.Name在switch语句中只返回lambdas(fe ),但是怎么做呢?
在switch语句中,ascending参数传递了3次.这不是重复吗?
我需要将一个开关与一个if语句结合起来.
我怎样才能做到这一点?我想做这样的事情:
switch (periodtype)
{
if(starttime>endtime)
{
;
}
else
{
case 0: nextRunTime = nextRunTime.AddHours(period); break;
case 1: nextRunTime = nextRunTime.AddMinutes(period); break;
case 2: nextRunTime = nextRunTime.AddSeconds(period); break;
}
}
Run Code Online (Sandbox Code Playgroud) 嗨,我正在练习程序中的"Switch Loop".我正在编写一个用户可以输入整数的代码,在用户输入整数后,它还会显示用户输入的内容.现在我正在尝试实现程序要求用户输入的位置通过选择Y/N再次编号.我已将它包含在我的代码中但是如果我在第一次输入字符时要求我输入一个整数,程序将执行catch部分.我怎样才能说明如果用户输入一个字符,它也会再次显示该消息,"请输入整数:"
谢谢你的帮助,我在这里的某个地方迷路了.
int enterYourNumber;
char shortLetter;
try
{
Console.WriteLine("Please enter the integer: ");
enterYourNumber = Convert.ToInt32(Console.ReadLine());
WriteNumber(enterYourNumber);
Console.WriteLine("Do you still want to enter a number? Y/N");
shortLetter = Convert.ToChar(Console.ReadLine());
while (shortLetter == 'y' || shortLetter == 'Y')
{
Console.WriteLine("Please enter the integer: ");
enterYourNumber = Convert.ToInt32(Console.ReadLine());
WriteNumber(enterYourNumber);
Console.WriteLine("Do you still want to enter a number? Y/N");
shortLetter = Convert.ToChar(Console.ReadLine());
}
}
catch
{
Console.WriteLine("Please enter an integer not a character");
}
}
public static void WriteNumber(int wordValue)
{
switch …Run Code Online (Sandbox Code Playgroud) 我正在实现一个系统,用于在我正在编写的程序的不同部分之间发送消息.有一些通用的消息类型以及一些特定于程序的每个部分.我想避免从每个类型的基本消息类派生中固有的层次结构腐烂,所以我将这种类型封装在int或ushort中.然后,我使用"Messages"命名空间集中不同的类型,并使用一堆常量集中静态类.但是,我遇到了必须维护每个不同部分的唯一编号列表的问题:
namespace Messages
{
public static class Generic
{
public const Int32 Unknown = 0;
public const Int32 Initialize = 1;
...
public const Int32 Destroy = 10;
}
}
Run Code Online (Sandbox Code Playgroud)
其他地方
namespace Messages
{
public static class Graphics
{
public const Int32 Unknown = 0;
public const Int32 AddGraphic = 11; // <-- ?
}
}
Run Code Online (Sandbox Code Playgroud)
拥有那么随意的11似乎很难,特别是如果我有其中的几个,保持和更新以确保没有碰撞似乎是一种痛苦.是否有一个简单的解决方案,以确保每个参考都是独一无二的?我尝试使用静态只读,在静态构造函数中将它们从Unique.ID()函数初始化,但如果我这样做,我无法切换()传递的Message类型,因为它说"预期的常量类型"对于每个案例.
为什么这个java switch语句一直告诉我我的语句不是语句
public void setConstant(float inNumGrade)
{
this.yourNumberGrade = inNumGrade;
switch (this.yourLetterGrade)
{
case 'A':
this.yourNumberGrade >= 0.90;
break;
case 'B':
this.yourNumberGrade >= .8;
break;
case 'C':
this.yourNumberGrade >= .7;
break;
case 'D':
this.yourNumberGrade >= .6;// not a statement
default:
} // end switch
}
Run Code Online (Sandbox Code Playgroud) 我想知道这种风格的switch语句应该改成if else语句.
switch (foo) // foo is an enumerated type
{
case barOne:
if (blahOne)
{
DoFunction(//parameters specific to barOne);
break;
}
case barTwo:
if (blahTwo)
{
DoFunction(//parameters specific to barTwo);
break;
}
//etc.
default:
// Whatever happens if none of the case's conditionals are met
}
Run Code Online (Sandbox Code Playgroud)
除非其中一个案件满足条件,否则基本上会发生坠落事件.这些情况非常相似,只是需要检查的内容和需要传递的内容不同,这就是我使用switch语句的原因.
使用会更好if else if吗?否则,它是否足够清晰,但不足以保证对该漏洞的评论?多态性也总是一种选择,但对我来说似乎有些过分.
嗨,我有一个简单的问题,但是一直困扰我一段时间.
题:
在C#中使用switch语句时,使用enumsover constants或者反之亦然?或者这是一个偏好的问题?我问这个是因为很多人似乎喜欢使用enums,但是当你打开一个int值时,你必须将每个包含的值转换enum成一个int,即使你指定了它的类型enum.
代码片段:
class Program
{
enum UserChoices
{
MenuChoiceOne = 1,
MenuChoiceTwo,
MenuChoiceThree,
MenuChoiceFour,
MenuChoiceFive
}
static void Main()
{
Console.Write("Enter your choice: ");
int someNum = int.Parse(Console.ReadLine());
switch (someNum)
{
case (int)UserChoices.MenuChoiceOne:
Console.WriteLine("You picked the first choice!");
break;
// etc. etc.
}
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法可以创建一个实例enum并将整个enum转换为int?
谢谢!
我正在尝试简化一个需要大量空间和时间来编写的switch语句.查看代码,你会明白的.如果没有,我稍后会解释.
// When row is selected
- (void)pickerView:(UIPickerView *)pickerTimer didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
switch (row) {
case 0:
NSLog(@"It obviously worked.0");
break;
case 1:
NSLog(@"It obviously worked.1");
break;
case 2:
NSLog(@"It obviously worked.2");
break;
case 3:
NSLog(@"It obviously worked.3");
break;
case 4:
NSLog(@"It obviously worked.4");
break;
case 5:
NSLog(@"It obviously worked.5");
break;
case 6:
NSLog(@"It obviously worked.6");
break;
default:
NSLog(@"It did kindof work.NIL");
break;
}
}
Run Code Online (Sandbox Code Playgroud)
有什么方法可以简化这个吗?只是喜欢,
NSLog(@"It did work! %@", row);
Run Code Online (Sandbox Code Playgroud)
为了记录,我尝试了,它没有用.
像这样的场景:
我有不同的对象执行与各自func()实现的类似操作.
func_manager()有两种解决方案可以根据不同的对象调用func()
解决方案1:使用c ++中指定的虚函数字符.func_manager以不同的方式对不同的对象点传入进行工作.
class Object{
virtual void func() = 0;
}
class Object_A : public Object{
void func() {};
}
class Object_B : public Object{
void func() {};
}
void func_manager(Object* a)
{
a->func();
}
Run Code Online (Sandbox Code Playgroud)
解决方案2:使用普通开关/外壳.func_manager的工作原理与不同类型的传入不同
typedef enum _type_t
{
TYPE_A,
TYPE_B
}type_t;
void func_by_a()
{
// do as func() in Object_A
}
void func_by_b()
{
// do as func() in Object_A
}
void func_manager(type_t type)
{
switch(type){
case TYPE_A:
func_by_a();
break;
case TYPE_B:
func_by_b();
default: …Run Code Online (Sandbox Code Playgroud) 假设我有一堆箱子,每个箱子都有一系列数字.一个例子是:
bin 1: 0-8
bin 2: 9-16
bin 3: 17-24
etc
Run Code Online (Sandbox Code Playgroud)
有没有办法使用switch语句来确定一个数字属于哪个bin?我试图弄清楚如何使案例反映一个数字在一个范围内,但它们似乎不是"常量表达式"所以我不确定它是否可能......
编辑
我应该更准确; 我的箱子是两个人的力量......
bin 1: 0 to 2^3
bin 2: 2^3 + 1 to 2^4
bin 3: 2^4 + 1 to 2^5
etc
Run Code Online (Sandbox Code Playgroud) switch-statement ×10
c# ×4
c ×2
c++ ×2
.net ×1
const ×1
constants ×1
enums ×1
if-statement ×1
iphone ×1
java ×1
lambda ×1
objective-c ×1
syntax ×1
uipickerview ×1