Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 949507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:24:48+00:00 2026-05-15T23:24:48+00:00

I want to initialize a class with data coming from a MySql db. Some

  • 0

I want to initialize a class with data coming from a MySql db. Some fields can be null:

Dim dr As MySqlDataReader = ...
Dim item As New Item(dr.GetInt16(0), dr.GetString(1), dr.GetString(2))

Suppose the last two fields could be NULL In the db, so that calling GetString on that field causes an exception.

I could certainly write code to test for NULLs before I get each field:

dim field1 as String 
if ( dr.IsDbNull(1) )
   field1 = Nothing                  ' or even ""
else
   field1 = dr.GetString(1)

But if you have many fields this is an “ifs” nightmare.

To this purpose I rewrote the IIf VB function to make it more typed, thus to avoid casts:

Namespace Util

Public Shared Function IIf(Of T)(ByVal condition As Boolean, ByVal iftrue As T, ByVal iffalse As T) As T
        If condition Then Return iftrue Else Return iffalse
End Function

So that I could write something like:

Dim item As New Item(
     dr.GetInt16(0), 
     Util.IIf(dr.IsDbNull(1), "", dr.GetString(1), 
     Util.IIf(dr.IsDbNull(2), "", dr.GetString(2))

The typed IIf works well in other cases, but unfortunately it doesn’t in this instance, because being it a normal function and not a language keyword, each inpout parameter is evaluated during the call, and when the field is NULL the exception is raised.

Can you think of an elegant if-less solution?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T23:24:48+00:00Added an answer on May 15, 2026 at 11:24 pm

    First off, I’d recommend you to use an ORM mapper – there are very few cases nowadays when you have to do manual “mapping”.

    If this is one of these cases, I’d recommend you to use field names instead of indexes while accessing Data Reader.

    And to answer your original question: try extension methods. Sorry for C#, but VB.NET syntax drives me nuts:

    public static class DbDataReaderExtensions
    {
        public static T GetField<T>(this DbDataReader dbDataReader, string fieldName, 
            T defaultValue)
        {
            if(dbDataReader.IsDBNull(fieldName))
                return defaultValue;
            return (T)dbDataReader[fieldName];
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write some tree data structure in ruby. The class file: class
I want to initialize a static Class variable in Java: public class NumberExpression {
I want to initialize a protected member in a derived class using the constructor,
Stack Overflow. Let's say I want to initialize a non-static class of variables, two
I want to create an object, let's say a Pie. class Pie def initialize(name,
I have a class with some data members (int, string, bool, Point). These data
I want to initialize two static data members. See the two files // Logger.h
I want to know why constant data member of a class need to be
Am trying to inherit a class from a C++ vector and initialize it at
I want to get data from an API using Python. The API documentation give

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.