Reflection - .NET: Checking If Variable Is Of Guid (nullable) Type?
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
ADVERTISEMENT
Jun 6, 2011
Scroll to the bottom, EDIT 19 onwards. See @Chris's comments also for good examples
[code]...
It's all gone quiet since Edit 19ish and @Chris reinforcing the issue with a good example (thank you Chris). I'll make the question a little easier (and attempt to tidy up all of the above), can anyone prove this is not an MS bug or similar? It seems to be something under the hood is not correctly wired up for VB.Net with regards to Nullable Structures and Events? However, all other cases using the Nullable Structures does appear to work?
View 3 Replies
Sep 15, 2010
This function loops all properties of an object to create the updatequery to save te object to the DB.
We had to make some changes to it because of the introduction of nullable properties. If the property is nullable we would like to check the 'HasValue' property. This does works when it has a value. When the property has no value we get an 'Non-static method requires a target'-error at the CBool-line
An other way to check the 'HasValue'-prop of a property using reflection?
Private Function GetUpdateQuery(ByVal obj As Object, ByRef params As List(Of SqlParameter), Optional ByVal excl As String() = Nothing) As String
Dim sql As String = String.Empty
[Code].....
View 1 Replies
May 12, 2009
how can I convert an object type to a GUID type in VB.NET?
View 3 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
Nov 15, 2011
I am getting a really bizarre error and it makes no sense at all.Basically I have a user entity which has a Password property. This property is of Type Binary, the server data type being Binary(512).I am reading this value in and converting it to a byte array. However whenever I run my code I am getting a completely unrelated error of Unable to cast object of type 'System.Guid' to type 'System.String'.This strikes me as very odd since I am not using a property or variable of type string or GUID anywhere in this procedure where it falls over.
The code is below:
Public Shared Function ValidateUser(ByVal username As String, ByVal password As String) As UserValidationMessage
Using db As New EntityMapDataContext
Dim u = From user In db.Users
Where user.Username = username
[code]....
View 3 Replies
Jan 28, 2011
So I'm writing the equality operator overload (Operator =())for a custom object, and the resulting mess of If conditionals is just an eyesore. But so far, it seems like the only sane way to check the values to as to match the specific behavior of this object.
The rules are:Num1 is required, period, and both the left and right operands and must be equal for True. Else, False.Num2 is optional, but if specified, must be present for both the left and right operands, and must be equal for True. Else, False.Num3 is optional, but can only be specified if Num2 is also present. Else, False.Num3, if specified, must be present for both the left and right operands, and must be equal for True. Else, False.
[Code]..
I also know that I could cache elements of the checks into booleans and then use those to reduce the amount of text. But that seems like a waste of cycles just for code readability. So I wanted to know if the SO community had better thoughts on reorganizing it, just in case I'm being too verbose with my checks.
View 1 Replies
Dec 16, 2009
I want to do something like this:
Dim selectedCourses As List(Of Guid) =
From item In chkListCourses.Items Where item.Selected =
True Select item.Value
But I get the error:
Unable to cast object of type
'WhereSelectEnumerableIterator2[System.Object,System.Object]'
to type
'System.Collections.Generic.List1[System.Guid]'.
The value of item is a string representation of a Guid. I'd also like the syntax for a Lambda expression.
View 1 Replies
Apr 20, 2011
I've got a problem in one of my SQL queries, the type GUID can't be used with the operator & (obviously from the title). Here's the few lines of code which I'm using:
Dim ClientsAssetID As Guid = New Guid(ClientsAssetIDTextBox.Text)
Dim Client1 As Guid = New Guid(Client1TextBox.Text)
Dim Client2 As Guid = New Guid(Client2TextBox.Text)
[code]....
ClientsAsset, Client1, Client2 and Client3 are all of type GUID, but are entered into the textboxes as strings, are converted to guid and then should all go smoothly into the query, but it's having a problem with linking everything together. The error I'm
getting is "Operator '&' is not defined for types 'String' and 'System.Guid'.How do I fix this? I've tried a few different things, such as using Ctype in the query, but they have also circled back to this error.
View 2 Replies
Mar 10, 2011
I'm using Linqer to convert SQL to Linq: Update EmployeeSite SET SiteId = Null
[Code]....
View 2 Replies
Feb 26, 2012
im workin on a task that will get the current computers Default Gatway, So i chose the easiest option, using the Registry, in most cases i find this with no problem at all, here's the code im using right now,
Public sHost As String = My.Computer.Registry.GetValue _
("HKEY_LOCAL_MACHINESYSTEMControlSet001servicesTcpipParametersInterfaces THE GUID NEEDED HERE ", "DhcpServer", Nothing)
Which works fine, if i put My GUID in where i've stated in above code, I get the right value ect..., no what i was woundering is how would i get the program to preform this task on another Computer as their GUID will be diffrent, if their away i can do this with simplicity?
View 3 Replies
Apr 14, 2010
I have this function:
Public Sub DoStuff(ByVal type as System.Type, ByVal value as Object)
End Sub
The 'value' argument is always an array of the same type as 'type'. How can I loop through the values of the array?
I'd like to be able to do something like this:
DoStuff(GetType(Integer), New Integer(){1,2,3})
Public Sub DoStuff(ByVal type as System.Type, ByVal value as Object)
//Strongly types arr as Integer()
[Code].....
View 3 Replies
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
Jul 15, 2010
I have an open source project I converted to vb.net. I solved all the errors, except for one. This is the C# line
[Code]....
View 4 Replies
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
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
Mar 23, 2010
.NET I want to clone a value type's fields. How can i set a field value on a value type using reflection (or something else dynamically)?
This works for reference types but not for value types. I understand why but I don't know an alternative.
shared function clone(of t)(original as t) as t
dim cloned as t
'if class then execute parameterless constructor
[Code]....
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
Dec 30, 2011
What I wanted to do was, given the table name (as string), use reflection to instantiate the get method for specific fields (defined as properties with Get and Set methods.
Public Property VC60() As String
Get
Return _VC60
[code].....
View 2 Replies
May 27, 2010
I am trying to write a function that has an object parameter. That object will always be an BindingList. That BindingList will be of some unknown (at design Time) class. I think I've figured out how to get the type of the collection object, but now here the tricky part. I'm trying to create a function that can handle any type of collection and be able to return an item from that collection. I need to create a new object of that type and return it from the function. [Code]
View 1 Replies