Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Extending the scope of a variable
Message
De
22/05/2009 15:17:20
 
 
À
22/05/2009 14:56:52
Information générale
Forum:
ASP.NET
Catégorie:
Formulaires
Versions des environnements
Environment:
C# 2.0
OS:
Windows XP SP2
Network:
Windows 2003 Server
Divers
Thread ID:
01401587
Message ID:
01401594
Vues:
30
If the first input string start with "Address" or "DESCRIPTION" (instead of "["), your code will crash as "site" will never be instanciated.

>>>Given the following code
>>>                CSite site; //variable declaration
>>>
>>>                while ((input = sr.ReadLine()) != null)
>>>                {
>>>                    if (input.StartsWith("["))
>>>                    {
>>>                        site = new CSite();  //variable instantiation
>>>                        input = input.TrimStart('[');
>>>                        input = input.TrimEnd(']');
>>>                        site.SiteName = input;
>>>                        site.NodeType = CApplication.NodeType.Site;                        
>>>                    }
>>>                    if (input.StartsWith("Address"))
>>>                        site.SiteAddress = input.Substring(8);  // **ERROR
>>>                    if (input.StartsWith("DESCRIPTION"))                        
>>>                        site.SiteDescription = input.Substring(12);  //**ERROR
>>>This compiles with the error "use of unassigned local variable site" and refers to the site in the second two if statements. My question is how can I extend the scope of site in the first if block to those of the second two if blocks?
>>
>>Can you try
>>
>> CSite site = null; //variable declaration
>>
>>instead?
>>
>>You need to initialize the site variable with something. Also in your checks you may also check for site!=null
>
>That did it, Thanks.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform