The below is a snippet from VB that I am porting to a C# rewrite. My question is what is receipt_date after the assignment? Is it still an object or is it a string?
Dim receipt_date As Object
receipt_date = CType(dr.Item("RECTDT"), String)
Would this be the correct C# counterpart?
object receipt_date;
receipt_date = dr["RECTDT"].ToString();
After both of these execute would the VB version, receipt_date be equal to the C# version? If not, what do I need to do to make it so? Thanks
Yes, you would end up with the same result. It’s semantically different from the VB version (you’re calling
ToString()explicitly instead of usingCType, which is loosely equivalent to a cast in C#), but it’s functionally identical. It’s also safer, since casting a null value in the database (which would beDBNullin the runtime) would throw an exception.Just for the sake of being comprehensive, though, the actual C# counterpart would be this:
As a point of style, though, try to avoid implicit typing (i.e., using the
varkeyword instead of sayingstringorint) for simple types.varis useful when the type might change in the future (and isn’t important), or if the type is long and/or complex andvarmakes it more readable. In this instance, I would suggest: