Add Together Two Nullable Types?

Aug 27, 2010

I have: Dim nVar1 As Long?Dim nVar2 As Long?Dim nVarSum As Long?nVar1 = Nothing nVar2 = 5 nVarSum = nVar1 + nVar2 I would prefer the result to end with nVarSum being 5, instead of Nothing. I understand if you add something to an unknown value, you will end up with "somthing + unknown" or x+5 will always equal "x+5" not "5" because you are still carrying around that unknown "x". However, how can I effectively treat an unknown or Nothing as a zero for the purposes of addition in this case?(What is basically happening is that the end user is sending us a data file, this code parses that file and then sums together about 15 fields. If the user leaves those fields blank instead of assigning a zero to them, I need to treat it as if it was a zero for this one addition operation, but all the rest of the code needs to continue seeing it as a Nothing value since the user did not ACTUALLY submit zero... they submitted blank or nothing)

View 3 Replies


ADVERTISEMENT

Can Nullable Types Be Used

Jun 27, 2012

Can Nullable Types be used in VB.NET? If so, is it possible to have a Nullable Integer that I can use with a field that accepts NULL in SQL Server?

View 4 Replies

How To Use Nullable Types

Aug 25, 2010

I'm converting an Access VBA app to VB.NET. It has dates defined as variants or objects to handle null values. I thought this would be a great chance to use the Nullable type. But I can't get it to work. Either it is inappropriate for what I am trying to use it for, or I am doing it wrong. Anyone know how to use it? Reader is a SqlDataReader.

[Code]...

I was hoping I didn't have to put a lot of If .. Null statements throughtout the code, but that is my only option unless someone has any other idea.

View 11 Replies

C# 'AS' Equivalent For Nullable Types

Feb 15, 2011

C#'s 'as' keyword will let you do this: int? input = value as int? Here's what I would assume the vb.net equivalent would be: Dim input As Integer? = TryCast(value, Integer?) There's an intellisense error in the TryCast stating the operand must be a reference type but Integer? is a value type.

Intellisense on Nullable(Of Integer) says 'Represents an object whose underlying type is a value type that can also be assigned null like a reference type.' It seems C#'s 'as' handles this like a reference type where TryCast doesn't have this built in. In VB10 I was able to take advantage of the new CTypeDynamic function to do the casting. Conversion.CTypeDynamic Method

Dim input As Integer? = CTypeDynamic(Of Integer?)(value)

Or:

Dim input As Integer? = CTypeDynamic(Value, GetType(Integer?))

There's a cost here as CTypeDynamic examines the type at runtime.

My question is what is the elegant way to handle this without CTypeDynamic?

View 6 Replies

C# 'AS' Equivalent For Nullable Types?

Nov 15, 2011

Dim input As Integer? = TryCast(value, Integer?)There's an intellisense error in the TryCast stating the operand must be a reference type but Integer? is a value type.Intellisense on Nullable(Of Integer) says 'Represents an object whose underlying type is a value type that can also be assigned null like a reference type.'It seems C#'s 'as' handles this like a reference type where TryCast doesn't have this built in.

View 1 Replies

C# - Insertion Of Nullable Types Into Database

Aug 24, 2011

I have recently been working on a number of sections of code that deal with the insertion of Nullable types into a database. As I'm sure anyone who has dealt with similar code will be aware of the annoyance of constantly writing conditional logic to deal with the insertion of nulls into a database. I.e.:
MyValue.HasValue ? MyValue.Value : DBNull.Value;
If(MyValue.HasValue, MyValue.Value, DBNull.Value)
Basically I am just wondering why DBNull.Value exists and why Null simply couldn't be used?

View 1 Replies

C# - Nullable Types And Properties With INotifyPropertyChanged?

Jan 17, 2010

It seems like overkill to set the value of a nullable type and implement iNotifyPropertyChanged. Is there a better way of doing this?

[Code]...

View 2 Replies

Compare Nullable Types In WHERE Clause Of Linq To Sql .NET?

May 4, 2012

I am trying to query for records where that column IS NULL:

Dim UnassignedSubSvc =
From c In CurrentContext.SubService
Where c.Product.ProductSubServiceId **is null**
Select c).ToList()

View 1 Replies

Difference Between Using .GetValueOrDefault(0) And If(variable, 0) With Nullable Types?

Mar 9, 2010

Is there any difference between the 2 methods below for calculating c ... specifically boxing/unboxing issues?

Dim a As Integer? = 10
Dim b As Integer? = Nothing
Dim c As Integer
' Method 1

[code]....

View 3 Replies

Error Querying When Nullable Types In Where Clause

Mar 1, 2012

I have the following NHibernate Linq query:
From eachLine In myNhSession(Of SamplePoco)()
Where eachLine.SampleIntField = 1234
The property SamplePoco.SampleIntField is type Nullable(Of Int32)

When I run the query, I get the following exception:
System.InvalidCastException: Unable to cast object of type 'NHibernate.Hql.Ast.HqlCoalesce' to type 'NHibernate.Hql.Ast.HqlBooleanExpression'
If I change the property type to Int32, it works. It seems that Nullable types are automatically converted into a coalesce expression by the Linq compiler.

Debugging the NHibernate, I just found out that this Where clause was converted into: {where ((eachLine.SampleIntField == 1234) ?? False)}. As I can understand, the whole condition comparison was translated to be coalesced instead of just the Nullable property.
If I put this way eachLine.SampleIntField.Equals(1234) it doesn't work as well ('Equals not implemented' exception)

If I change the query to the following code, it works:
From eachLine In myNhSession(Of SamplePoco)()
Where {1234}.Contains(eachLine.SampleIntField)

Another code that works as well (coalescing the field properly as I was expecting by the first query):
From eachLine In myNhSession(Of SamplePoco)()
Where If(eachLine.SampleIntField,0) = 1234

View 1 Replies

Properly Use Nullable With Numeric Types In Constructors?

Jan 9, 2011

I have a class that implements a range of numbers....call it NumericRange(Of T). Internally, NumericRange stores T as a Nullable, T?. I have another class that wraps this class as NumericRange(Of UInt16). Call this class MyNumRange (I'm being simplistic here). So In MyNumRange, I have a few constructors defined:

[Code]....

View 2 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

VS 2008 BLL - Nullable Types And Crystal Reports .Net

Jul 21, 2011

I'm using my data objects in a crystal report. The thing is, when I select my object, it gets added to the field explorer table. My properties are listed except for the Nullable Type properties. Why would this happen. I've had quite a few issues yesterday as this is my first attempt at a crystal report in .Net. I thought I worked around them but now I've hit this snag.

View 11 Replies

C# - Get A Technical Reason For No Implicit Conversion From DBNull To Nullable Types?

Jan 10, 2012

Is there a technical reason why there is no implicit conversion from DBNull to the various nullable and/or sql types? I understand why the conversions don't currently happen, but don't understand why an implicit conversion wasn't created at the time or added in subsequent versions of the framework.Just to be clear, I'm looking for technical reasons, not "because that's they way they did it" or "I like it that way".

View 2 Replies

DB/Reporting :: Crystal Reports & Entity Framework 4 With Nullable Types

Jul 6, 2011

I have written an application which manages some bookings, and want to use Crystal Reports to provide the reporting engine. I have used it before with datasets with no problems.

I am using EF4, and have a LINQ query and report datasource assignment as below:

Code:

Dim ActiveCustomers = From Customer In FMEntities.Customers
Where Customer.Status = 1
Select Customer

[Code].....

I have read up that I may need to convert the incoming data, but I am confused as to what it needs to be converted to. Should it be a datatable?

View 1 Replies

Cast Value Type To Nullable Enumeration Type In Generic Object If All Types Are Unknown At Write-time?

Dec 14, 2011

I have a generic Class I'm using to hold information loaded from a database.I have a method which takes a DataRow as an argument, uses the object's known column name and extracts the data from the DataRow, such that:Dim loadData As T = CType(myDataRow("myColumnName"), T))works as my default assignment in most cases.Unfortunately, due to some horrifying design constraints, some of my columns may be null, and may also be taken from enumerations.This means that when <T> is Nullable(Of SomeEnumeration) the above code does not work because I can't cast 0 directly to SomeEnumeration.Zero.Is there some way to check whether <T> is Nullable(Of [Enum])? Or some way to write a method which allows Integers to be cast to Nullable(Of [Enum])?I feel like I'm forgetting something that would allow me to write one of the other of these, but my weak google-fu is turning up nothing.

EDIT: Okay, thanks to dasblinkenlight's answer below, I can detect when this circumstance is occurring, but what I need to do now is to take a type <T> which I know is Nullable(Of SomeClass), get a type reference to SomeClass and then create a new object of type Nullable(Of SomeClass) and assign that to LoadData.My problem was that I had a lot of difficulty in finding any function which would accept baseType as an actual Type.Parse accepted baseType as a parameter, I knew baseType was an [Enum] type because of dasblinkenlight's code, so I was, in this instance, able to code a solution. It's a solution which is very specific to my problem (i.e., T is Nullable(of SomeEnumeration)), but it's a solution nonetheless.

View 2 Replies

Error - Soap Serializer Does Not Support Serializing Generic Types : System.Nullable`1[System.DateTime]

Oct 2, 2009

Im working on my first n-tier application. I am trying to serialize a structure and Im getting an error"Soap Serializer does not support serializing Generic Types : System.Nullable`1[System.DateTime]."Here is the structure that is being serialized
Namespace Structures

<Serializable()> _
Public Structure structAllergy
Public AllergyID As String
Public ProfileID As String

[code]....

The bold line is the line that is throwing the error.

View 1 Replies

C# :: Determining Object Equivalence For Value Types, Reference Types And ILists?

Nov 1, 2009

I have a class with a Property called 'Value' which is of type Object.Value can be of any type, a structure, a class, an array, IList etc.My problem is with the setter and determining whether the value has changed or not.This is simple enough for value types, but reference types and lists present a problem.For a class, would you assume that the Equals method has been implemented correctly, or just assume that the value has changed every time the setter is called?If I did assume it's changed, then perhaps I should assume it for value types as well, so that the behaviour is consistent.

View 2 Replies

Pass A Nullable Valuetype Into A Non Nullable Valuetype?

Sep 17, 2009

I accidently wrote some code today that was like this[code]...

I immediately noticed the issue, but I had already hit the run button. It compiled successfully, I ran it through to the section and it threw an exception.

You can't do this in C#, it gives a compile error "cannot convert from 'int?' to 'int'".

Is there an 'Option Explicit' type switch that I can turn on to ensure that this sort of error does not occur again?

View 1 Replies

Compare Two Types In Dictionary Of Types .net?

Jun 23, 2012

I need to compare two dictionary values if the types stored are equal, this is what i have

if gettype(Args(key)) = gettype(argtypes(key)) then
'' do something
end if

[Code]....

View 1 Replies

.net - Nullable Object Must Have A Value Vb?

Dec 5, 2011

I have a bigger code block which I have recreated in this simpler example:

[Code]...

View 1 Replies

Nullable - What Does DataType Mean In .NET

Sep 15, 2011

I have the following line of code:

CType(IIf(CBool(product.IsDiscontinued Is Nothing Or product.IsDiscontinued = True), False, True), Boolean?)

What does the Boolean? mean at the end. I have seen it used on other data types as well.

View 3 Replies

Nullable DateTimePicker?

May 6, 2009

I have a Nullable DateTimePicker but it lacks a vital property, that is to be able to change its ForeColor, anyone who has some suggestion on how can this be done? Or perhaps a free nullable DateTimePicker out there that supports changing of backcolor and forecolor?This is C# but it should not be different with .Net, and I prefer posting it here since there is more traffic here over the C# section.

using System;
using System.Collections.Generic;
using System.ComponentModel;[code].....

View 2 Replies

Nullable Object Must Have A Value

Jan 31, 2012

Okay, none of the previous questions I have seen with this error seem to apply in this situation.tEmp is a class that contains (among other things) two nullable date fields.[code]During a routine on my form, I am attempting to see if the following conditions are true[code]I get the above error (the title of question), but when I hover over the debugger, NextReview and InsuranceEligibleDate are both Nothing (which is correct for the employee I am looking at).I am using VB2010, and the Properties in the Employee class are using the new way of declaring Properties (i.e. no set/get)

View 4 Replies

.NET: Why Doesn't This Nullable Work

Dec 15, 2010

dim str as nullable(of string) is this syntax incorrect?

View 1 Replies

C# - Comparing The Nullable DateTime's?

Nov 17, 2010

I am a c#/asp.net developer and I am having to work on a VB/asp.net. I have two variables

Dim originalDate as DateTime?
Dim newDate as DateTime?

Both nullable datetimes, originalDate is a nullable date I am getting from the database and newDate time is set in code, I need to compare them, they can either both have dates, neither have dates or one have and one not.

I have a bit of code as follows:

if origEndDate = origEndDate then

When both origEndDate and origEndDate are "nothing" this statement is false (well when I run it in the watch window it comes back as nothing)!I don't understand why this is the case because I was under the impression doing an "=" compares the two values and as they are the same surely it should be true? What syntax should I be using as in C# I can do the above as so:

if (origEndDate == origEndDate) { }

and it will come back as true.

View 4 Replies

C# - Correct Way To Check If Nullable Has A Value?

May 8, 2011

assuming v is a nullable, I'm wondering what are the implications / differences between these usages:

VB:

If v Is Nothing Then
If v.HasValue Then

C#:

if (v == null)
if (!v.HasValue)

View 4 Replies

Marking Parameters As Nullable?

Dec 22, 2009

I've picked up some code from a colleague, and in one of his methods he has a boolean parameter: ByVal showProactiveChases As Boolean?. I had to look up the ? operator yesterday to see that it denotes a Nullable type. My question is, if I change it to: ByVal showProactiveChases As Nullable(Of Boolean), does the meaning remain the same? I think provided it doesn't change the meaning the second way is much more readable.

View 4 Replies

Property Keycode Is Non-nullable?

Apr 7, 2009

I am creating a setup project for my VB application. Everything seems to look fine. However when I tell it to build the solution I get the following error:Property 'keycode' is non-nullable. I have never seen this before in other setup projects and I can't find any help from MS.

View 8 Replies

Use Nullable(of T) For Property Using List<of T>?

Jan 17, 2012

In the below sample code, I have tried to use Nullable(of T) for DateTime and I want to use it same of the Dimensions property which is List(of Dimension).

CODE:

View 1 Replies







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