There is no correct or wrong itself in this case. There is the indicated and the most efficient. If the fields are integers, treat them as such. It may seem like you don’t have anything stopping you from using integers converted to string in this situation, but there is a "rule" in system development that I usually follow very successfully: Do the least surprising thing. Always.
Surprising, in this case, should be read as "something that generates less surprise".
In this case, if the field is whole (or long
) in the database, treat it as such in your code as well. This will generate less surprise to some other programmer, or even to yourself when you come to solve a bug in the code a few years from now. Also, use Convert.ToInt32()
in place of ToString()
it will have no difference, but just the fact that you wonder if treating the whole as a string in your code is the right one, already shows that the ideal would be neither need to ask this question and treat whole as the whole that is.
I also assure you that in DataTable
this field is integer. If you compare the Gridview ID with the id in the dataset/datatable, you may have to convert back to integer of any
There are still future features that may be affected by this unnecessary conversion made today. For example, you may need to put the Ids inside a Hashset
and if they are strings the operations will be slower (not much, because Hashset uses a hash to be found, but there is a loss of performance). You may want to sort the Ids to know which one was created before in the database (since it is Identity) and the string ordering, in addition to much slower, does not follow the numerical order: "10" < "2"
whereas 10 > 2
.
In short: while there are reasons to use integer as integer, with advantages, there are no advantages or reasons (in your case) to treat them as string.
Don’t nail pieces to yourself (and others who use your code). Do what’s least surprising. Ask yourself: What’s the most expected? That an entire field in the database is treated as a string in your code, or that is treated as an integer?
how you treat the ID has to do with the needs of your application... If you are not using for anything then neither Check this value in the object of the application
– guijob
And how do you record these Ids? How do you make sure they are unique? How do you know which one is next? Give more context.
– Maniero
from what I understand are integers that he treats in the application as string... and the doubt is whether this treatment is correct
– guijob
@J.William needs these values, because he identifies the record to be deleted, and doubts and this same, if the treatment is correct.
– Thomas Erich Pimentel
@bigown did a review on the question
– Thomas Erich Pimentel
Database ID is numeric, right?
– Maniero
See if it helps you: http://answall.com/q/136398/101, http://answall.com/q/45213/101 and http://answall.com/q/16967/101.
– Maniero
@bigown yes, on the bench is int, thank you I will read these questions, but this one is already answered, if it is whole treat as whole, no surprises, thank you.
– Thomas Erich Pimentel