Convert Boolean To Generic Type?

Jan 13, 2011

In VB.NET 2010, how to return a boolean value as generic type without compile warning?

View 2 Replies


ADVERTISEMENT

How To Convert Boolean Type Into On / Off Using String.Format

Jan 3, 2011

Is there are any way to convert boolean type into "ON"/"OFF" using string formatter
Like:
Dim inpValue as Boolean
Dim outValue = String.Format("0:ON;OFF", inpValue)
' should show OFF as output
I cannot use code or IFormatProvider

View 6 Replies

Convert A String To Generic Type T When T Is A Valuetype?

May 1, 2012

fast way in VB to go from a string to a generic type T constrained to a valuetype (Of T as Structure), when I know that T will always be some number type?This is too slow for my taste:

Return DirectCast(Convert.ChangeType(myStr, GetType(T)), T)

But it seems to be the only sane method of getting from a String --> T. I've tried using Reflector to see how Convert.ChangeType works, and while I can convert from the String to a given number type via a hacked-up version of that code, I have no idea how to jam that type back into T so it can be returned.

I'll add that part of the speed penalty I'm seeing (in a timing loop) is because the return value is getting assigned to a Nullable(Of T) value. If I strongly-type my class for a specific number type (i.e., UInt16), then I can vastly increase the performance, but then the class would need to be duplicated for each numeric type that I use.

It'd almost be nice if there was converter to/from T while working on it in a generic method/class. Maybe there is and I'm oblivious to its existence?

Conclusion:Testing the three provided implementations below and my original DirectCast/ChangeType form, @peenut's approach of using a prepared delegate to fetch the Parse method from a basic type works. No error checking is done, however, so implementors need to remember to only use this with valuetypes that have a Parse method available. Or extend the below to do error checking.

All runs were done on a 32bit system running Windows Server 2003 R2 with 4GB of RAM. Each "run" is 1,000,000 executions (ops) of the method to be tested, timed with StopWatch and reported back in milliseconds.

Original DirectCast(Convert.ChangeType(myStr, GetType(T)), T):

1000000 ops: 597ms
Average of 1000000 ops over 10 runs: 472ms
Average of 1000000 ops over 10 runs: 458ms

[code]....

Comparatively, peenut's approach is almost 200ms faster when executed 1,000,000 times in a tight loop, so his approach wins out.

View 32 Replies

.net - Faster Way To Convert From A String To Generic Type T When T Is A Valuetype?

Jan 6, 2011

Does anyone know of a fast way in VB to go from a string to a generic type T constrained to a valuetype (Of T as Structure), when I know that T will always be some number type?This is too slow for my taste:

Return DirectCast(Convert.ChangeType(myStr, GetType(T)), T)

But it seems to be the only sane method of getting from a String --> T. I've tried using Reflector to see how Convert.ChangeType works, and while I can convert from the String to a given number type via a hacked-up version of that code, I have no idea how to jam that type back into T so it can be returned.

I'll add that part of the speed penalty I'm seeing (in a timing loop) is because the return value is getting assigned to a Nullable(Of T) value. If I strongly-type my class for a specific number type (i.e., UInt16), then I can vastly increase the performance, but then the class would need to be duplicated for each numeric type that I use.

It'd almost be nice if there was converter to/from T while working on it in a generic method/class. Maybe there is and I'm oblivious to its existence?

Conclusion:Testing the three provided implementations below and my original DirectCast/ChangeType form, @peenut's approach of using a prepared delegate to fetch the Parse method from a basic type works. No error checking is done, however, so implementors need to remember to only use this with valuetypes that have a Parse method available. Or extend the below to do error checking.

All runs were done on a 32bit system running Windows Server 2003 R2 with 4GB of RAM. Each "run" is 1,000,000 executions (ops) of the method to be tested, timed with StopWatch and reported back in milliseconds.

Original DirectCast(Convert.ChangeType(myStr, GetType(T)), T):

1000000 ops: 597ms
Average of 1000000 ops over 10 runs: 472ms
Average of 1000000 ops over 10 runs: 458ms

[code]....

Using System.Reflection and calling InvokeMethod to get at the Parse method:

1000000 ops: 12213ms
Average of 1000000 ops over 10 runs: 11468ms
Average of 1000000 ops over 10 runs: 11509ms
Average of 1000000 ops over 10 runs: 11524ms

[code]....

Konrad's approach to generate IL code to access the Parse method and store the call into a delegate:

1000000 ops: 352ms
Average of 1000000 ops over 10 runs: 316ms
Average of 1000000 ops over 10 runs: 315ms

[code]....

peenut's approach of using a delegate to access the Parse method directly:

1000000 ops: 272ms
Average of 1000000 ops over 10 runs: 272ms
Average of 1000000 ops over 10 runs: 275ms

[code]....

Comparatively, peenut's approach is almost 200ms faster when executed 1,000,000 times in a tight loop, so his approach wins out. Although, Konrad's wasn't far behind and is itself a fascinating study of things like ILGenerator.

View 4 Replies

[2008] Changing Boolean Value In Generic List

Mar 15, 2009

Say I have a generic list that is a list of a structure.

[Code]....

I always get an error that it's a value and cannot be set. Is there a way to change it without having to make a local copy of the item, then set the item on the list to that local item?

View 3 Replies

.net - Can A Generic Version Of A Derived Class Override A Base Method Using The Generic Type

Apr 13, 2012

Consider:

Public MustInherit Class Column
Public ReadOnly Property ReturnSomethingUseful() As Object
Get
'return something useful

[code]....

But this gives the following error:

Public Overrides Function ParseValue(sValue As String) As Boolean'
cannot override 'Public Overridable Function ParseValue(sValue As String) As Object'
because they differ by their return types.

I accept that you can't do this, but I'd like to be able to preserve the semantics of what I'm. trying to do, which is to have an untyped version that deals with Object, but a typed version in derived classes that knows about the specific type T.

View 2 Replies

.net - Determining A Generic Type At Runtime In Non-Generic Class

Aug 14, 2010

I have a Journal that records entries for different types: Journal(Of ParentT)

[Code]....

View 2 Replies

Converting Boolean And Date From Generic - Designer Does Not Accept A Simple CType?

Dec 6, 2010

I am creating a 'generic input dialog': InputDialog(Of T).The idea is that I can create new instances of this dialog for different values of T. For example,I can create an InputDialog(Of String) and it displays a textbox.I create an InputDialog(Of Boolean) and it displays two radiobuttons (I could use a Checkbox, irrelevant).I create an InputDialog(Of Date) and it shows a DateTimePicker.

I do this by simply checking the type of T at run-time.In this way it is not really generic, as the dialog still has to know which type T is (which is usually not the case), but the generic is in the fact that the T can be multiple types that require a TextBox. For example, InputDialog(Of String), (Of Integer), (Of Single), (Of Double), etc, all simply display a TextBox (and it is validated later),so I still want to use generics as much as possible.Anyway, the result of the dialog is of course a property of type T. This property needs to return the value in the textbox, converted from string to T, OR the value in the DateTimePicker, converted from Date to T,OR the checked property of the 'Yes' radiobutton, converted from Boolean to T.In order to convert from any object to T I googled and found this

vb.net

Imports System.ComponentModel Public Class GenericTypeConverter Public Shared Function FromObject(Of T)(ByVal value As Object) As T Dim tc As TypeConverter = TypeDescriptor.GetConverter(GetType(T)) Return CType(tc.ConvertFrom(value), T) End Function End Class

I have to use this, I cannot simply CType a string to T since a String cannot be converted to any type T.So, my Result property looks like this at the moment:

vb.net

Public ReadOnly Property Result()[code].....

This actually works for Strings, Integers, Doubles, Singles, etc. I pass it the String in the textbox and it converts it to 'T'(note: T is then a String, Integer or Double!) just fine.So, it can convert a String to a String, Integer or Double without any problems (I am validating the text before using the Result property so it will always be a valid integer, double, etc).However, it does not work for a Boolean, nor for a DateTime. When I try that, it says "BooleanConverter cannot convert from System.Boolean" or "DateTimeConverter cannot convert from DateTime". I realize it is a bit of a strange thing, since the object already IS a Boolean or a Date(Time) so no conversion should be done at all, but this doesn't work in design-time because I need to return the objects as type T.I know that T will be Boolean when the value to be converted is a Boolean, and that T will be DateTime when the value to be converted is a DateTime, but the designer does not accept a simple CType. As I said, it does not accept this:

Return CType(rbYes.Checked, T)
because 'Boolean cannot be converted to T'.

However, I did find a way to make it work, but it seems like a complete hack... I can assign the boolean to an Object and then convert that using CType:

Dim obj As Object
obj = rbYes.Checked
Return CType(obj, T)

This works, it's accepted during design-time, and it works during run-time, but it seems very wrong... Is there no better way to handle this?

View 5 Replies

Error: Operator '=' Is Not Defined For Type 'FileInfo' And Type 'Boolean'

Aug 19, 2010

This is my

[Code]...

This is my error: Operator '=' is not defined for type 'FileInfo' and type 'Boolean'.

View 3 Replies

.net - Creating A Generic List Of A Generic Type

Jun 30, 2011

I am trying to create a list of a generic type in vb.net 2.0 framework.
This is the generic type definition:

Public Class GenericParamMap(Of T)
Public Sub New(ByVal pParamName As String, ByVal pPropValue As T)
mParamName = pParamName

[Code]....

The compiler does not allow a "T" in the method's parameter because it's not defined, but I'm not sure how or where to define it. I thought it was okay to have a generic method definition.

View 2 Replies

Generic Cannot Instantiate Instance Of The Generic Type With New?

Feb 8, 2012

I am trying to write a generic method, to avoid code duplication, which will create or activate a Form as an MDI children, based on its type. But I have to lines in error (see comments).

[Code]...

View 4 Replies

Conversion From Type 'DBNull' To Type 'Boolean' Is Not Valid

Jun 5, 2011

i have code below...but i cannot run because this error..."Conversion from type 'DBNull' to type 'Boolean' is not valid."

CheckBox1.Checked = DataGridView2.CurrentRow.Cells(10).Value
CheckBox2.Checked = DataGridView2.CurrentRow.Cells(11).Value
CheckBox3.Checked = DataGridView2.CurrentRow.Cells(12).Value
CheckBox4.Checked = DataGridView2.CurrentRow.Cells(13).Value
CheckBox5.Checked = DataGridView2.CurrentRow.Cells(14).Value

how i have to set in my table to insert input 1 or 0..

View 1 Replies

Opearator '=' Is Not Defined For Type DBNull And Type 'Boolean'

Jul 20, 2010

The following code at run time gives the error - Opearator '=' is not defined for type DBNull and type 'Boolean'.

The reason is the data at the table is set as NULL value.. How can I change it to accomodate the error.

Dim irowNo As Integer
For irowNo = 0 To DgvReturns.Rows.Count - 1
If DgvReturns.Rows(irowNo).Cells(2).Value = True Or

[Code]......

View 5 Replies

Operator '=' Is Not Defined For Type 'Char' And Type 'Boolean'

Apr 16, 2009

In my application when i click the button i'm getting this" Operator '=' is not defined for type 'Char' and type 'Boolean'." But when i test it locally there is no problem at all!

View 2 Replies

Warning: Type Library Exporter Encountered A Generic Type Instance In A Signature

May 5, 2009

I'm compiling a VB.Net 2.0 app (created in VS2008) using msbuild, and now I've added a generic return type, it's giving me the following:

Warning: Type library exporter encountered a generic type instance in a signature. Generic code may not be exported to COM.

Having just spent ages removing all of the previous warnings, I don't really want to add a new one. Any idea how to get rid of it (aside from not using generics)?I don't know what details I'd put in the attribute, or what number to put in the project-level ignore list.

View 1 Replies

Unable To Cast Object Of Type .objectquery To Type Generic.list

Sep 12, 2011

I have a question about ASP.Net, visual basic I have 2 LINQ query's, the first one works, the second one doesnt, produces a

"Unable to cast object of type 'System.Data.Objects.ObjectQuery'1[SelmaV2.Products]' to type 'System.Collections.Generic.List'1[System.String]'.

[Code]....

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

Using An Interface Defined In A Generic Type From Within The Defining Type

Feb 14, 2012

In trying to add a bit of usage variety to a generic class I'm working on, I ran into this issue with trying to cast an object into an interface instance where the interface is defined inside the generic class.

[Code]...

View 1 Replies

VB Allows The Type Parameters To Be Used As The Base Class For The Generic Type?

Mar 30, 2010

1) VB Allows non-type template parameters2) VB supports explicit specialization 3) VB allows the type parameters to be used as the base class for the generic type4) VB allows a generic type parameter itself to to be a generic 5) VB enforces that all codes are valid for all types of parametrs

View 1 Replies

Convert A Boolean To Bit Datatype?

Jul 25, 2010

I have a boolean check property of my check boxes in VB.NET. how can I send that value to SQL server having a bit data type

View 4 Replies

Convert An Int (bit Value From SQL Server) To A Boolean Value?

Aug 2, 2010

What is the best way to convert an int (bit value from SQL Server) to a Boolean value in vb.net? I am currently using Cbool, but believe this is now obsolete.

View 3 Replies

It Says "Conversion From Type 'DBNull' To Type 'Boolean' Is Not Valid."_String?

Jun 9, 2011

Imports System
Imports System.Data
Imports System.Data.SqlClient[code]....

View 9 Replies

Conversion From String To Type 'Boolean'

Apr 7, 2010

I've got a routine that checks a website my my external address. What I'd like to do is loop through the string just to get the first 15 numbers and full stops (periods). But that isn't my question, my problem is I've got this routine that isn't building because of a string conversion issue. I don't know how to correct it.

[Code]...

View 2 Replies

Asp.net - Why Boolean Data Type Is Not Working In MySQL

Feb 1, 2011

I am using MySQL 5.5 and i linked it with my ASP.net Application . i added a dataset to link my application within the MySQL stored procedure !!! the problem is when i call any stored procedure which contains BOOLEAN like this one :

[Code]...

View 1 Replies

Conversion From String To Type Boolean Is Not Valid

Oct 24, 2011

The error occurs on the line If wba.Selected = "MARKETING CODE" Then.

[Code]...

View 1 Replies

Operator Not Defined For Type DBNull And Boolean

Jul 13, 2009

I have this little problem :Operator '=' is not defined for type 'DBNull' and type 'Boolean'. This the code:
For i As Int32 = Me.DataGridViewSearch.Rows.Count - 1 To 0 Step i - 1
If DataGridViewSearch.Rows(i).Cells(6).Selected = True Then
Try
What I am trying to do is to loop threw a datagrid and find the checkboces that are checked.

View 10 Replies

Convert An Integer Into A Boolean Array Based On It's Binary Bits

Jul 14, 2009

I am looking to convert an integer into a boolean array where each item in the array corresponds to a bit in the binary representation of the integer.

MyInteger = 3 '011 in binary
to
MyArray(2) = False
MyArray(1) = True
MyArray(0) = True

Also is there anyway to assign a value to an Integer in binary or hex (like MyInteger = 1010b or MyInteger = A7h)?

View 20 Replies

Unable To Cast Object Of Type 'System.Collections.Generic.List`1[System.Int32]' To Type 'crmWebService.ArrayOfInt'?

May 14, 2012

I'm getting an error after sending a list to a web service.This is the code that is calling the web service:

Dim sProgramInterest As New List(Of Integer)
crmService.InsertProspectGetId(sProgramInterest.ToList)
But I'm getting this error.

[code].....

View 3 Replies

Why Boolean True Is Equivalent To -1 If Identifier Type In Integer

Jan 24, 2012

Dim method1 = Function(x As Integer, ByRef y As Integer) As Boolean
If x = y Then
Return True
Else

[code]....

View 3 Replies

Unable To Cast Object Of Type 'ImagesFlag' To Type 'System.Collections.Generic.IEnumerable`1[ImagesFlag]'

Jan 7, 2012

And here is the code that produces the error

CODE:

How can i cast this one?

View 1 Replies







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