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


ADVERTISEMENT

.NET Generic Constraints And Subclasses?

Sep 17, 2010

Consider the following extension method:

<Extension()> _
Public Function Satisfies(Of T)(ByVal subject As T, ByVal specification As ISpecification(Of T)) As Boolean
Return specification.IsSatisfiedBy(subject)
End Function

This works as expected if subject is the exact class being operated on by the specification. However, if the specification is examining the super-class of T, this extension will not work unless subject is explicitly cast to the super-class. Is there a way I can avoid this? So far, the best I've been able to come up with is:

[Code]...

Since I (apparently) can't get this to work exactly as I'd like in VB.NET due to limitations in the language itself, is my second attempt the safest/most efficient way to do this?

View 2 Replies

Generic Constraints That Allow Type To Be 2 Different Classes?

Mar 21, 2012

I have a generic function in VB.Net. I also have two classes called A and B. Is it possible to allow the generic constraints on my function to allow the Type to be either class A or B? Class A and B do not share any base classes except for object.

View 1 Replies

Specifying Inherits / Implements On Generic Class With Multi-Constraints

Jun 7, 2010

When I write the following statement in VB.Net (C# is my normal language), I get an "end of statement expected" referring to the "Implements" statement.
<Serializable()> _
<XmlSchemaProvider("EtgSchema")> _
Public Class SerializeableEntity(Of T As {Class, ISerializable, New}) _
Implements IXmlSerializable, ISerializable
...
End Class

The C# version that I'm trying to emulate is:
[Serializable]
[XmlSchemaProvider("MySchema")]
public class SerializableEntity<T> : IXmlSerializable, ISerializable where T : class, new()
{
....
}

View 1 Replies

Filter Data Based On Various Constraints

Feb 17, 2012

I am doing a projectin VB 2008..I ave list of data being displayed in listview and it gets stored in DB..The problem is,i am unable to filter particular data based on some constraints like date,client etc.. How can i filter data based on various constraints..As i am new to VB,

View 5 Replies

Visual Studio 2010 - Failed To Enable Constraints - One Or More Rows Contain Values Violating Non-null - Unique - Or Foreign-key Constraints. Error

Dec 9, 2011

There were three similar questions in StackOverFlow but none gave an answer.. If have found why this error in occurring but don't know the fix. I am using Strongly Typed Dataset for my project which is created as a dll for DAL. I have added the Sql Server Table into this dataset using the designer and has created a DataAdapter

[Code]...

View 1 Replies

Detect The Type Of A Generic At Compile Time Using Attributes

Nov 21, 2009

I may have a difficult question here. I am working on a Generic class that is meant to work specifically with Enum's. Right now, I get the type of the Generic on instantiation and make sure that it is an Enum and throw an exception if it is not. So:

[Code]...

View 2 Replies

Using Anonymous Types And Assigning Values To Controls Or Attributes

Jul 26, 2010

I'm trying to create a web site that users can log into and self serve their user information ie name, address etc. I've set up a SQL server DB with a couple of table holding the data and accessing those using Linq. I'm selecting specific data from my datacontext then trying to assign it to a label, ideally I want to assign the value to a class attribute so I can use the users details on multiple pages.

I've simplified the code (removing WHERE statement etc for the purposes of this question):

Using CurrentUserDataContext
As
New UserDataDataContext()

[Code].....

View 2 Replies

XML - Determine Basic Types Of Elements And Attributes From Schema

Feb 17, 2011

I have given the local name of an element or attribute and the schema for the document. What is the easiest way to determine the basic datatype of the element or attribute. By basic datatype I mean the xs:string, xs:date etc. (The built in data types for the xml schema.) One of the problems I face is that it is rare for the elements type to be one of the basic built in types. 99% of the time it is a complex type that 50% of the time refers to another complex type that refers to another complex type and so on.

A simple Example for this schema: I want to find the basic type for Employee/Person/Name/LastName (determine that LastName is xs:normalizedString).
In the schema Employee is defined as an xs:element and type="bns:EmployeeType"

EmplyeeType has a Person element defined but it is type "PersonType" and then Name in person is NameType which is a complex type that extends GeneralName type that is type BasicNameType and that type finally defines the LastName which is of type "LastNameType" and on and on. There also definitions etc. I am currently writing a parser using linq-to-xml to get at this but it isn't easy or pretty. I have searched for other solutions and haven't found any but I fully admit my XML/schema/XPath ignorance. Is there an easy way to get the basic type for elements?

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

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

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

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

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

Set Attributes Of Entry Point Thread Of A Form Based Application Before It Start?

Mar 13, 2010

I do not want to disable Application Framework feature and turn to main function technique

View 1 Replies

Generic Based On Enum

Sep 2, 2009

I have a generic class Class MyTestClass(Of T)where I know that T will only ever be an enum. Is there any way to declare this restriction to the compiler?And a second question: If the answer to my first question is 'no, there isn't', then what's the best way to cast T to a plain integer? Doing that is difficult when the compiler doesn't know that T is always an enum. [code]That certainly works, but it looks nasty as it apparently involves unnecessarily boxing and then unboxing the enum value. Is there a better way of doing it?

View 1 Replies

VS 2005 - Inheriting Class Based On Generic Interface

Nov 17, 2009

I want to define a generic interface which will be implemented by an abstract Generic Class. Basically this generic class is a collection class of any class. Interfaces are in a separate project saved as FileReconciliation. Here are the interface definitions

Interfaces
Imports System.Collections
Public Interface ICollectionCommon(Of T As Class)
Inherits IEnumerable
Function Exists(ByVal oKey As Object) As Boolean
[Code] .....

View 3 Replies

How To Declare Anonymous Types - Different Query Based On A Condition

Jun 29, 2009

How to declare Anonymous types. I want to do a different query based on a condition.

Something like this:

If true then

Dim Myquery = From data In MyXML.Root.<hist> _
Select New With { _
.SensorID = CType(data.<sensor>(0), Integer)}

else

[CODE]...

Then I want to reference MyQuery outside of the condition. But how do I declare MyQuery outside of the if statement?

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

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

.net - Sort Generic List Based Upon A Different List?

Oct 29, 2010

What would be the fastest way to sort a list that contains a list of objects, based upon another list? An example follows:Say I have multiple lists of employees. Each individual list has a common property value, say "Department". So I have a list of employees, in one list they all have the department string value of "Sales". In another list all the objects have a department value of "Finance". These lists of employees are then contained in a list which holds them all.

I have a second list, which should drives the sort order of the employee lists. The second list simply contains a list of strings like, "Finance", "Sales", "IT" and so on. I'd like my lists of customers to be sorted in the order of "Finance", "Sales", etc.I'm using VB.NET in .NET 2.0

View 1 Replies







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