.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


ADVERTISEMENT

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

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

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

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

.net - Shared Variables Among Subclasses?

Jan 18, 2011

I have a problem with inherited classes. Have a look at the following VB.NET 2.0 / VS 2005 code:

MustInherit Class templateclass
Public Shared x As String
End Class
Class child1

[code]....

The templateclass has a shared variable x which is of course inherited by the child classes. But I wonder that all child classes share only one x! Until now I thought that shared variables are only shared among the instances of a class, not among all childs. This is very annoying because I have a base class which I need in two slightly different versions and they should not "share the shared" variables. And because the classes have a lot of shared variables, shadowing each in the childs would be very..

View 1 Replies

Interacting Classes With SubClasses?

Mar 17, 2009

I have a project in Visual Basic 2008 where there is a Main Class and couple of classes inside the Main Class. I want to be able to share variables/objects within the Main Class and SubClasses while those vars/object are instanced.

Ex:

Code:
Public Class clsMain
Public t as New clsSub1

[Code].....

So, can i make var1 be in separate instances per clsMain in the memory while being able to access the var1 in clsSub1?

View 2 Replies

XML Serialization - Objects With Different Subclasses

Oct 11, 2011

I'm want to serialize a list(of Animal) of objects with different subclasses of type "their own type" but getting nowhere as exceptions doesn't give out any useful information. I've set up the serializer method which which serializes individual sublass objects if I declare them with their own type but not if I declared them as a derived (Animal).

Code is below:
Public Sub XMLPersistAnimalList(ByVal filePath As String)
Dim serializer As New XMLSerialization(m_animalList, filePath)
serializer.Serialize(Of List(Of Animal))(filePath, m_animalList) '------doesn't work!
[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

Control The Element Names Of Serialized Subclasses?

Jul 5, 2011

Let's say I have the following class structure (simplified from my real-world problem):

Public Class PC_People_Container
Private _people_list As New List(Of PL_Person)
Public Sub New()[code].....

If I were to serialize this, I'd get the default assigned node names in my XML. That means my root is named PC_People_Container and each person in the list is marked up as PL_Person. I know I can change the root node using <XmlRoot(ElementName :="PeopleContainer")>. The trouble is doing that for the subclasses. I can't use the <XmlRoot> tag on PL_Person class because there can't be two root elements, and IntelliSense throws a fit when I try to use the <XmlElement> tag on a class like I would on a property. Is it even possible to control what those subclasses are named when they're serialized as child nodes?

View 1 Replies

C# - How To Group Enums Structures And Subclasses In Classes

Aug 21, 2010

I've heard bad things about overusing regions but when adding enums to my classes I put them into a #region "Enums" at the end of the class, and do the same with structures and even subclasses.

Is there a better/standard way to go about grouping such elements on classes?

(Note: this is tagged C#/VB but maybe the same situation exists for other languages, like Java)

View 1 Replies

VS 2008 Casting Subclasses, Base Classes?

Jun 26, 2010

I am a VB6 programmer learning VB.Net and am having trouble with the following concept.

[Code]...

View 4 Replies

Constraints On Generics With .Net?

Dec 14, 2011

I have the a function that is declared like so: Public Sub Modify(Of SIMType As {New, DAOBase})(ByVal obj As SIMType)

I also have a class called Products which is declared like so:

Public Class Products Inherits DAOBase

So as you can see, if I were to call this function like so:

Modify(Of Products)(new Products())

This would not be an issue. The issue actually arises when I try to cast the object being past in to its real type. For example: both do not work. I get a Value of type SIMTYPE cannot be converted to IMS.Products error. Im assuming this is because I am using generics. Is there a way to adjust my function to allow for a casting operation like I am trying to do? In the end, what I need is a reference of the actual type (Products in this case) to the object.

View 2 Replies

What Constraints Are In Application

Nov 1, 2009

Can some one explain what constraints are in this application?

View 11 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 - Extension Method And Type Constraints?

Sep 13, 2010

I am starting to play with extension methods and i came across with this problem:In the next scenario i get a: "extension method has a type constraint that can never be satisfied"

Public Interface IKeyedObject(Of TKey As IEquatable(Of TKey))
ReadOnly Property InstanceKey() As TKey
End Interface

[Code]...

View 1 Replies

.net - Getting The Foreign Key Constraints Of A Table Programmatically?

May 17, 2012

I'm trying to use a DataTable to get the schema of a SQL Server DB.But, when try to detect the ForeignKeys, the constraints collection only brings the UNIQUE constraints.

Private Sub ShowConstraints(ByVal tableName As String)
Dim table As DataTable = New DataTable(tableName)
Using connection As SqlConnection = New SqlConnection(GetConnectionString)

[code]....

How can I get the ForeignKey Constraints?

View 2 Replies

Add Constraints To Image Resizing Code?

Feb 15, 2012

How do I add constraints to image resizing code? I want the image to be no larger then 165x146. The below code does not hold the constraint when image is 525x610.[code]

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

.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

Construct A Generic Interface Class With Generic Functions

Nov 25, 2011

I am trying to construct a generic interface class with generic functions. My goal was to use this to implement multiple worker classes for database interaction that have the same basic functionality. Each class will deal with different object for example, category, product or supplier but unless the the functions in the interface are generic that this won't work.This is the interface code that I have but I don't know if I have done it correctly. [code]

View 2 Replies

Asp.net - Enforcing Database Constraints In Application Code

Sep 16, 2011

I'm working on my first real asp.net web application and I'm kind of stumped on the best place and method to trap and handle database constraint violations. Suppose I have a unique constraint on a column and a user inputs something that would violate that unique constraint. Do I catch it in the business layer by making a call to the database to check of the value exists for that column or do I let it go all the way to the database and let it throw an exception and handle that in my application?

I don't like the latter answer- because it makes sense to handle as much as you can in the business layer before it reaches the database. But it's also somewhat problematic as my application is driven by procedures that implement basic CRUD operations on all tables. So in order for me to enforce uniqueness in the business layer, I need to create a procedure for every possible constraint so I can do a look up before an update or insert. That just seems tedious... and I'm duplicating business rules as they exist in both the database and the business layer, so if something changes, not only do I have to change the database, but I have to change the procedure and the application code.

So is there a right way or a good way to enforce these database constraints in application code or should I be looking at a way to catch exceptions thrown by the database and present them in a user-friendly way?

View 2 Replies

DataSet Bound To DGV - Failed To Enable Constraints

Jul 31, 2010

I keep getting this error message (see image). What's weird is it's coming from a "typed" dataset that is bound to a DataGridView and even though I get this message, the DGV still loads with all the data. I've bound this dataset to the DGV thru the designer. Here is the line of code that is erroring out:
Me.adpItemMaster.Fill(Me.JewelryDBDataSet18.tblItemMaster)
I checked the table and the primaryKey for the tblItemMaster is unique for each row.

View 2 Replies

Failed To Enable Constraints. DataAdapter.fill()?

Mar 29, 2009

When I use the dataAdapter.fill(ds,tableName), it reports an error: "Failed to enable constraints."Yes, I changed the selectcommand.I know if I modified the selectcommand, and if it will take diffrent columns result, this error will be reported.

[Code]...

View 4 Replies

Error Message "Failed To Enable Constraints

Sep 11, 2009

I am getting headache analyzing what's causing this error message "Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints."I have 7 different sections, each sections has the same subject component, one of which is CLUB. The error will only display for one particular section but for the rest of the sections, the program work fine. All sections uses the same code that's why I am very puzzled with the error. if the code works for the rest of the section one it fails for the one particular section?I have check my table (sql2005 express) and all the records are intact.The error is pointing on da.Fill(Me.Grading_Rpt_DataSet.tblEnrol)of the code below.

[Code]...

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

VS 2008 Accessing Something With A Generic Generic?

Jun 19, 2012

If i have an object that implements an interface (and don't know the exact object) i can call a sub (DoSomething) by going:

vb
Dim iObj = trycast(something, iInterface)
if iObj isnot nothing then
iObj.DoSomething()
end if

How can I do this for a Generic? such as...

vb
Public Class TestClass(of T)
Public sub DoSomething()
....
end class

View 2 Replies

Error: Cannot Clear Table Table1 Because ForeignKeyConstraint FK_Table1Table2 Enforces Constraints And There Are Child Rows In Table2

Feb 15, 2012

I have datagridview1 binded to data Table1 (with binding source) . This data table1 is in relation with his child data table2.When I insert new record to table1 I want to refresh datagridview1.

If I use table adapter fill method I get error.TableAdapter1.Fill(Me.DataSet1.Table1) ' error error: Cannot clear table Table1 because ForeignKeyConstraint FK_Table1Table2 enforces constraints and there are child rows in Table2.

View 1 Replies

Multithreading - Modify A Generic Invoke Method To A Generic BeginInvoke Method?

Oct 7, 2010

I have been using a pretty slick generic invoke method for UI updating from background threads. I forget where I copied it from (converted it to VB.NET from C#), but here it is: Public Sub InvokeControl(Of T As Control)(ByVal Control As t, ByVal Action As Action(Of t))

[Code]....

View 1 Replies







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