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


ADVERTISEMENT

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

C# - Define A Variable Of Different Types?

Feb 8, 2011

i wonder if it's possible to define a variable/property of more than one type. Let's say i want a property TextWebControl that is a WebControl and also implements Web.UI.ITextControl(f.e. like a TextBox or Label). But i don't want to enforce that it is a TextBox or Label, but only one that inherits from WebControl and also implements ITextControl so that it also would work with controls added in future releases of .Net-Framework.

.Net-Framework 4.0

Edit: I have retagged the question and added VB.Net because it's my default language. Normally it's no problem for me to understand C# also, but i must admit that it's difficult to translate generic stuff to VB.Net without experiences and it's also better documentated in C# than in VB. So i would appreciate(and aceept) a working example of a VB.net generic type of ITextControl/WebControl.From Marc's answer i understand that i need a generic class. But how do i instantiate it in SomeClass? This won't compile:

Class SomeClass
Public Property info As InfoControl(Of WebControl, ITextControl)
End Class
Public Class InfoControl(Of T As {WebControl, ITextControl})
End Class

View 3 Replies

Delphi To VB: Define Data Types?

Aug 11, 2011

in Delphi I have:

type osVer = (
osVerUnknown,
osVerWin95,

[code]....

I need to translate to VB...

View 3 Replies

Define The Meaning Of '+' Operator In Generic Methods?

May 20, 2010

I have defined a Sub in vb.net, with 'Of' type parameter like this:

[Code]...

I want to just add number1 and number2, But as you can see error has occured where i want to sum number1 and number2 saying that '+' is not defined for type 'VarType'. how to add these variables in Such Methods.

View 7 Replies

Define '+' Operator's Meaning For Of Clause Types?

Apr 21, 2010

i have a simple code just have a look End Class

[Code]...

When i add num1 and num2 as above, compiler gave error that -- Operator + is not defined for types "type" and "type".How to define the meaning of '+' or any other operator in such situations?

View 11 Replies

Define A 3 Dimensions Matrix With Different Data Types?

Dec 8, 2011

I define 3 arrays: ID(n), Hour(n), Showed(n). If Hour(i) = actual time, I get the data pointed by ID(i), if not Showed(i).

BUT... I can add new hours or modify some existent, but I need to have the Hour array sorted. If I sort this array, the ID and Showed will point to different data.

So I though some different solutions:

1 - To make a string array with all inside (a 7 characters ID, a 5 characters Hour and 1 char for showed (T or F)). It could work, but, how can I sort the middle part of the array (Hour)? So, this don't work.

2 - A 3 dimensions "multitype" matrix (1 dimension of type Integer, the second of type Date and the third of type Boolean). I have no idea about how this can be done.

3 - A 3 dimensions string matrix, some like the "all inside" array: ID converted to a fixed size string, hour converted to a 5 chars string (hh:mm) and Showed converted to T or F. And sort the second dimension.

4 - Sell my copy of Visual Studio and drive a taxi cab

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

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

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

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

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

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

Get A Multiple Number Of File Types?

Feb 21, 2009

I am not sure I am doing this correctly,I want to get a multiple number of file types (gif, jpg, png, etc.) and store them in one variable so I can access all the files in that var. I read that GetFiles doesn't allow for multiple patters, so how do I do this so I can have more than one file type in a variable?

Dim aryFiGif As FileInfo() = di.GetFiles("*.gif")
Dim aryFiJpg As FileInfo() = di.GetFiles("*.jpg")
Dim aryFiPng As FileInfo() = di.GetFiles("*.png")

View 2 Replies

Return Multiple Types In A .Net Function?

Feb 17, 2012

So you know how PHP internal functions usually return a boolean FALSE when a function fails, or some other data type when the function succeeds? Is this possible in VB .Net?For instance, lets say this simple code here Public Function TrySomething(ByVal Param As String) \what would go here??

[Code]...

You see I want to return a BOOLEAN false when a function fails, and a string when the function works. I've looked everywhere, and when I search for "return multiple types" all i find is Tuple.

View 4 Replies

Sorting Generic List On Multiple Keys?

Nov 2, 2009

I need to sort a list (of person) to person.Age within person.Department. f I user personlist.sort with a comparer for Age and then sort again with a comparer for Department, the sequence of age is destroyed.

View 1 Replies

Collated Printing Of Multiple Documents Of Different Types

Aug 4, 2010

I have a series of documents (Crystal reports, Excel workbooks, Word documents, PDF documents) that I need to print out in a collated order, but it seems that each application runs on its own thread and the documents don't come out in the order I intend.Is there a way of using VB.NET code to (a) control the order in which the documents are published; and (b) compelling the system to wait until one document is complete before starting the printing of the next one in the sequence?LDC

View 1 Replies

How To Store Multiple Objects (of Different Class Types)

Mar 22, 2012

My brain is fried at the moment, so here's the scenario.I have a form that adds a member to my VB.NET application.When I press submit I add their details: name and number as well as what products they want (internet and phone). Internet and Phone are their own class as is member.

I want to add that they HAVE whichever they have chosen and with the member store it.

[Code]...

As what happens is once the member is created (stored) I will go to input how much they have used, and then store that. But to do so I will need access to the internet and phone classes, which are associated with said member.

View 2 Replies

XML Serialization Arraylist With Multiple Object Types?

Feb 10, 2010

My quest is in regards to xml serialization. I need to get rid of the root node of a collection. I don't have access to my source now so I'll just try to generalize

public class SomeClass
'...some other properties...
<XMLArray(isnullable:=true), _
<XMLArrayitem(datatype:=(getType(object1)), _

[code]....

View 6 Replies

C# - Generic Extraction Of Multiple Decimals Using Regular Expressions

Mar 2, 2012

how do you extract multiple decimals with different number of decimal places from a string? I'm looking to find a generic way to extract 3 numbers out the following strings.

[Code]...

View 1 Replies

[2008] Working With A Generic List (multiple Manipulators)

Mar 14, 2009

so I have a generic list in a console application. Dim Channels as New List(Of Chatroom) Chatroom is a structure. But I have multiple classes, threads, and timers looping through this list, sometimes at the same time, but it always seems to raise errors because the number of items in the list gets changed while one of the other classes/threads/timers are looping through also. Is there any way to allow only one to manipulate it at a time?

View 8 Replies

Converting Multiple Numerical Data Types In 2010?

May 7, 2011

I'm having a bit of an issue with this program I have been working on for my class. It's a future values calculator, that takes a single data type, decimal data type and a integer data type does the formula and then spits back out the Future value. What i'm having difficulty with is converting the string over.

Public Class Form1
'Define the Module level variables
Dim FutureValueInteger As Integer

[Code]....

View 1 Replies







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