C# - Multiple Generics Ambiguity?

Dec 27, 2011

The codes below are exactly the same, except that one is C# and the other one is VB.Net.C# compiles just fine, but VB.Net throws the warning:

Interface 'System.IObserver(Of Foo)' is ambiguous with another
implemented interface 'System.IObserver(Of Bar)' due to the 'In' and
'Out' parameters in 'Interface IObserver(Of In T)'

Why does VB.Net show the warning and not C#? And most important, how can I resolve this problem?

Obs: I'm using .Net Framework 4 with Visual Studio 2010 Ultimate.

VB.Net Code:

Module Module1
Sub Main()[code]......

View 2 Replies


ADVERTISEMENT

Multiple Type Cast With Or Without Generics

Mar 21, 2010

Ok, this looks junky to me and I tried a lot of approaches; Generics won't allow me to cast the types on the fly and use MemberwiseClone() because it is 'Protected'.

Does anyone know a "short form" of doing this? I tried CType by Object.GetType on the fly; but the IDE cannot resolve the MemberwiseClone().

Private lastObj as Object = Nothing
Public Function IsObjectedDifferent(CurrentObject as Object) as Boolean
If CurrentObject Is Nothing Then Return False

[Code]....

I am trying to make a generalized function to handle if the Object is Changed; or If the values are different.

This seems like way too much code for such a simple task to me, and I have a lot of classes to compare; is there away to get the class defination dynamically so that the CType function will handle it?

View 8 Replies

C# - Linq Ambiguity On Where And Select

Nov 15, 2011

Today I ran into an issue with LINQ to objects (not SQL) that popped up due to a typo. I had a .Select one place and a .Where in another place. I was expecting same result but they are showing different numbers. Assume somelist has 10 elements with all elements having qty = 0

[Code]...

View 4 Replies

Same Name Methods In Different Modules Cause Ambiguity

May 20, 2010

I have 2 modules. Each contains a Sub with the same name. See below:
Module moduleA
Public Sub f(ByVal arg1 As myType)
Console.WriteLine("module A")
End Sub
End Module

Module moduleB
Public Sub f(ByVal arg1 As myType, ByVal arg2 As Boolean)
Console.WriteLine("module B")
End Sub
End Module

But the compiler complains that there's ambiguity between moduleA and moduleB. I have totally different signatures. However, if I put the 2 methods into the same module, there's no ambiguity at all.

View 1 Replies

.NET: Ambiguous Class Name Error When No Ambiguity Exists?

Aug 16, 2010

A client of ours reported that when trying to use our .NET .DLL in VB.NET they receive the error:error BC31429: 'OurClass' is ambiguous because multiple kinds of members with this name exist in namespace 'our.company.nspace' I've also been able to reproduce the error with a dummy project containing the single line of

Dim x as our.company.nspace.OurClass Normally this is because there are several types with names differing only in case. But in this case there is no such ambiguity. OurClass is a unique name not only in the specific namespace but in the whole assembly and any other assemblies referenced by the project. Reflector also shows this. There are also no class members with the same name, also verified by Reflector.

Also a weird thing is that the error wasn't there immediately after I created the dummy project, and then it suddenly appeared and now it doens't go away anymore. In fact I didn't even change anything between the two recompiles from which the first one worked, and the second didn't.

View 2 Replies

Can The T Of Generics Be An Interface

Jul 17, 2010

In my program I have an interface iGraphable that contains two properties: Abscissa and Ordinate. Then I have an xxxx class (actually more than one) implementing iGraphable and a ListOfxxxx class implementing BindingListView(Of xxxx).To draw graphs I have a Graph class with a property called Data whose type is BindingListView(of iGraphable).Why have I a cast exception when I pass a BindingListView(Of xxxx) to the Data property.

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

.net - Polymorphism In .Net When Dealing With Generics?

Jan 6, 2012

I have a function called Modify. It is delcared like so:Public Function Modify(Of SIMType As {New, DAOBase})(ByVal obj As DAOBase) As Boolean

You can see that this function is generic. It takes as a paramer a object that is a DAOBase or subclasses of DAOBase.Inside the modify function there is a call like so:

DAOToGP(obj)This is where the polymorphism comes into play. There are four or so subclasses I have created of DAOBase. I have written a DAOToGP() for each of these types. So in the Modify() function, when it calls the DAOToGP(obj), polymorphism should kick in and it should call the correct implementation of DAOToGP() depending on the type that I pass into Modify().

However, I get the following error:Error 20 Overload resolution failed because no accessible 'DAOToGP' can be called without a narrowing conversion:'Public Shared Function DAOToGP(distributor As Distributors) As Microsoft.Dynamics.GP.Vendor': Argument matching parameter 'distributor' narrows from 'SierraLib.DAOBase' to 'IMS.Distributors'.'Public Shared Function DAOToGP(product As Products) As Microsoft.Dynamics.GP.SalesItem': Argument matching parameter 'product' narrows from 'SierraLib.DAOBase' to 'IMS.Products'. C:Usersdvargo.SIERRAWOWIRESDocumentsVisual Studio 2010ProjectsSIMDev_2SIMIMSDVSIMLibGPGPSIMRunnerRunnersRunnerBase.vb 66 39 IMS

I am kind of at a loss here. I am not sure why it cant figure out which function to call.

View 2 Replies

.net - Suppress COM Generics Warning?

Dec 8, 2010

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

.Net To Use Generics For Settings Persistance?

Nov 30, 2010

I'm trying to reduce code bloat, reduce errors and simplify codebehind by use of generics. In this case I'm applying generics to declaration of persistable properties. Persistance is implemented by My.Settings. Here's the code so far.

[Code]...

View 1 Replies

C# - Appropriate To Use Generics Versus Inheritance?

Apr 28, 2009

What are the situations and their associated benefits of using Generics over Inheritance and vice-versa, and how should they be best combined?I'm going to try to state the motivation for this question as best I can:I have a class as shown below:

[Code]...

Now suppose I have a repository that takes an InformationReturn argument, that has to strore different fields in a DB depending on the type of Info object T is. Is it better to create different repositories each for the type T is; one repository that uses reflection to determine the type; or is there a better way using inheritance capabilities over/with generics?

View 6 Replies

Call NET Generics In A Dynamic Way?

Oct 28, 2011

Names of entities have been altered to protect their identities...

I've created a class called AnimalSearch(Of AnimalType As Animal(Of Int32))

Public Class AnimalSearch(Of AnimalType As Animal(Of Int32))[code]...

View 2 Replies

Calling Constructor When Using Generics

Mar 9, 2011

I'm not sure if this is possible or not. I have a number of different classes that implement interface IBar, and have constructors that take a couple of values. Rather than create a bunch of almost identical method, is it possible to have a generic method that will create the appropriate constructor?

private function GetFoo(Of T)(byval p1, byval p2) as List(Of IBar)
dim list as new List(Of IBar)
dim foo as T
' a loop here for different values of x
foo = new T(x,p1)
list.Add(foo)
' end of loop
return list
end function

I get:
'New' cannot be used on a type parameter that does not have a 'New' constraint.

View 2 Replies

Converting VB6 Collections To .Net Generics?

Apr 21, 2012

I have a VB6 project with about 100 custom collection classes which I want to convert to VB.Net. A typical example would be something like.

Class CAccounts
Private m_Accounts As New Collection
Public Sub Add(newItem As CAccount)
m_Accounts.Add newItem, newItem.IdKey

[code].....

All of the collection classes in the project use this standard approach. However, not all the properties/methods of the collection classes are actually used. Most of the collection are used in "for each" loops. Keyed access using the string key is quite common. Keyed access by index is much less common.

Ideally I'd like to take a standard approach to converting these classes. I don't really want to have to review each collection and it's usage to consider whether I need a List, Dictionary, etc. Some of these collection contain 100,000 objects, and some will only contain 10. However, on the other hand I don't want to cause performance problems by using a more complex structure where a simpler option would do.

Sticking with the old style Collection. So, it would be relatively easy to convert to VB.Net But, I'd rather move to the more modern structures.Have CAccounts Inherit KeyedCollection(Of String, CAccount). Fortunately most of the classes held in the collections do have the key as part of the class (eg CAccount.IdKey above). This seems to work well. However, relatively few classes will access the colelction by numeric index. So, perhaps this is overkill if I only want keyed access by the string key?Have CAccounts Inherit Dictionary(Of String, CAccount) for the classes where I don't need access by numeric index. The problem I have with this is that all the existing "for each" loops are like "for each account in accounts". I don't want to have to change all these occurences to something like "for each account in accounts.Values". Although perhaps I can get round this by changing the default property?Have CAccounts Inherit MyCollection(Of String, CAccount), where MyCollection is my own bespoke collection. This seems a bit too much hard work.

View 2 Replies

Generics And Duck-Typing XML In .NET?

Feb 12, 2010

I'm working with some XML representations of data instances.I'm deserializing the objects using .NET serialization but something in my soul is disturbed by having to write classes to represent the XML.[code]

View 4 Replies

Generics Versus Extensions?

Nov 8, 2009

This is my problem:

<System.Runtime.CompilerServices.Extension()> _
Function CastAs(Of TSource As TTarget, TTarget)(ByVal array As TSource()) As TTarget()
Return Global.System.Array.ConvertAll(array, Function(src As TSource) DirectCast(src, TTarget))
End Function

View 10 Replies

Have Generics In A Class Property?

Jul 28, 2010

I have a Class that is defined as

Public MustInherit Class Entity(Of T As Entity(Of T))

And various classes derived from it. I would like to have another class accept all of the derived objects as a property, but I cannot seeem to find a workable syntax. If it were a parameter of a sub, I could say

Public Sub Foo(T As Entity(Of T))(TheEntity As T)

I can't seem to get that to work for a property:

Public Property E(Of Entity(Of T))() As Entity(Of T)

Gives me "Type parameters cannot be specified on this declaration"

Public Property E() As Entity2008(Of T)

Gives me "Type Parameter T does not inherit from or implement the constraint type ..."Is there an acceptable way to do this or am I stuck with using a Set Sub and a Get Function?

View 2 Replies

Numeric Type Only Generics?

Jan 20, 2011

Suppose I have an interface called IParseable(Of TParsed, TUnparsed) which requires two functions:[code]Is there a way that I can restrict TParsed and TUnparsed to be numeric types (for which operations like "*" and "+" are already defined)?The problem is that, when I try to implement my interface and define one of the functions, e.g.:[code]VS throws an error saying the "*" is not defined for TUnparsed. I understand that, since TUnparsed could be anything, but is there a way to restrict my generic such that, say, TUnparsed could only be Double, Integer, Long, etc? To require Control to be a TextBox (or maybe I don't understand that very well either). But, anyway, any idea or am I way off track? Just trying to get a hang of these interface thingies and generic types.

View 2 Replies

Use Generics In Linq Query?

Jul 28, 2009

I am trying to write a generic function that generates an Xelement based on a list of objects, and a property of the object[code]....

View 3 Replies

.net - Naming Standard For Generics - Of T Or Of TEntity?

Mar 16, 2011

I know this is a stupid question maybe, but what is the naming standard for generics?Of t or Of TEntity or Of..it doesn't really matter?I see IQueryable(Of T) but then DBSet(Of TEntity).

View 3 Replies

C# - .NET Generics - Compare Two Lists And Filter?

Jun 19, 2011

I have two generic lists of type T. Both lists contain same type, and I'd like to create a third list (or a filtered version of list 2) based on the items in list two that do not exist in List 1, based on the ID of each item.Each list holds a "Package" object, which has an ID property.Right now I mocked up the code using For Each loops, which I know is horrible (the Big O is constant time) so I'd like a more efficent method. this code is in VB per project requirments, but I prefer C# - so either code sample would work for me.

Private Sub RemoveStockPackagesFromSelection()
Dim p As Package
Dim packageList As List(Of Package) = New List(Of Package)

[code]....

View 5 Replies

C# - .Net Generics: Making Validation Generic?

Feb 27, 2012

I have the following C# code. Here the validations are kept outside the class to satisfy Open - Closed Principle. This is working fine. But the challenge is - the validations are not generic. It is specific to employee class (E.g DateOfBirthRuleForEmployee). How do I make the validations generic for all objects (DateOfBirthRuleForAnyObject).

Note: Make Generic <==> Make Type-Independent

Note: I have NameLengthRuleForEmployee validation also. New validation may come in future.

class Program
{
static void Main(string[] args)

[code]....

View 1 Replies

Difference Between Array List And Generics

Dec 20, 2010

I know boxing and unboxing part in arraylist but question is if I define only string in arraylist then there is no question of boxing or unboxing. What will be the difference.

View 1 Replies

First Parameter In The Definition Of .NET Generics Uses The 'Of' Keywo?

Dec 13, 2011

Why only the first parameter in the definition of VB.NET Generics uses the 'Of' keyword ?

For example I have here the the full code that LINQ uses for the Enumerable.Select() method :

<System.Runtime.CompilerServices.Extension()> _
Public Shared Function [Select](Of TSource, TResult)(
ByVal source As IEnumerable(Of TSource),
ByVal selector As Func(Of TSource, TResult)
) As IEnumerable(Of TResult)

[Code]...

View 9 Replies

Flexibility Vs Performance With CType And Generics?

Apr 11, 2012

Recently I found a way to make CType work with generics (FYI, VB 2010 Express with option strict on). Say I have two generic types, T1 and T2, with variables x as T1 and y as T2. y=CType(x,T2) diagnoses with "Value of type 'T1' cannot be converted to 'T2'". y=CType(CObj(x),T2) compiles cleanly, and it works fine provided that T1 can be converted to T2. Failing that, an exception will be raised at run-time. Obviously, a downside of this is the boxing performance hit. Also, it is a half a step backward along the road to strong typing (a type conversion problem is discovered at run-time vice compile-time).

These objections aside, consider the GSU class below. There are two ways to do a generic CType of a variable, and then there is a way to do a CType of an entire array. GSU is sufficient (see sub Test) to convert an item or an array of items between, for example, all the numeric types (byte, short, double, ...). Exception handling remains to be done.

Type conversion code tends to get lengthy, so I think this is a nice result. What do you think? I am guessing that CType has some pre-generic smarts in it, and all I did was find a way to exploit it in the context of generics and option strict. I am mostly interested in generic tricks for value types.

[Code].....

View 3 Replies

Free Items In Generics List?

Mar 21, 2011

I have been trying to use the Generics List as a means of storing a large amount of data that I am reading in from hardware. I defined a class called 'Datapoint', that has several public variables of type Double, and as I read in a number of points (this is of variable length, and could potentially be very large), for every point I create a new instance of 'Datapoint', update its fields and then add it to a List(of Datapoint) which I named 'Datalist'. All works fine, except when I come to reset the List, I do not know how to free up the allocated memory. I have attached some snippets of code,

[Code]...

View 3 Replies

Generics - How To Limit Size Of List

Mar 24, 2009

I am trying to limit the size of my generic list so that after it contains a certain amount of values it wont add any more. I am trying to do this using the Capacity property of the List object but this does not seem to work.
Dim slotDates As New List(Of Date)
slotDates.Capacity = 7
How would people advice limiting the size of a list? I am trying to avoid doing a statement to check the size after an object is added.

View 5 Replies

Generics - What Does .Net For Each Loop Look At To Infer The Type

Apr 6, 2012

In the following code,

For Each item in MyCollection
...
Next

What does the compiler use to determine the type of item?For example let say I have this class, which is inheriting a non generic collection,

Public Class BaseDataObjectGenericCollection(Of T)
Inherits BaseDataObjectCollection
End Class

A for each loop still infers the Item type as Object. How would I have to modify the above class to make the type inference work?

Edit: Per Beatles1692's answer, Implementing IEnumerator(Of T) kinda works. The base class already has a GetEnumerator function, inherited from CollectionBase, so I my implementation looked like this,

Public Function GetEnumerator1() As System.Collections.Generic.IEnumerator(Of T) Implements System.Collections.Generic.IEnumerable(Of T).GetEnumerator
Return MyBase.Cast(Of T)().GetEnumerator
End Function

However, the for loop still infers the type as object. But, if I change the interface implementation to this,

Public Shadows Function GetEnumerator() As System.Collections.Generic.IEnumerator(Of T) Implements System.Collections.Generic.IEnumerable(Of T).GetEnumerator
Return MyBase.Cast(Of T)().GetEnumerator
End Function

That works, the for loop gets the type inference correct. So I guess the question is now, does For Each just look for a function called GetEnumerator ?

View 3 Replies

Generics And Interfaces: Correct Deceleration?

Feb 8, 2012

I came to the point where I have to clone a complex data structure. During this process, cloning an object multiple times must be avoided, and circular references must be considered. I already have a working solution for this:

Friend Interface ICloneable(Of T As {Class, ICloneable(Of T)})
Function Clone(ByVal CloneMap As CloneMap(Of T)) As T
End Interface
Friend MustInherit Class CloneMapBase(Of T As Class)

[code]....

View 3 Replies

Generics Mixed With Overloaded Event?

May 24, 2011

I am looking for a little expert design insight.I am trying to save an overloaded property from a generic class.

Base Class

Public MustInherit Class BaseEvent
Public MustOverride ReadOnly Property IsWorkCalendar() As Boolean[code]....

View 2 Replies







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