Use A Generic That Only Accepts Integer Types?

Mar 5, 2010

Is there a way to use a generic that only accepts integer types?

I have a Clamp(value, min, max) function, and it would be nice if it could accept ints, uints, floats, shorts, ect without having to write an individual function for each type.

View 1 Replies


ADVERTISEMENT

Write A Generic Function In .NET That Only Accepts Numerical Types?

Jun 4, 2009

Suppose I want to write a function like the following (as usual, a trivial example for illustrative purposes):

Public Function calcSqSum(Of T)(ByVal list As IEnumerable(Of T)) As T
Dim sumSq As T
For Each item As T In list

[Code]....

As you can probably guess, this function causes an error because a generic object is not guaranteed to implement the + operator. As far as I know, though, any numerical type (Integer, Double, Decimal, etc.) will.

Is there a way to write a (quasi-)generic function that can accept any numerical type, without having to explicitly overload the function for every such type yourself?

Alternatively, I suppose an equally acceptable solution would be to somehow check if a type implements the '+' operator (or any operator generally associated with numerical types and used by the function).

View 4 Replies

If Imports System.IO Accepts All Files Types

May 28, 2009

Can anyone tell me if the Imports System.IO accepts all files types?? What I mean is, can I use it to access .csv, .xls or .doc files? And one other thing.Do I need to include the file location in the body of the procedure/function??

View 18 Replies

Get This Message : Operator '+' Is Not Defined For Types 'integer' And '1-dimensional Array Of Integer'?

Sep 25, 2010

ive got some simple problem i cant understand how to solve.

str(1) is an array and integer(or double, tried both)
a(1) is an array and integer (or double, tried both)
a(1) = a(1) + 1
Label1.Text = str(1) + a

the big part is where i get: operator '+' is not defined for types 'integer' and '1-dimensional array of integer'.why do i get that message and what does it mean? how do i make the code do what i want?

View 1 Replies

Operator '>=' Is Not Defined For Types '1-dimensional Array Of Integer' And 'Integer'

May 9, 2012

this is for a BINGO game final project that I already turned in but this just bugged me that I could not get the sorting figured out. I am trying to get this to sort the random numbers in to columns under the correct letters but I keep getting the error Operator '>=' is not defined for types '1-dimensional array of Integer' and 'Integer' under the line:

[Code]...

View 5 Replies

Develop An Application Which Accepts A Positive Integer Value From The User?

Nov 8, 2011

develop an application which accepts a positive integer value from the user and outputs the sum of all integers between 0 and the given integer.

View 7 Replies

.net - Operator '=' Is Not Defined For Types 'Integer' And 'IQueryable(Of Integer)'

Aug 3, 2010

This is giving me a headache. I have this link query here that grabs an ID

Dim mclassID = From x In db.SchoolClasses Where x.VisitDateID = _visitdateID Select x.ClassID And then later on I have this linq query

ViewData("Staff") = From t In db.Staffs Where t.ClassID = mclassID Select t

I've tried quite a few things but to no avail. I've attempted casting, converting, Is operand, etc.

View 5 Replies

.net - Casting Generic Types?

Apr 20, 2010

Public Function CastToT(Of T)(ByVal GenericType(Of Object) data) As GenericType(Of T)Return DirectCast(data, GenericType(Of T))End Function

The above clearly does not work. Is there any way to perform this cast if I know that all objects inside data are in fact of Type T?

View 2 Replies

Define A Generic That Can Take Several Possible Value Types?

May 4, 2011

Is there any way to define a Generic in VB.NET which will accept only Singles or Doubles? I tried the following things, based on what I've found online, but none compile:

Dim target As Nullable(Of {Single, Double})
Dim target As Nullable(Of T As {Single, Double})
Dim target As Nullable(Of T {Single, Double})
Dim target As Nullable(Of Single, Double)
Dim target As Nullable(Of T As Single, Double)

I want to specify that target can either be a Single? or a Double? only.

View 1 Replies

GetType On Generic Types

Dec 2, 2010

I'm trying register presenters with Windsor using the convention based method but trying to do this in VB.NET, but the problem is it does not want to compile this statement:

Dim type = GetType(AbstractPresenter(Of))

I am getting : Too few type arguments to AbstractPresenter(Of TView, TPresenter)

Which I don't understand because this is a valid statement according to question. Also showing valid in other C# to VB.NET converters when converting typeof(AbstractPresenter<>).

View 1 Replies

Specify Generic Types On Delegates?

Nov 15, 2010

Maybe I am just not reading the MSDN documentation correctly, but given a function that takes in one string parameter and returns type T, how can this be specifed as a shared function using Func()?

MSDN says Func(Of In T1, Out TResult), but all of their examples use the same data type, i.e., Func(Of String, String). I want to do Func(Of In String, Out T), where T is arbitrary (but I can constrain it if necessary by a base class). I want it shared/static at the class level, yet the encapsulating class will itself not be a generic class. It seems in this specific scenario, it's impossible to do what I want because the compiler would have no way of knowing what Type T is at runtime.

So is it possible to do generics on delegates or anonymous lambda expressions in VB.net (not C#)?

View 1 Replies

.net - Retrieving Generic Argument Types

Oct 23, 2009

I am using VB.Net. I have an object class called clsA(of T as clsB). Depending on what T is, I want to do different things. Let's say that clsC and clsD both inherit clsB and therefore can be used for T.

If I have an instance of clsA(of clsC), how can I get the inside type (i.e. clsC) using reflection?

View 4 Replies

Arrays And Generic Types - Conversion

May 10, 2011

Well this is maybe a little different than other array based conversion questions, but I'm dealing with forced typing concerns due to interface implementation and thus would like a few opinions on the process. I've basically redesigned the Collection suite for .Net because I don't like the extreme simplicity of the default Collections and dictionaries, as well, when I want the more complex concepts I don't want to have rewrite the same collection over an over again.

[Code]...

View 2 Replies

C# - What Are All Of The Generic Collections/types In The 4.0 .NET Framework

Nov 29, 2010

Is there an exhaustive list of all of the "base" (not used in an object-oriented sense but more in a common sense) generic types in the 4.0 .NET Framework? I have found this list that I often send newer/mid-level devs to so they can understand how non-generic types map to generic types, but this is by no means exhaustive. I'm looking for something that also includes things such as KeyValuePair<>, Tuple<>, and other basic generics that may not be very-well known. Interfaces such as IObservable<> would be nice but not necessarily required.

View 6 Replies

C# ::Generic Overkill To Define Multiple T Types?

May 25, 2011

NOTE: I am mixing VB and C# - snippets come from different librariesI have a VB class definition as follows:

Public Class Session(Of TConnectionBuilder As {DbConnectionStringBuilder, New}, _
TConnection As {DbConnection, New}, _
TDataReader As {DbDataReader})

[code].....

View 3 Replies

Use Generic Constraints Based On Attributes Rather Than Types?

Aug 28, 2009

I'm trying to write a class that will be in charge of persisting application options. Since the options need to be persisted the values that I'm sent must be serialisable.

Initially I thought I've be able to write a method with a signature like this[code]...

View 4 Replies

Generic BitConverter.GetBytes For Primitive Data Types?

Jan 19, 2012

addressing the need for getting the bytes of an object. But I am wondering if there is an approach to calling BitConverter.GetBytes on a generic type where I know the type is a primitive (Int32, UInt16, etc).

Public Sub Foobar(Of T as Structure)()
Dim x as T 'Assume T is declared as Int32
Dim y() as Byte
y = System.BitConverter.GetBytes(x)
End Sub

The above will throw your usual error:

Overload resolution failed because no accessible 'GetBytes' can be called with these arguments:
'Public Shared Function GetBytes(value As Double) As Byte()': Value of type 'T' cannot be converted to 'Double'.
'Public Shared Function GetBytes(value As Single) As Byte()': Value of type 'T' cannot be converted to 'Single'.

[code]....

One solution I think would work is a large Select Case calling GetType(), but that is horrendously slow (because of boxing) and looks ugly. I would think that since I call my higher level class with a primitive data type for T, that the compiler would be smart enough to figure it out, but I assume I am not providing enough information for it to derive what T's underlying value is at compile time for the invoked instances.

View 6 Replies

How To Match Generic Types, Without The Type Parameter In A IF-ELSE Statement

Jan 23, 2009

I have bee trying to figure out, how to match generic types, without the type parameter in a IF-ELSE statement - heres what i mean:

<P>IF TypeOf(Obj) Is GenericTypeExample(Of ???Any???) Then</P>
<P>End if</P>

View 7 Replies

.net - Double Or Integer : What To Use With BIG Or SMALL Data Types

Jul 26, 2011

I have a value of: "2.54334881002458E-37" and i keep getting "overflow" exception when i'm using a double.what should i use to make this work?

code snippet:

Dim curries, act, cat As Double
For Each dataRow As DataRow In dt.Rows
curries = dataRow("Activity")

getting the error when i try to assign Activity to curries.but "activity" is a string in the database....

View 4 Replies

Implementing Two's Complement On Unsigned Integer Types?

Jun 24, 2011

How can I implement a two's complement in VB.NET using unsigned integer types such as Byte, UShort, UInteger and ULong? Can I cast a UInteger to an Integer?

View 1 Replies

Override Basic Types Like Integer, String?

Nov 12, 2010

I am extending some of the basic data types in vb.net for a large application. This includes integer, string, short, etc. As it stands, my new data type objects have names like MYInteger and MYString. Since these are the only types I'm using for my application and they are mostly compatible with the default types, is there a way I can override the defaults with mine so when you Dim iThing as Integer you're actually using my slightly customized integer type?

View 3 Replies

VS 2010 Operator '<<' Is Not Defined For Types 'Char' And 'Integer'

Mar 14, 2012

I am trying to convert a piece of C# code, and I ran it through a converter, and had to make some changes.I am now running into this error:

Operator '<<' is not defined for types 'Char' and 'Integer'

The code is:

a += CType(URL(k + 0) + (URL(k + 1) << 8) + (URL(k + 2) << 16) + (URL(k + 3) << 24), UInt32)
b += CType(URL(k + 4) + (URL(k + 5) << 8) + (URL(k + 6) << 16) + (URL(k + 7) << 24), UInt32)
c += CType(URL(k + 8) + (URL(k + 9) << 8) + (URL(k + 10) << 16) + (URL(k + 11) << 24), UInt32)

The error occurs on each of the (URL(...) << X)

View 11 Replies

Raise Events - Generic Function For Sorting Integer Arrays

Aug 26, 2010

I am very confuse over a Raise Events example and using delegate as a function pointer: I am clear with this function pointer example program below which creates a generic sort function for sorting integer arrays without changing the main sort function.
' Returns True if need to swap
Delegate Function CompareFunc(ByVal x As Integer, _
ByVal y As Integer) _
As Boolean

Here are two alternative target methods for the delegate - one for an ascending sort and one for a descending sort:
Function SortAscending(ByVal x As Integer, ByVal y As Integer) As Boolean
If y < x Then
SortAscending = True
End If
End Function
Function SortDescending(ByVal x As Integer, _
ByVal y As Integer) As Boolean
[Code] .....

View 1 Replies

Operator '+' Is Not Defined For Types - Each Indexed byte Position Gets Assigned The Converted Integer Value In The For Loop?

Sep 22, 2010

Operator '+' is not defined for types '1-dimensional array of Byte' and 'Byte'.How would I change the assignment statement in the code below so that each indexed byte position gets assigned the converted integer value in the for loop?

Dim byteArray As Byte()
Property Data_Out() As Integer()
Get[code].....

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

Constrain A Generic Method / Extension Method To Numeric TYPEs Only?

Sep 16, 2010

Write an overload for every numeric type or if possible constrain a generic extension method to just numeric types.

View 2 Replies

Vb 2008 Number Guess Game - Operator '<' Is Not Defined For Types 'System.Windows.Forms.Button' And 'Integer'

Oct 29, 2011

I am getting Errors: Operator '<' is not defined for types 'System.Windows.Forms.Button' and 'Integer'.

Operator '>' is not defined for types 'System.Windows.Forms.Button' and 'Integer'.
Value of type 'Integer' cannot be converted to 'System.Windows.Forms.Button'.

And my program does not work!

Below is my code..............

Private Sub ProgressBar1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProgressBar1.Click
ProgressBar1.Value = TextBox1.Text
End Sub

[CODE]...

View 11 Replies

Error "Operator * Is Not Defined For Types Date And Integer"?

Jan 28, 2009

i used the following code in vb 6.0 and it produced the required value but when i used the same code in vb.net it shows the error "Operator * is not defined for types Date and Integer"

Dim Act_Rec As Integer
Dim VTar_Hr As String
Dim VYptime As String[code]....

View 4 Replies

Is There A Concept Of "Generic Structure" Similar To Generic Collections, Generic Classes

Dec 16, 2009

We have migrated our Vb6 application to VB.NET using a third party tool. Now we are in a process of Refactoring and introducing object oriented concepts in the application.

In VB6, we were using structures in many places. As a part of introducing object oriented programming,

1. is it a good idea of changing all Structures to Classes? or Is there a concept of "Generic Structure" similar to Generic collections, Generic classes?

2. Can some one guide me any source containing guide lines or best practices for applications that are migrated to VB.NET from VB6 and implementing object oriented programmaing.

View 3 Replies







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