Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to achieve the C# 'as' keyword for value types in vb.net?

Tags:

c#

.net

vb.net

Most of our development is done in vb.net (not my choice) and one frequently used code pattern uses an 'On Error GoTo' followed by a 'Resume Next' so that all database fields can be read with a DirectCast() and any DBNull values are just ignored.

The current code would be

On Error GoTo error_code
oObject.Name = DirectCast(oReader.Item("Name"), String)
oObject.Value = DirectCast(oReader.Item("Value"), Integer)
error_code:
Resume Next

C# code to replace this an enable the removal of the On Error code would be

oObject.Name = oReader["Name"] as string ?? string.Empty;
oObject.Value = oReader["Value"] as int? ?? -1;

The problem is that the vb.net eqivelent of this C# code uses a TryCast() which can only be used for reference types (nullable types are value types) whilst the C# as keyword can be used for reference and nullable types.

So in summary does anyone have an example of vb.net code that does the same thing as the C# code in a single line per database field?

-EDIT-

I've decided what I think is the best solution in our case. Helper methods would not be suitable (due to management) and we can't have extension methods as we're only using .NET 2.0 (though with VS 2008 so we get the If())

oObject.Name = If(oReader.IsDBNull(oReader.GetOrdinal("Name")), String.Empty, oReader.GetString(oReader.GetOrdinal("Name")))
oObject.Value = If(oReader.IsDBNull(oReader.GetOrdinal("Value")), 0, oReader.GetInt32(oReader.GetOrdinal("Value")))
like image 698
stevehipwell Avatar asked Apr 14 '09 08:04

stevehipwell


People also ask

Is C hard to master?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.

Is C easy to learn?

While C is one of the more difficult languages to learn, it's still an excellent first language pick up because almost all programming languages are implemented in it. This means that once you learn C, it'll be simple to learn more languages like C++ and C#.


1 Answers

Edit

Sorry for sprouting such nonsense. I relied on a posting by Paul Vick (then head of the VB team) rather than the MSDN and don't have Windows installed to test the code.

I'll still leave my posting – heavily modified (refer to the edit history to read the wrong original text) – because I find the points still have some merit.

So, once again, three things to recap:

  1. For reference types, C#'s as is directly modelled by TryCast in VB.

    However, C# adds a little extra for the handling of value types via unboxing (namely the possibilities to unbox value types to their Nullable counterpart via as).

  2. VB 9 provides the If operator to implement two distinct C# operators: null coalescing (??) and conditional (?:), as follows:

    ' Null coalescing: '
    Dim result = If(value_or_null, default_value)

    ' Conditional operator: '
    Dim result = If(condition, true_value, false_value)

Unlike the previous IIf function these are real short-circuited operators, i.e. only the necessary part will be executed. In particular, the following code will compile and run just fine (it wouldn't, with the IIf function, since we could divide by zero):

    Dim divisor = Integer.Parse(Console.ReadLine())
    Dim result = If(divisor = 0, -1, 1 \ divisor)
  1. Don't use VB6 style error handling (On Error GoTo … or On Error Resume [Next]). That's backwards compatibility stuff for easy VB6 conversion. Instead, use .NET's exception handling mechanisms like you would in C#.
like image 146
Konrad Rudolph Avatar answered Sep 27 '22 20:09

Konrad Rudolph