Coalesce The Value Using IIf?

Sep 14, 2011

I'm experiencing unpredicted effects with nullables in VB.net. The object in question has a property defined: Public Property Value As Int32?

When I try to coalesce the value using IIf, I get a null exception

cmd.Parameters.AddWithValue("@HOValue", IIf(headOffice.Value.HasValue, headOffice.Value .Value, DBNull.Value))

In C#, I know there's no implicit conversion for nullables, hence you can't use ??, but why is the first part of the IIf being evaluated in VB.NET?

View 3 Replies


ADVERTISEMENT

C# - Coalesce / Tokenize A SQL Command

Mar 1, 2012

I would like to break a "SELECT" SQL statement into its logical components. i.e. I would like to create an object like "SelectSqlStatement" which has a property called "Table", "Where", "OrderBy", etc. The reason I want to do it is that I don't want to manipulate a string but rather manipulate an object and serialize it back to a string. Before I write one for .NET, I was wondering if there was one available. I did a search but didn't see anything.

View 4 Replies

C# - Null Coalesce Operator In .Net(8)?

Jan 4, 2011

i'm afraid that this is a stupid question, but i must assume that i have programmed VB.Net too long and now can't figure out how to convert this C# null coalescing operator into VB.Net:

if( Convert.ToBoolean(ViewState[tp.UniqueID + "_Display"] ?? true) == false ){}

I know the IIF-Function but i'm not sure how to use it here and if it gives the correct result(in IIF both expressions are being evaluated). Please help to shed light on the dark.EDIT: if you want to see the source of this: forums.asp.net There you can see a solution that generates a Option Strict On disallows implicit conversions from 'Object' to 'Boolean' compiler exception.

View 7 Replies

Chain A Null Coalesce Operator?

Oct 25, 2011

I have a class Customer which contains the property Extensions which in turn contains the property Any. I tried to do: Dim room = If(customer.Extensions.Any.ElementAt(0).InnerText, Nothing) but it threw an error when it did not find an Extension element in the incoming xml. I thought it would return nothing once it saw that the first expression was Nothing. Do I have to do a multiple if statement in this case?

View 1 Replies

Use Coalesce With Db Column Values And Nullable Types?

Dec 8, 2011

I'm trying to do something similar to what's described here, but with nullable types.

[URL]

int availableUnits = unitsInStock ?? 0;

In VB, it would be this:

Dim availableUnits As Int32 = If(unitsInStock, 0)

However I'm working with db columns, which could be DbNull, and nullable types, which can be Nothing (which is different to DbNull). If a column is DbNull, I want to return Nothing, otherwise return the value. For eg:

Dim availableUnits As Int32? = If(myDataReader("UnitsInStock").Value, Nothing)

The error I'm getting is "Specified cast is not valid" but I'm not sure why. I've tried this as well:

Dim availableUnits As Int32? = If(isDbNull(myDataReader("UnitsInStock").Value), myDataReader("UnitsInStock").Value, Nothing)

Which is messy and just results in the same error. The only thing that works is this:

Dim availableUnits As Int32?
If isDbNull(myDataReader("UnitsInStock").Value) Then
availableUnits = myDataReader("UnitsInStock").Value
Else
availableUnits = Nothing
End If

Which is just silly. Is there a better way of getting nullable db values into nullable variables that I'm not aware of?

View 3 Replies

Linq To Sqlite - This Returns The Error "Coalesce Used With Type That Cannot Be Null"?

Sep 17, 2010

I am using vb.net sqlite.net and dblinq0.20.1 to search a sqlite table on a primary key field.My code looks like this

Dim blb = (From d In db.Data Where d.UID = myuid Select d).Single

This returns the error "Coalesce used with type that cannot be null" .If I search on a nullable field it works fine.

View 1 Replies

Coalesce Operator And Conditional Operator In .NET?

Mar 10, 2009

Possible Duplicate: Is there a conditional ternary operator in VB.NET?

Can we use Coalesce operator(??) and conditional ternary operator(:) in VB.NET as in C#?

View 5 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved