Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Trouble with casting
Message
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01608054
Message ID:
01608088
Vues:
24
>>>>In the database the column is smallint and defined as short in the class definition.
>>>
>>>So why is the Convert.ToInt16 needed?
>>
>>I think it's not needed, I'll remove it. In my original code it was needed as the result was anonymous type.
>>
>>So, the code now is reduced to
>>
>>
>>var lastLocSuffix = _salespointAdapter.GetAll().Max(sp=>sp.LocSuffix);
>>            Int16 newSuffix = 1;
>>            if (lastLocSuffix != null)
>>            {
>>                newSuffix = lastLocSuffix++;                
>>            }
>>
>>            salespoint.LocSuffix = newSuffix;
>
>The ++ operator after the variable is done after the assignment (that is, the assignment happens first, then lastLocSuffix is incremented). So if GetAll().Max() returned 1, newSuffix would be assigned a 1 (not a 2).
>
>You would want:
>
>newSuffix = ++lastLocSuffix;
>
>
>I'd actually end up trying to write the above as:
>
>
>var lastLocSuffix = _salespointAdapter.GetAll().Max(sp=>sp.LocSuffix) ?? (short)0;
>salespoint.LocSuffix = ++lastLocSuffix;
>
>
>actually, this is probably easier to understand and avoids the assignment to lastLocSuffix which probably isn't needed anyway:
>
>
>var lastLocSuffix = _salespointAdapter.GetAll().Max(sp=>sp.LocSuffix) ?? (short)0;
>salespoint.LocSuffix = lastLocSuffix + 1;
>
The last code doesn't compile. It says operator ?? can not be applied to short and short.
If it's not broken, fix it until it is.


My Blog
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform