|
You need to use a format string so that he gets 0000 and not 0. Otherwise, this looks pretty good to me. I was just too lazy to do it for him
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Generating it in C# should be easy enough. Remember that a char implictly converts to int. So, you can store three chars, and a number, and increment from there.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Something like this do the trick?
<code>
public static string nextNumber(string num)
{
if (num == "")
num = "AAA-0000";
string[] parts = num.Split(new char[] { '-' }, System.StringSplitOptions.RemoveEmptyEntries);
if(parts.Length == 2) // else incorect format
{
string charPart = parts[0];
string intPart = parts[1];
int part = 0;
int.TryParse(intPart, out part);
if (part < 9999)
{
part++;
}
else
{
part = 0;
// we must now increment the char part.
bool incrementedChar = false;
int position = charPart.Length - 1;
while (!incrementedChar)
{
if (charPart[position] >= 'Z')
{
// set last char to A,
char[] chars = charPart.ToCharArray();// = charPart[position]+1;
chars[position] = 'A';
charPart = chars.ToString();
if (position > 0)
{--position;}
else
{
// we are at 'ZZZZ'
}
// and the next position to char++ (do this in the next iteration).
}
else
{
// we increment the char at this position
char[] chars = charPart.ToCharArray();// = charPart[position]+1;
++chars[position];
charPart = chars.ToString();
incrementedChar = true;
}
}
num = charPart + part.ToString("-0000");
}
}
return num;
}
</code>
|
|
|
|
|
This seems to work. Just pass in the current max key.
private string NextKey(string currentKey)
{
string ret = string.Empty;
string[] parts = currentKey.Split('-');
if (parts.Length == 2)
{
string alpha = parts[0];
Int32 numeric = Convert.ToInt32(parts[1]);
numeric++;
if (numeric > 999)
{
numeric = 0;
alpha = UpdateAlpha(alpha);
}
ret = alpha + "-" + Convert.ToString(numeric).PadLeft(3, '0');
}
return ret;
}
private string UpdateAlpha(string alpha)
{
string ret = string.Empty;
Char c = ' ';
for (Int32 i = alpha.Length-1; i >= 0;i-- )
{
c = alpha[i];
if (c == 'Z' && i != 0)
{
ret += 'A';
}
else if ((Int32)c < (Int32)'Z')
{
if (i == alpha.Length - 1)
{
ret += Convert.ToChar((Int32)c + 1);
}
else
{
ret = Convert.ToChar((Int32)c + 1) + ret;
}
for (Int32 j = i - 1; j >= 0; j--)
{
ret = alpha[j] + ret;
}
break;
}
else
{
ret += c;
}
} //foreach
return ret;
}
I tested it with this:
string key = "AAA-999";
richTextBox1.AppendText(key + Environment.NewLine);
// Int32 cnt = 0;
while (key != "ZZZ-999")// && cnt < 30001)
{
key = NextKey(key);
richTextBox1.AppendText(key+Environment.NewLine);
// cnt++;
} //while
Hope that helps.
Ben
|
|
|
|
|
Its amazing to see how there are so many ways to achive the same result.
Is there an optimal way?
ie: your else if ((Int32)c < (Int32)'Z')
could be written as
else if (c < 'Z')
and be cleaner, and skip the cast correct?
What do you see in my code that is 'sloppy' for lack of a better term. :P
|
|
|
|
|
Yes for sure it could be written as c < 'Z' I guess my coding background doesn't include C++. So I start with Cobol then SAS then Delphi (Pascal) then vb.net finally settled on C#. So with the baggage I bring to programming I always tend to cast things. In this case I am not sure if it is faster with the cast or not. I think C# actually see the Char as the ordinal value so there is probably no speed different. I am pretty sure in VB.net there would be a speed difference. Anyway, it was kind of a fun problem to work on since I had a little time.
Ben
|
|
|
|
|
So thanks for all beloved persons, who are trying to help me, i will check all solutions and notify , if i get the proper result.
Bye,
|
|
|
|
|
i have an windows application in .net 2005
i want get the logged in user name (the current user one who is logged in )
any idea?
thank u
|
|
|
|
|
WindowsIdentity.GetCurrent().Name
only two letters away from being an asset
|
|
|
|
|
I am trying to display columns as rows. The dataset has int and Double types.
I am not sure if this can be best accomplished within SQL Server / C# / .Net
This is what I want to accomplish:
Input:
val1A val2A val3A val4A
val1B val2B val3B val4B
val1C Val2C val3C val4C
Output:
val1A val1B val1C
val2A val2B val2C
val3A val3B val3C
val4A val4B val4C
Thanks for your help!
|
|
|
|
|
Hi,
I'm converting all of the database code in my app, and want to go through every class and every method to ensure that I haven't missed anything. What's a quick way to get a listing of all of my classes/methods?
(I code in VS2003/.net1.1)
Thanks,
cdj
|
|
|
|
|
Miss anything, in what way ? I often would comment out a member variable and compile, if I want to see all the places it is used.
If you want to generate a list, you'd need to write some reflection code, or look for a tool that uses reflection and generates such a list.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Have you tried viewing your class in the Object Browser? It won't give details but will list methods. What are you looking for?
only two letters away from being an asset
|
|
|
|
|
If I can print out the listing, that'll work fine - thanks!
|
|
|
|
|
I see only the option to build in C# . Is there a compile option like C++(ctrl F7) or is that not a concept in C#?
|
|
|
|
|
Save yourself (and us) some fustration and read some books or documentation before using the tool.
Build -> Build Solution or F6 if using the default VS2005 key mappings
only two letters away from being an asset
|
|
|
|
|
What is the difference between compile, and build ?
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Hello everyone,
I have a ListView that I wish to run a data search quary on it. I am able to get the raw's information once they are clicked but I was wondering if I can run a query to get the last registered item in the list without clicking on it?
as always, thank you very much for your help and have a great day.
Khoramdin
|
|
|
|
|
Can you tell more exactly what do you need? And make a sample where it will be used. Because it is not clearly understood what do you want.
What is the last registered item ?
One nation - underground
|
|
|
|
|
Hello Saikek,
Thank you for the reply. Your message came few seconds before I lose hope!
I have a ListView which I am registering and displaying some data once a particular button is clicked. Unfortunatly, the data that is going to be displayed in the ListView can be repeative and I am not intrested to register and display the same data once it has been displayed.
That is why I thought if I can run a query to see what has been registed then I can exclude the data that has already been registered and is displayed in the ListView.
Thank you very much for your help and have a great day.
Khoramdin
|
|
|
|
|
You can try the option ListView.FindItemWithText - that finds item with text, or more interestin (like lingvo style) create textbox, listbox and make event on textbox text changing:
private void searchBox_TextChanged(object sender, EventArgs e)
{
ListViewItem foundItem =
textListView.FindItemWithText(searchBox.Text, false, 0, true);
if (foundItem != null)
{
textListView.TopItem = foundItem;
}
}
One nation - underground
|
|
|
|
|
Does C# have the concept of member variables?
I notice that there is no .h file also.
Where would they get defined?
|
|
|
|
|
You mean like this
class MyClass<br />
{<br />
private string MyMember;<br />
}
only two letters away from being an asset
|
|
|
|
|
In C++ in a .h file, i can delclare a variable say 'm_myvar'.
Then i can use m_myvar, globally within the class i.e. (.cpp file).
How does it work in C#. I take it that there are no .h files.
|
|
|
|
|
.h and .cpp files are an artefact of how the C++ compilers worked. There's no reason it's required, I think C# is far nicer in this way.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|