How to initialize a string variable to a length of 500?

  • Thread starter Thread starter AAaron123
  • Start date Start date
AAaron123 said:
Alberto Poblacion" (myself) said:
string shortestPath = "";
[...]
if (s.Length<shortestPath.Length)
shortestPath = s; //this copies a reference
[...]
When you enter the loop shortestPath.Length equals zero, doesn't it?
You will never find a lower value in s.Length.

Ooops! Sorry!. This is what happens when I type a piece of code out of
memory without testing it, and I concentrate on a different aspect of the
code (the fact that it was copying references instead of copying the
strings).
A couple of solutions have already been proposed in other messages:
Either use an int to represent the length and preload it with int.MaxValue,
or preload the shortestPath with an existing path from the array.
 
Alberto Poblacion said:
AAaron123 said:
Alberto Poblacion" (myself) said:
string shortestPath = "";
[...]
if (s.Length<shortestPath.Length)
shortestPath = s; //this copies a reference
[...]
When you enter the loop shortestPath.Length equals zero, doesn't it?
You will never find a lower value in s.Length.

Ooops! Sorry!. This is what happens when I type a piece of code out of
memory without testing it, and I concentrate on a different aspect of the
code (the fact that it was copying references instead of copying the
strings).
A couple of solutions have already been proposed in other messages:
Either use an int to represent the length and preload it with
int.MaxValue, or preload the shortestPath with an existing path from the
array.

I appreciate you taking the time to reply.
Thanks
 
Back
Top