Compare Two Variables Of Type Nullable?
Jun 9, 2011
I'm trying to compare two variables of type nullable(of boolean) in VB.NET 2010. One of the variables has a value False and the other is Nothing. Now I was expecting the following expression to evaluate to true, but this is not the case:
Dim var1 as nullable(of boolean) = False
Dim var2 as nullable(of boolean)
var2 = Nothing
[Code].....
Why don't I see my MsgBox? How should I compare two nullables (of boolean)?
View 4 Replies
ADVERTISEMENT
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
Sep 15, 2010
I have a Job Register Table and that table doesn't have any records.
This is my LINQ code:
Dim QRecordCount = (From LC In CntxtJobDetails.JobRegistrations _
Where LC.JobCode <> 0 _
[code].....
View 1 Replies
Aug 18, 2010
When i execute this linq to sql command its given an error "The null value cannot be assigned to a member with type System.Int32 which is a non-nullable value type." 0 Records in my Commotidy Table
I want generate manual number and get the last CommodityCode.My Code as follow,
Dim QRecordCount = From RC In cntxtCommodity.CommodityMasters _
Where RC.CommodityCode <> 0 _
Select RC.CommodityCode[code].....
View 1 Replies
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
Mar 23, 2011
I'm trying to use the type Boolean for some parameter.
<AttributeUsage(AttributeTargets.Method, Inherited:=True, AllowMultiple:=False)>
Public Class MyAttribute
Inherits Attribute
[code].....
View 14 Replies
Nov 11, 2010
I want to get a value from an item in a DataRow and I would like to store the result in a Nullable Date field.
I see (in VB) that there appears shared "Field" function on the object System.Data.DataRowExtensions. Here's from the Object Browser:
Public Shared Function Field(Of T)(ByVal row As System.Data.DataRow, ByVal column As System.Data.DataColumn, ByVal version As System.Data.DataRowVersion) As T
Member of System.Data.DataRowExtensions
[Code].....
Does somebody have a better way to move values to and from a datarow into Nullable types?
..OR is there a built-in handy function to do this so I don't have to define my own?
View 1 Replies
Mar 3, 2011
I have the following VB.NET function, for example:
Public Function MyFunction (Of TData) (ByVal InParam As Integer) As TData
End Sub
How do I, in a function, determine if TData is a NULLable Type?
View 3 Replies
May 20, 2009
In the code base I'm working in there have a method that has the signature
Public Sub SetDropDownValue(Of T As Structure)(ByVal target As ListControl, ByVal value As Nullable(Of T))
The method I am writing is passed a parameter of type object.
How can I cast the object into something that can be passed into the SetDropDownValue method?
View 2 Replies
Dec 14, 2006
I have the following VB.NET function, for example:Public Function MyFunction (Of TData) (ByVal InParam As Integer) As TDataEnd Sub
View 5 Replies
Jul 9, 2010
I have a structure that contains another structure. The inner structure is nullable. Now wht I do is something like the following. I create a new instance of the inner structure, set the member variables and assign the whole structure to the inner structure of the parent. But it is giving an error. The assignment is not successful. If I try to peek into the structure in the watch window, it says "property evaluation failed" for the HasValue and Value properties.
Dim testData As List(Of TestData) = Nothing
Dim testData_List1 As New TestData
With testData_List1.commonTestParam
[Code].....
View 1 Replies
Oct 29, 2010
The code I want to work:
<Extension()>
Public Function NValue(Of T)(ByVal value As Nullable(Of T), ByVal DefaultValue As T) As T
Return If(value.HasValue, value.Value, DefaultValue)
[code].....
View 2 Replies
Jan 18, 2010
how to implement Nullable Parameter of type string in vb.net
Function:
sub savedetail(byval name as string, byval age as integer)
if name isnot nothing then
some work
end if
[code]....
View 1 Replies
May 27, 2009
Why does this give variable A the value of zero (instead of setting it to nothing)?Dim A as double? A=If(2<3, Nothing, 6)
View 11 Replies
Nov 30, 2011
I have a function that will be passed an object that contains MANY variables. These variables all get stored in a database. Later a new object will be passed to my function and most of the time it will be identical. But sometimes one or two variables will be different and I'll have to take various actions based on those changes. Other than looping through every variable and comparing it to every stored version of that variable, how might I do this? Can I some how generate a hash value for an entire object? Since the object will be the same most if the times it is passed to me this would be a quick way to determine that nothing has changed.
View 2 Replies
Jun 21, 2010
as the title states. Is there a way to determine if both datatypes are the same?
View 2 Replies
Aug 30, 2011
When using the If operator [URL] to assign a value to a System.Nullable object, if the result is Nothing (null), then 0 is assigned to the object. Example: 'Expected value is null (Nothing). Actual value assigned is 0. Dim x As System.Nullable(Of Integer) = If(1 = 0, 1, Nothing) If x is a nullable type, why is it being assigned the default integer type of 0. Shouldn't it receive a value of null?
View 2 Replies
Jan 16, 2012
I'm trying to check if a variable has been defined as a nullable Guid. eg.
Dim myGuid As Nullable(Of Guid) or Dim myGuid As Guid?
It seems doing a myGuid.GetType returns the underlying type, that is type Guid, not Guid?. So testing myGuid.GetType Is GetType(Guid?) always returns False.How do I find out if myGuid is a nullable type?
Ed: I can do the following, which correctly returns True for "Guid?" and False for "Guid":
Not Nullable.GetUnderlyingType(GetType(Guid?)) Is Nothing
The problem is that I don't know how to retrieve the nullable type from the variable itself, in order to test it. I've only been able to get the underlying (non-nullable) system type.
I've written a db helper function. I pass it an object comprised of public members, representing the row data of a table. The members are the table columns.Using reflection, I loop through these public members to create an INSERT statement for a command object and populate its parameters with the values in those members. So far so good.But now there's a table which has a uniqueidentifier column which I must not populate from the row object, as it defaults to "NEWID()" (using SQL Server 2008). Instead of skipping all Guid columns, which would be easy, I only want to skip ones defined in the row data class as "Guid" (non-nullable).Basically, I'm using the Guid? (Nullable) type to indicate it's ok to populate that uniqueidentifier column with data. If it's non-nullable, that tells me to skip it because the column has a NEWID() default value.
View 3 Replies
May 23, 2009
how do i compare 2 string variables to determine if they are the same?ie:
if(string1 = string2 AND string3= string 4) then
perform operation
else
perform another operation
end if
View 2 Replies
May 4, 2011
I have 12 Varibales in VB.Net.
If a = 1 _ Or b = 2 _ Or c = 3 _ Or d = 4 _ Or e = 5 _ Or f = 6 _ Or...... Then
Like That....
For that my string will be so long for 12 varibles...
SO is there is any other way to compare 12 variables
View 2 Replies
Jan 6, 2012
I created a SRT program in vb6. works great. I thought it would be a good time to port it over to vb2010 for obvious compatibility issues. the following will not work, even though through debug; all the vars are correct - it just wont come up.
' Timer1.Stop() : MsgBox("start: " & CaptionStart & " end: " & CaptionEnd & " smptime: " & WMPTime) : End
If (CaptionStart <= WMPTime) And (WMPTime <= CaptionEnd) Then
txtCaption.Text = Caption
'// HIGHLIGHT IT IN THE LIST
[code]....
in the above its simpler (why I like vb6) and works perfectly. I am most likely "not seeing the forest for the trees" syndrom.
View 16 Replies
Mar 17, 2010
So I'm building an application that is going to do a ton of code generation with both C# and VB output (depending on project settings). I've got a CodeTemplateEngine, with two derived classes VBTemplateEngine and CSharpTemplateEngine. This question regards creating the property signatures based on columns in a database table. Using the IDataReader's GetSchemaTable method I gather the CLR type of the column, such as "System.Int32", and whether it IsNullable. However, I'd like to keep the code simple, and instead of having a property that looks like:
[Code]...
View 2 Replies
Sep 22, 2009
I have an object that I know was originally declared was an enum, but I need tothe specificnum type. I'm aware of the GetType method and it appears to work correctly, but I can't figure out how to go from there to a direct type comparison For reference types, you can use Typeof(object1) Is Class1, but this doesn't seem to work for value types.
View 3 Replies
Mar 11, 2010
I am currently working on a custom data type, and wanted to implement a = operator.
I data type is a nullable date that returns a string value if date was supplied.
I want to allow the variable to be set like this
Dim nd as New NullableDate nd = Today Does anyone know how this can be done, or can anyone point me in the right direction?
View 9 Replies
Oct 4, 2010
Dim nullInt As Int32?
Console.WriteLine("1: " & nullInt) 'has no value
nullInt = If(False, Int32.Parse("12345"), Nothing)
Console.WriteLine("2: " & nullInt) 'has value 0?
nullInt = If(False, Nothing, Nothing)
Console.WriteLine("3: " & nullInt) 'has no value?
View 17 Replies
May 8, 2012
I am setting up a game for my final visual basic program in my computer programming class, and it is supposed to compare what you type into a textbox to a randomly generated password, and then return whether the last character you entered had a higher, lower or the exact same ascii value as the corresponding character in the randomly generated password. Well, in order to test it, I set up labels to compare the two characters... when you type in ALT + 125, the ascii value returns 125, as it should, and it says that the ascii value of the correct character is 142. I typed in ALT + 142, and the character's ascii value, according to what the label puts out, is 196... obviously its converting dec into hex or oct or something... but how do I fix it?I can also tell you that when I typed in 196 to see if it was reversed or something stupid like that, it printed that the ascii value was 49... what's up with that?!
View 4 Replies
Jan 12, 2011
What's a sound approach to override the CompareTo() method in a custom class with multiple approaches to comparing the data contained in the class? I'm attempting to implement IComparable(Of T) just so I have a few of the baseline interfaces implemented. Not planning on doing any sorting yet, but this will save me down the road if I need to.
Reading MSDN states mostly that we have to return 0 if the objects are equal, -1 if obj1 is less than obj2, or 1 is obj1 is greater than obj2. But that's rather simplistic.
Consider an IPv4 address (which is what I'm implementing in my class). There's two main numbers to consider -- the IP address itself, and the CIDR. An IPv4 address by itself is assumed to have a CIDR of /32, so in that case, in a CompareTo method, I can just compare the addresses directly to determine if one is greater or less than the other. But when the CIDR's are different, then things get tricky.
Assume obj1 is 10.0.0.0/8 and obj2 is 192.168.75.0/24. I could compare these two addresses a number of ways. I could just ignore the CIDR, and still regard as obj2 as being greater than obj1. I could compare them based on their CIDR, which would be comparing the size of the network (and a /8 will trump a /24 quite easily). I could compare them on both their numerical address AND their CIDR, on the off chance obj2 was actually an address inside the network defined by obj1.
What's the kind of approach used to handle situations like this? Can I define two CompareTo methods, overloaded, such that one would evaluate one address relative to another address, and the second would evaluate the size of the overall network? How would the .NET framework be told which one to use depending on how one might want to sort an array? Or do some other function that relies on CompareTo()?
View 1 Replies
Jul 12, 2011
Just wondering what everyone uses to name variables that relate to an already descriptive class? [code]...
View 2 Replies
Dec 29, 2009
What happens if I do: Dim x As New Int32? Is a object of int placed on the heap? If it is, maybe your application would be able to access global variables faster if they were reference types.
A global variable as an value type should be somewhere at the bottom of the stack most of the time and it might take more time to get to this then to get a object from the heap. Unless it's the same story with the pointer of this object is also at the bottom of the stack of course.
View 9 Replies
May 26, 2010
I'm trying to create an object Of a specific type. I've got the following code, but it fails because it can't cast the new table object to the one that is already defined. I need table to start of an IEnumerable type so I can't declare is an object.
Public sub getTable(ByVal t as Type)
Dim table As Table(Of Object)
Dim tableType As Type = GetType(Table(Of )).MakeGenericType(t)
[code]....
is there a way of changing a variable type at runtime?
View 3 Replies