views:

100

answers:

2

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

+6  A: 

Yes, you would end up with the same result. It's semantically different from the VB version (you're calling ToString() explicitly instead of using CType, 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 be DBNull in the runtime) would throw an exception.

Just for the sake of being comprehensive, though, the actual C# counterpart would be this:

var receipt_date = (string)dr["RECTDT"];

As a point of style, though, try to avoid implicit typing (i.e., using the var keyword instead of saying string or int) for simple types. var is useful when the type might change in the future (and isn't important), or if the type is long and/or complex and var makes it more readable. In this instance, I would suggest:

string receipt_date = (string)dr["RECTDT"];
Adam Robinson
I'll +1 you even though I disagree with you about when to use `var`. Learn to love `var`.
Randolpho
@Randolpho: It's a matter of style; there's been loads of discussion about it on SO. Neither way is correct in any technical sense.
Adam Robinson
@Adam Robinson: See Edit for my miss-type. I didn't write the VB portion and I am trying to figure out is what **TYPE** `receipt_date` is after assigning the value to it. Does it make it a String? I am not familiar with VB so I am cautious as to what `CTYPE` is doing.
Refracted Paladin
Careful with that cast, it is *not* a substitute for CType(). CType does value conversions.
Hans Passant
@Refracted: `CType` is essentially VB's cast operator (though `DirectCast` is a better analog for a cast, but that's not the point). The type of `receipt_date` in the VB example is `object`, but it contains a value that is of type `String`.
Adam Robinson
@Hans: There is no direct analog for a C# cast in VB.NET; `CType` performs conversions that go beyond what the type might define, and `DirectCast` performs *no* conversions (closer to the actual IL cast opcode).
Adam Robinson
@Adam: So HOW would I rewrite that in C#?
Refracted Paladin
@Refracted: I would go with your original code (using `ToString()`), as casting a value that's null in the database (meaning it would be `DBNull` in the runtime) would cause an exception.
Adam Robinson
@Adam: Didn't mean to sound dense I just wanted to double check with all the other convo going on.
Refracted Paladin
Convert.ToString() does a lot that CType() does. About as much as you'd want it to.
Hans Passant
@Adam: Cool, that was going to be a follow up question, "How is `.ToString()` different from `(string)`". Thanks for the pre-emptive answer.
Refracted Paladin
@Adam: oh, I agree; it's all style. I'm just sayin'... learn to love `var`.
Randolpho
I'd also recommend `Convert.ToString()` as a better (i.e. closer) replacement to `CType`. Perhaps not in this case, but in general.
Pavel Minaev
@Pavel: Yes, in cases where the object could be an actual `null` reference, then `Convert.ToString()` eliminates the need for that check (as would `string.Format`, though we're splitting hairs here).
Adam Robinson
+1  A: 

VB's CType keyword is more or less equivalent to Convert.ToString though not exactly the same.

So the following in VB...

Dim receipt_date As Object 
receipt_date = CType(dr.Item("RECTDT"), String) 

...would be best (or most closely) translated to the following in C#.

object receipt_date;
receipt_date = Convert.ToString(dr.Item["RECTDT"]);

By the way CType(..., String) gets compiled into Microsoft.VisualBasic.CompilerServices.Conversions.ToString.

Brian Gideon