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

A

Alberto Poblacion

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.
 
A

AAaron123

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top