Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Why a string comes back as null
Message
 
 
À
03/12/2008 12:50:55
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
C# 3.0
OS:
Windows XP SP2
Application:
Desktop
Divers
Thread ID:
01365266
Message ID:
01365405
Vues:
10
>>Hi Gregory,
>>
>>>>Hi All,
>>>>
>>>>I have a method that returns a string if there is no file in a directory. I have stepped through this and watched this line get set to an empty string but when it comes back to the calling line of code, it is null. I am missing something and wonder if it is the way I did the while statement. Any ideas appreciated.
>>>>Thanks for the help
>>>>Tim
>>>>
>>>>When the return from PopQueue is set to "" then in my while statement the value of fileName is null. Why would this be?
>>>>
>>>>while ((fileName = this.PopQueue(path, filter)) != "" && llSuccess)
>>>>{
>>>>    // Do all my stuff here, process each file returned until no more files are returned.
>>>>}
>>>>}
>>>>
>>>
>>>hi Tim,
>>>
>>>There's nothing wrong with the while statement. It's just that the debugger upon returning from PopQueue() still shows the old (null) value
>>>My guess is that the assignment hasn't executed yet, so filename is still null
>>>
>>>
>>>class test
>>>{
>>>	static void Main(string[] args)
>>>	{
>>>		string fileName;
>>>		bool success = true;
>>>
>>>		while ((fileName = PopQueue()) != "" && success) // here it's still null in the debugger - after execution of PopQueue()
>>>		{
>>>		} 
>>>		// here it is ""
>>>		Console.ReadLine();
>>>	}
>>>	static string PopQueue()
>>>	{
>>>		return "";
>>>	}
>>>
>>>
>>>}
>>>
>>
>>I suspect I was jumping to a conclusion too soon as your comments make sense. I had a breakpoint on the While line and after the break each time i was Stepping Into the PopQueue() method. Whatching the directory I see no more files and PopQueue returns an empty string. After coming back to the while loop I was expecting the fileName to be "" but it was still null. Thus I still have the problem I was trying to figure out. Why do I have an endless loop in this while statement. If the PopQueue method returns an empty string I needed it to exit the loop. I had originally thought it was working, but then discovered the breakpoint continues to hit over and over regardless of the empty string. Any ideas?
>>Thanks, I think I learned something from you today and I am going to do some experiments to make sure I got it.
>>Tim
>
>Dunno, Tim, but why not returning null instead of "" if there are no files. [ the comparaison will be a bit faster ]
>

Wouldn't a return value of null imply it's unknown whether there are any files remaining in the queue, when in fact we know there are no more?
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform