Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Trouble with casting
Message
De
23/09/2014 12:19:45
 
 
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:
01608064
Vues:
27
>>>Hi everybody,
>>>
>>>Do you see why this very simple code gives me trouble?
>>>
>>>
>>>var lastLocSuffix = _salespointAdapter.GetAll().Select(sp => new { LocSuffix = sp.LocSuffix }).OrderByDescending(sp => sp.LocSuffix).FirstOrDefault();
>>>            Int16 newSuffix = 1;
>>>            if (lastLocSuffix != null)                
>>>                newSuffix = Convert.ToInt16(lastLocSuffix) + newSuffix; // Doesn't like this line
>>>
>>>            salespoint.LocSuffix = newSuffix; 
>>>
>>>
>>>I am trying to increment a number by 1. The number is supposed to be short. Why does it say something about cast if I declared the number to be short?
>>>
>>>What am I missing in this code?
>>>
>>>Thanks in advance.
>>
>>What is the value of lastLocSuffix? Can it be converted to a short?
>>
>>Also you can probably replace your Select, OrderByDescending, and FirstOrDefault with just Max.
>
>I've changed the code to be
>
>
>var lastLocSuffix = _salespointAdapter.GetAll().Select(sp => new { LocSuffix = sp.LocSuffix }).Max();
>            Int16 newSuffix = 1;
>            if (lastLocSuffix != null)                
>                newSuffix = Convert.ToInt16(lastLocSuffix);
>
>            salespoint.LocSuffix = newSuffix;
>
>The LocSuffix is defined
>
>
> public short LocSuffix { get; set; } // loc_suffix. 3 digit prefix appended to each sale, pass, invoice, etc... issued by this station.
>
>So, I just need to increment it by one after getting the max from the database. If I try to add + newSuffix in the newSuffix assignment line, I get the error back.

So what is the value of lastLocSuffix? Is it actually a short, and if so, why are you wrapping it in a convert? What is the actual error message you get?

You can still get rid of the Select. Max has an overload that lets you do the select.
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform