BIG if/switch声明 [英] BIG if/switch Statement

查看:81
本文介绍了BIG if/switch声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的if语句,说我变成了开关,但是不幸的是,我发现开关变得太大了.我看了看哈希表和映射表,但在这一点上还不算什么.理想情况下,我想将所有这些存储在头文件中,然后从那里调用它.只是开关的一小部分.谢谢.

I have a big if statement that I turned into a switch, unfortunately I find the switch getting too big. I looked at the hash and map but it''s beyond ne at this point. Ideally I would like to store all of this in a header file and just call it from there. Just a piece of the switch. Thank you.

switch (ipsrc) {
	case 192.168.1.0;
	Source = 0.0.0.0;
	break;
	case 165.133.22.1;
	Source = 0.0.0.0;
	break;
	case 192.000.1.1 to 192.000.1.9;
	Source = 0.0.0.0;
	break;
	}

推荐答案

似乎您想要的是根据IP地址设置源变量.
您要避免在代码中使用大的switch语句.如果我错了,请纠正我.

我通常使用数据库表来处理这类情况,该数据库表包含我需要的案例以及相关的值.

It seems that what you want here is set the source variable dependent on an IP address.
You want to avoid a big switch statement in code. Correct me if I''m wrong.

I usually handle these kind of cases with a database table that has the cases I need and the value that goes with it.

ID     IPFrom        IPTo          Source
1      192.168.1.0   192.168.1.0   0.0.0.0
3      165.133.22.1  165.133.22.1  0.0.0.0
2      192.000.1.1   192.000.1.9   0.0.0.0




然后编写一段代码(或SP),检查IP(ipsrc)是否在IPFrom和IPTo之间,并获取正确的Source值.前两行的IPTo可能为NULL(随您的便是您的设计).
好处是您没有巨大的switch语句,而且如果IP地址更改或添加,您要做的就是更改或向数据库表添加IP范围.

希望对您有用:)



Etc.
Then write a piece of code (or an SP) that checks if the IP (ipsrc) is between any of the IPFrom and IPTo and get the correct Source value for it. Possibly IPTo could be NULL for the first two rows (whatever you like, it''s your design).
The upside is that you do not have a huge switch statement and also that if IP addresses change or get added all you have to do is change or add an IP range to your database table.

Hopefully this is any use to you :)


这篇关于BIG if/switch声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆