1

I am trying to convert c# code to vb.net but I have difficulties in converting the sign "?" in c# to vb.net. here my code to convert

 public audit_trail Mapper(IDataReader rdr)
 {
      audit_trail audit_trail = new audit_trail();
      audit_trail.Log_id = rdr["Log_id"] is DBNull ? 0 : (int)rdr["Log_id"];            
      audit_trail.Host = rdr["Host"] is DBNull ? string.Empty : (string)rdr["Host"];
      return audit_trail;
 } 

how to convert the sign "?" to vb.net??

I would like to convert

  audit_trail.Host = rdr["Host"] is DBNull ? string.Empty : (string)rdr["Host"];

to something like

if rdr["Host"] is DBNull.Value then
  audit_trail.Host =  string.Empty 
else 
  audit_trail.Host = rdr["Host"]
end if

but the code is too long, too many line of code.. do you guys have an idea to convert to the short one?

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Fathur
  • 11
  • 4
  • 3
    See [Is there a conditional ternary operator in VB.NET?](http://stackoverflow.com/questions/576431/is-there-a-conditional-ternary-operator-in-vb-net) – BoltClock Jan 13 '12 at 21:19

2 Answers2

6

The visual basic syntax for the ternary operator is If([condition,] op1, op2).

Wooble
  • 87,717
  • 12
  • 108
  • 131
4
audit_trail.Host = If(IsDBNull(rdr("Host")), String.Empty, CType(rdr("Host"), String))
Esoteric Screen Name
  • 6,082
  • 4
  • 29
  • 38