|
Both pieces of information should have been valuable in the original question.
You don't assign a value to an identity-field. It should generate a value by itself. And a GUID will never fit into an INT. Removing the hyphen will not help.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
|
Either change the type in the database so it can hold a GUID , or let the database generate an INT .
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
identity = identity.Replace("-", "");
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
Where can i change your replace id?
|
|
|
|
|
Most major retail clothing outlets have changing rooms.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
This is work. How how can i dow that for under Column name / row?
modified 23-Feb-15 8:52am.
|
|
|
|
|
Thanks, this string work with click. Replace string dont work for datagridview, how dow i that for datagridview?
|
|
|
|
|
Hi coders
I am looking to convert alphabet entries in a char array into their corresponding place in the alphabet. E.g. A=1, B=2, C=3, and onward.
I have done some searching online but can't find a way of doing this for an array, just for individual values. I imagine it will require a foreach loop, but I can't figure out the correct terminology
If someone could help I would hugely appreciate it 
|
|
|
|
|
Well on the assumption that your string is ASCII encoded, and with the knowledge that 'A' = 65 its simply a case of casting the character to an integer and taking off 64.
You could try something like this using linq:
var input= "ABC";
var output = input.Select(x => ((int)x)-64).ToArray();
or, more clearly:
int[] output = new int[input.Length];
for (int i = 0; i < input.Length; i++) output[i] = ((int)input[i]) - 64;
Regards,
Rob Philpott.
|
|
|
|
|
That's done it, thanks for the help Rob
|
|
|
|
|
Rob Philpott wrote: var output = input.Select(x => ((int)x)-64).ToArray();
Or, more clearly:
int[] output = input.Select(x => (x - 'A') + 1).ToArray();
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I'm being stalked! Well presuming subtraction of chars yields an int I guess that's a neater answer.
Personally, I'd probably stick with my answer though due to the explicit cast. And I should state I'd never do anything as filthy as using 'var' in my own code!
Regards,
Rob Philpott.
|
|
|
|
|
Rob Philpott wrote: I'd never do anything as filthy as using 'var' in my own code!
I thought I was the only one who hated that filthy habit 
|
|
|
|
|
Tetra1044 wrote: a char array
An actual array of characters? Or a string? (Not that it matters much.)
Tetra1044 wrote: a foreach loop
for loops tend to be quicker than foreach loops, so use for whenever you can.
|
|
|
|
|
It was an array of characters, but I've changed it to a string for ease.
And thanks, I'll keep that in mind
|
|
|
|
|
At the risk of adding too much salt to the stew, let me, obsessively, add:
You don't mention if the char[] (now a string) is guaranteed to use only upper-case letters, and if the string might contain spaces. And, how about if the string contains numbers, or other white-space, or high-value Unicode characters. What about encoding ?
private IEnumerable<int> AlphaToInteger(string source)
{
source = source.ToUpper();
return source.Where(ch => Char.IsLetter(ch)).Select(chr => chr - 64);
}
«I'm asked why doesn't C# implement feature X all the time. The answer's always the same: because no one ever designed, specified, implemented, tested, documented, shipped that feature. All six of those things are necessary to make a feature happen. They all cost huge amounts of time, effort and money.» Eric Lippert, Microsoft, 2009
|
|
|
|
|
Well, now we're talking.
*Although* you could do away with that ToUpper, and the -64.
private IEnumerable<int> AlphaToInteger(string source)
{
return source.Where(ch => Char.IsLetter(ch)).Select(chr => chr & 0x9f));
}
But now we're getting silly.
Regards,
Rob Philpott.
|
|
|
|
|
Hi, I have tried to implement my own array sort for struct arrays. But I noticed that the performance was not what I expected. So I tried to find out the reason behind and made a very simple test code, using not a struct array but a normal one dimensional long array. The comparer class looks like this:
struct sortKeys : IComparer<long>
{
private static readonly Comparer<long> comparer
= Comparer<long>.Default;
public int Compare(long x, long y)
{
return x.CompareTo(y);
}
public static int GetHashCode(long value)
{
return comparer.GetHashCode();
}
}
and for comparing the Array.Sort without and with the comparer, I wrote the following:
private void compareSortsPerformance()
{
Stopwatch sw = new Stopwatch();
int Maximum = 10000000;
Random rand = new Random();
long[] la = new long[Maximum];
for (int i = 0; i < Maximum; i++)
la[i] = rand.Next();
sw.Start();
Array.Sort(la);
Console.WriteLine("Sort without icomparable" + sw.ElapsedMilliseconds);
for (int i = 0; i < Maximum; i++)
la[i] = rand.Next();
sortKeys sk = new sortKeys();
sw.Restart();
Array.Sort(la, sk);
Console.WriteLine("Sort with icomparable" + sw.ElapsedMilliseconds);
}
Array.Sort(la) is about 3 times faster than Array.Sort(la,sk). If I do the same comparison for a struct array with only one long variable inside, it takes even longer. Does anyone have an idea how to increase the speed for this first step?
Thx
|
|
|
|
|
Interesting question. I've taken your code and come to the same findings.
My suspicion is that because you are using a primitive type 'long' as the thing to sort, .NET has its own optimized method of sorting these effectively bypassing the whole IComparer/IComparable mechanism as a specific performance tuned operation.
What I mean by that is that it knows what a long is, so when sorting without a comparer specified it can use a dedicated long sort rather than something that repeatedly calls IComparer.Equals.
In your example, it gets called around about 300 million times...
Regards,
Rob Philpott.
|
|
|
|
|
Good to know or... bad to know that my code seems to be as efficient as possible... but still 3 times slower than the Array.Sort(la). I have implemented a quicksort as well and found the same performance as when I use the comparer Array.Sort(la,sk). But I read on several pages that C# uses quicksort in Array.Sort(). Using unsafe would increase the performance by about 20%, so something is strange. There must be a possibility to further increase the speed.
It would be interesting to know how they do it, because if I want to improve the speed of the struct array sort, which needs 8 times longer than the Array.Sort(la), I need to understand the mechanism behind the .NET implementation. Do you have an idea of how to get behind their mechanism?
Thx
|
|
|
|
|
|
|
Rob Philpott wrote: Well, you can use something like dotPeek to disassemble mscorlib ...
Or, go to http://sourceof.net/[^] and get the source straight from the horse's mouth.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
... which is altogether a much better idea! Good thinking.
Regards,
Rob Philpott.
|
|
|
|