C#中的字母数字增量 [英] Alphanumeric increment in C#
问题描述
我需要增加数据库中的产品代码(smthg lyk"BK001").
所以我写了一条sql语句,并从表n中获取了MAX值,并尝试使用(来自C#的字母数字增量,由Sandip Dalvi Tung)的逻辑对其进行递增. dsnt的工作显示了我从sql语句中获得的与gt值相同的数量.
我将sql语句("BK001")中的值gt作为字符串传递给NxtKeyCode方法
****递增类代码********************************
I need to increment a product code (smthg lyk " BK001 ") which is in a database.
so i wrote a sql statement and got the MAX value from the table n tried to increment it using the logic from ( Alphanumeric increment in C# By Sandip Dalvi Tung) ... bt wen retrive the max value n send it to the below class it dsnt work is shows the same amount which i gt from the sql statement as the MAX value.
i passed the value i gt from the sql statement ("BK001") as a string to the NxtKeyCode method
****increment class code********************************
public string NxtKeyCode(string KeyCode)
{
byte[] ASCIIValues = ASCIIEncoding.ASCII.GetBytes(KeyCode) ;
int StringLength = ASCIIValues.Length ;
bool isAllZed = true;
bool isAllNine = true;
//Check if all has ZZZ.... then do nothing just return empty string.
for(int i =0; i < StringLength-1; i++)
{
if(ASCIIValues[i] != 90)
{
isAllZed = false;
break;
}
}
if(isAllZed && ASCIIValues[StringLength-1] == 57)
{
ASCIIValues[StringLength-1] = 64;
}
// Check if all has 999... then make it A0
for(int i =0; i < StringLength; i++)
{
if(ASCIIValues[i] != 57)
{
isAllNine = false;
break;
}
}
if(isAllNine)
{
ASCIIValues[StringLength-1] = 47;
ASCIIValues[0] = 65;
for(int i =1; i < StringLength-1; i++)
{
ASCIIValues[i] = 48;
}
}
for(int i = StringLength; i > 0; i--)
{
if(i-StringLength == 0)
{
ASCIIValues[i-1] +=1;
}
if(ASCIIValues[i-1] == 58)
{
ASCIIValues[i-1] = 48;
if(i-2 ==-1)
{
break;
}
ASCIIValues[i-2] += 1;
}
else if(ASCIIValues[i-1] == 91)
{
ASCIIValues[i-1] = 65;
if(i-2 ==-1)
{
break;
}
ASCIIValues[i-2] += 1;
}
else
{
break;
}
}
KeyCode = ASCIIEncoding.ASCII.GetString(ASCIIValues);
return KeyCode;
}
public string NxtKeyCod(string KeyCode)
{
//int startint = KeyCode.IndexOf("0123456789",0,1);
StringBuilder sb = new StringBuilder();
//Regex digitregex = new Regex("^[A-Z])");
//KeyCode = digitregex.Replace(KeyCode, "");
return KeyCode;
****************************************************** *********************
请任何人能帮助我即时完成一个项目
谢谢
[edit:rjm]将代码放在< pre></pre>中标记以提高可读性[/edit:rjm]
***********************************************************************
please can anyone help me im stuck with a project
thank you
[edit:rjm]Placed the code within <pre></pre> tags to improve readability[/edit:rjm]
推荐答案
我不知道您如何对此进行测试/调试,但是当我将"BK001"传递给方法将返回字符串"BK002".
I don''t know how you tested/debugged this but when I pass "BK001" to the NxtKeyCode()
method it returns the string "BK002".
是 C#中的字母数字增量 [ ^ ]代码可以正常工作
Yup Alphanumeric increment in C#[^] The code works fine
您确定使用的是NxtKeyCode()
函数,而不是下面称为NxtKeyCod()
的函数(注意不是"e"),该函数什么也不做,只是返回已传递的内容来吗?
来自理查德·M(Richard M):我认为这个显而易见的问题不值得提出,但我敢打赌您是对的.
Are you sure you''re using theNxtKeyCode()
function and not the one below calledNxtKeyCod()
(note no ''e'') which does nothing but return what is passed to it?
From Richard M: I didn''t think this obvious question worth asking, but I am willing to bet you are right.
这篇关于C#中的字母数字增量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!