Question:
Visual Basic.NET: How assign DBNull to an array element?
2007-11-13 12:20:49 UTC
1) Create a two-dimensional array “arrColors”.
2) Add four rows of data (two columns each):
The values should be as followed (please make sure you’re passing values with the types specified to array).

First Column
Value | Type

“Red” | String
“Blue”| String
DBNull | System.DBNull
“Pink” | String


Second Column
Value | Type

5 | Integer
6 | Integer
7 | Integer
DBNull | System.DBNull

I can do all of this, except the DBNull assignments. Later, I must test for those DBNull values using IsDBNull() function or the System.DBNull.Value equality. So, how do I make an assignment using DBNull as a value, when it is a data type?
Three answers:
Big John Studd
2007-11-13 13:56:43 UTC
In c# 2.0, we have nullable types. They allow you to convert DBNulls into value types like bools and ints.



Syntax for c# works like this:



int?[] nullableInt = new int?[10];

nullableInt[1] = null;



The syntax in vb 2005 for declaring a nullable type is similar:



Dim i As Nullable(Of Integer)

i = Nothing



You just need to declare an array of such.



Dim i() As Nullable(Of Integer) = New Nullable(Of Integer)(6) {}

i (1)= Nothing



Just expand this for a two-dimensional array.



Remember this only works for vb.NET 2.0 2005.
Jonny
2007-11-13 12:31:23 UTC
arrays don't work this way. If it is an array of integers than all have to be integers. I think to best help you one would need to ask why you are using DBNull. DBNull is for databases. I am gonna say just use 0.
2016-10-19 06:32:53 UTC
Vb.net Dbnull


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...