简体   繁体   English

可以将字符串值存储在C#中

[英]can int store string value in c#

I am working on the front end of an application. 我正在开发应用程序的前端。 I have to introduce one more filter criteria LoanNumber. 我必须再介绍一个过滤条件LoanNumber。 Now loan number is E-100. 现在贷款号是E-100。 Business layer and domain object is not in my control. 业务层和域对象不在我的控制范围内。 So i cannot change it. 所以我不能改变它。 Domain object which holds loannumber is integer, I have to do 持有借贷编号的域对象是整数,我必须做

ingeoFilterData.intLoanNumber="E-100"

ingeoFilterData is the domain object. ingeoFilterData是域对象。 intLoanNumber is declared as Nullable Int32 Now this domainobject is very critical and it goes to some external engine,so i cannot change it. intLoanNumber声明为Nullable Int32现在,此域对象非常关键,它已连接到某些外部引擎,因此我无法更改它。

Please suggest some workaround. 请提出一些解决方法。

Edit- 编辑-

I am copying down loannumber from database table. 我正在从数据库表中复制贷款编号。

RT1
RT2
PT1
pt10
PT11
PT12
PT13
PT14
PT15
pt16
pt17
pt8
pt9
MDR1
MDR2
MDR3

If you have only one character, you can do this: 如果只有一个字符,则可以执行以下操作:

multiply your int by 100. (for example E-51 -> 5100) 将int乘以100。(例如E-51-> 5100)

Then keep the char as int in the rest of the number (for example 5106). 然后在其余数字中将char保留为int(例如5106)。

Do the reverse when you need to show the UI id (E-51). 当需要显示UI ID(E-51)时,请执行相反的操作。

If you have no limitations (as you mentioned) then you can have your int as a protocol (according to me that is even harder because you are limited by Int32 - 4,294,967,296). 如果您没有限制 (如前所述),则可以将int作为协议(根据我的说法,这更加困难,因为您受到Int32的限制-4,294,967,296)。

You can set your number to something like 您可以将电话号码设置为

<meaning><number><meaning><number>

and meaning is - 1 - number, 2 - letter, 3 - hyphon. 含义是-1-数字,2-字母,3-连音。

then 11 will mean 1; 那么11代表1; 201 will mean A, 3 will mean hyphon, and 113201 will mean 1-A; 201表示A,3表示次音,而113201表示1-A;

It's complicated and not very likely to be usable... 它很复杂,不太可能使用...

This solution limits your id to length of 5 numbers or 3 letters and 1 number. 此解决方案将您的ID限制为5个数字或3个字母和1个数字的长度。 You can squeez some more by using your int bit-wize and optimize your "protocol" as much as possible. 您可以使用int bit-wize压缩更多内容,并尽可能优化“协议”。

I hope this helps, Danail 希望这对您有帮助,丹尼尔

Is "E-100" a string. 是“ E-100”字符串。 ie. 即。 E is not a variable? E不是变量吗?

No, you can't set an int to a string value. 不,您不能将int设置为字符串值。

No, an int type cannot store a string. 不,int类型不能存储字符串。 But you can parse your value to an int, before passing this to your domain object for filtering. 但是您可以将值解析为一个int,然后再将其传递给域对象进行过滤。

If the "prefix" of the loan number is always "E-" you could just exclude it. 如果贷款编号的“前缀”始终为“ E-”,则可以将其排除。
Otherwise maybe you could add a property "LoanNumberPrefix" and store the "E-" in it. 否则,您可以添加属性“ LoanNumberPrefix”并将“ E-”存储在其中。

Unfortunately at some point, bad design will give you unsolvable problems. 不幸的是,糟糕的设计有时会给您带来无法解决的问题。

I don't know if this is one of them, but if the domain model has specified that loan numbers are integers, then either you, or the people that made that model clearly hasn't done their job. 我不知道这是否是其中之一,但是如果域模型指定的贷款数字是整数,那么您或创建该模型的人显然没有完成他们的工作。

Why the E in there? 为什么E在那儿? What does it signify? 它代表什么? Is it just a prefix, can you remove it when storing it and put it back before displaying it? 它只是前缀,可以在存储时将其删除并在显示之前放回去吗?

Unfortunately, if the prefix can change, so that at some point you will have F-100 and so on, then you need to find a way to encode that into the integer you send to the domain model and business logic. 不幸的是,如果前缀可以更改,以至于在某个时候您将拥有F-100,以此类推,那么您需要找到一种将其编码为发送给域模型和业务逻辑的整数的方法。

If you can't do that, you need to find a different place to store that prefix, or possibly the entire code. 如果无法做到这一点,则需要找到其他位置来存储该前缀或整个代码。

If you can't do that, well, then you're screwed. 如果您做不到,那就太麻烦了。

But to be blunt, this smells badly of someone who has been asleep while designing. 但是直言不讳,这在设计时就睡着了。

"Yeah, that's a good idea, we'll make the loan identification number an integer. I know somewhere, someplace, that someone has an example of what those loan identification numbers look like, but it's just numbers right? I mean, what could go wrong...?" “是的,这是一个好主意,我们将贷款标识号设为整数。我知道某个地方,某个地方,有人举了一个例子,说明这些贷款标识号是什么样子,但只是数字正确?我的意思是,可以出错...?”

i think thats possible if you can convert the char into ASCII code. 我认为,如果您可以将char转换为ASCII码,那是可能的。

string --- ASCII 字符串--- ASCII

0-10---48-57 0-10 --- 48-57

AZ----65-90 AZ ---- 65-90

az----97-122 az ---- 97-122

check out the ASCII table for more info.. 请查看ASCII表以获取更多信息。

Conversion: 转换:

so you can convert 所以你可以转换

RT1 to 082084049 RT1至082084049

RT2 to 082084050 and RT2至082084050和

MDR3 to 077068082051 MDR3至077068082051

i just prepend 0's to each character if the value is not 3 digit one(because max possible ASCII (z) value is in 3 digits ). 如果值不是3位数字,我只是在每个字符前面加上0(因为最大可能的ASCII(z)值是3位数字)。 R is actually 82, it becomes 082. And the final integer (no of digits) would be in multiples of 3. R实际上是82,变为082。最后一个整数(无数字)应为3的倍数。

Extraction: 萃取:

This helps to extract the info in the other end. 这有助于提取另一端的信息。 just split this into seperate 3 digit values and convert them to char and append them. 只需将其分成单独的3位数字值,然后将其转换为char并追加即可。 you wil get the final string. 您将获得最终的字符串。

082,084,049 - R,T,1. 082,084,049-R,T,​​1。 thats all. 就这样。

ps: this method may end up in arithmetic overflow problem for large strings ps:此方法可能会遇到大字符串的算术溢出问题

I suggest that you talk to someone in the business/domain layer, or who is responsible for the design of the system, and point out to them that loannumber need to be changed to a string. 我建议您与业务/域层的人或负责系统设计的人联系,并向他们指出需要将借贷号更改为字符串。 No one will thank you for bodging your code to get around what is a design flaw--it can only lead to trouble and confusion later. 没有人会感谢您合并代码来解决设计缺陷,因为它只会在以后引起麻烦和混乱。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM