VS 2005 Get Item Type For A Given Generic.IList

Mar 25, 2009

I have a piece of code that does an action to any collection that implements generic.IList<T>However to do this it needs to get the type <T> of the generic list.e.g. If I pass in a variable of type Generic.List(Form) then in my code I need to get the fact that the list is made of Forms. To add complexity the list may be empty (So you can't just look at Item(0).GetType)

View 6 Replies


ADVERTISEMENT

Implementing IList (Of T) In Generic Class

Jun 26, 2009

I am creating a generic class that is Implementing IList(Of T) which requires that I implement the GetEnumorator of both IEnumerable(Of T) and IEnumerable. When looking at the members of IList(Of T) I see only one GetEnumorator function, yet it implements IEnumerable(Of T) and IEnumerable. How can I use one function to implement both interfaces when the function definitions only vary by return type?

View 8 Replies

Casting From Generic IList To Object During DataBind?

Feb 18, 2010

Okay, so I have a Generic IList of custom objects that I want to use as a datasource for a repeater.

Dim productRows As IList(Of MyCustomObject)()
'fill list with data
rptResults.DataSource = productRows

[code]......

View 1 Replies

VS 2005 - Generic Lists Of Unknown Type

Sep 14, 2009

I have recently moved from VB 2003 to VB 2005. While I am finding a lot of the new features quite useful, I am having trouble with List (of T). I want to be able to do something like this

Function GetList(MyObjectType as Type) as datatable
Dim list as new List(of MyObjectType)
End Function
Function GetList(MyObjectType as String) as datatable
Dim list as new List(of GetType(MyObjectType))
End Function

VB 2005 does not allow me to do this at all. Must the "type of" be srongly declared at all times? Can't this be done dynamically?

View 2 Replies

Possible To Add Multiple Strings Per Item In A Ilist?

Mar 17, 2012

is it possible to add multiple strings per item in a Ilist?If so can someone point me in the right direction? I've looked on the msdn and can only find examples on how to add one string per item

View 16 Replies

Use IList(Of IList(Of String) - Pass It As A Parameter To Another Method?

Aug 2, 2011

I'm trying to use a variable that is simply a list of a list of strings.I've declared it as follows:

Dim iRows As New List(Of List(Of String))

Then I'm trying to pass it as a parameter to another method and I've defined the method as follows:

Public Sub Import(ByVal Rows As IList(Of IList(Of String)))
For Each Row As IList(Of String) In Rows
ImportRow(Row)[code]....

Unfortunately, when I try to run that code I get the following error where it tries to pass my variable to my method.

System.InvalidCastException was unhandled by user code
Message="Unable to cast object of type 'System.Collections.Generic.List1[System.Collections.Generic.List1[System.String]]' to type 'System.Collections.Generic.IList1[System.Collections.Generic.IList1[System.String]]'."

When I change the method definition to use the types rather than the interfaces as follows, it works.

Public Sub Import(ByVal Rows As List(Of List(Of String)))
For Each Row As IList(Of String) In Rows
ImportRow(Row)[code]....

So, is it not possible to use generics with interfaces in this way? It works fine as long as I'm not nesting them.

View 3 Replies

Get Type Of Derived Generic List Class From List Item Method?

Mar 23, 2011

Public Class notifierMain
Public Class Contacts
Inherits List(Of row)
Public Sub New()

[code]....

When I debug this winforms application I get curType = "notifier.notifierMain+Contacts+row" I want to the Validate function to know it is in MyContacts. How do I do this?

View 3 Replies

VS 2005 - Code - Error - Value Of Type 'System.Collections.Generic.KeyValuePair(Of String, Long)' Cannot Be Converted

Feb 12, 2011

This code:

vb Dim s As String = strContents '<a global variable) Dim currentPos As Int64 = 0 Dim endPos As Int64 = strContents.Length - 1 '// A dictionary, used to count the frequencies Dim characterCounter As New Dictionary(Of [String], Int64)() While currentPos <> endPos '//

[CODE]...

Gives this error: Value of type 'System.Collections.Generic.KeyValuePair(Of String, Long)' cannot be converted to 'System.Collections.DictionaryEntry'.

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

.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

Warning: Type Library Exporter Encountered A Generic Type Instance In A Signature

May 5, 2009

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

Unable To Cast Object Of Type .objectquery To Type Generic.list

Sep 12, 2011

I have a question about ASP.Net, visual basic I have 2 LINQ query's, the first one works, the second one doesnt, produces a

"Unable to cast object of type 'System.Data.Objects.ObjectQuery'1[SelmaV2.Products]' to type 'System.Collections.Generic.List'1[System.String]'.

[Code]....

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

Using An Interface Defined In A Generic Type From Within The Defining Type

Feb 14, 2012

In trying to add a bit of usage variety to a generic class I'm working on, I ran into this issue with trying to cast an object into an interface instance where the interface is defined inside the generic class.

[Code]...

View 1 Replies

VB Allows The Type Parameters To Be Used As The Base Class For The Generic Type?

Mar 30, 2010

1) VB Allows non-type template parameters2) VB supports explicit specialization 3) VB allows the type parameters to be used as the base class for the generic type4) VB allows a generic type parameter itself to to be a generic 5) VB enforces that all codes are valid for all types of parametrs

View 1 Replies

Unable To Cast Object Of Type 'System.Collections.Generic.List`1[System.Int32]' To Type 'crmWebService.ArrayOfInt'?

May 14, 2012

I'm getting an error after sending a list to a web service.This is the code that is calling the web service:

Dim sProgramInterest As New List(Of Integer)
crmService.InsertProspectGetId(sProgramInterest.ToList)
But I'm getting this error.

[code].....

View 3 Replies

Unable To Cast Object Of Type 'ImagesFlag' To Type 'System.Collections.Generic.IEnumerable`1[ImagesFlag]'

Jan 7, 2012

And here is the code that produces the error

CODE:

How can i cast this one?

View 1 Replies

Getting A Reference To A Generic List Item?

Jun 2, 2011

Does anybody know how to pass a reference to the current item in a generic list, when processing that list? In VB.NET you have to use a delegate wrapper to pass parameters in a delegate, which I have set up OK, but I don't know how to get a reference to the current item that would be the parameter used at in the example below. Using the Current property of the list enumerator doesn't seem to work, but how else could this be done?

[Code]...

View 1 Replies

How To Check If A Item Is Aready In A Generic List

Oct 13, 2010

I want to check if a refdes already been added to the generic list.And if so he must not be added..How do I do this I looked every where and can not fins a solution. [code]

View 5 Replies

System.Collections.Generic.Dictionary `Add` Vs Set `Item`

May 20, 2011

If i wish to put items into a System.Collections.Generic.Dictionary, I can either Add or set the Item.I know if you do Add it helps you check if the key already exists and if not it throws an exception.Now I'm curious, so if I'm adding a ton of items, should I prefer setting Item instead of Add since Add does unnecessary checks that may actually slow things down?

View 3 Replies

Generic Declaration Using Type?

Jul 31, 2009

I'm trying to declare a generic using a variable of type System.Type, with VB 2005, and haven't found the right approach. Consider the typical:

Dim m_GenObj As MyGenericObject(of T)
Instead of using "T" I want to use a variable of System.Type, for something like:
Dim m_MyType as Type
Dim m_GenObj as MyGenericObject(of m_MyType)

But of course that doesn't work.

View 2 Replies

Returning Generic Type In .NET?

Mar 7, 2011

Someone (w69rdy) in Stack Overflow helped me out with a great example to handle DB output, that could potentially be NULL, passed into a function. The problem is I can understand the method as written in C# but I am having a problem understanding how to rewrite the method in VB.NET. The method uses generics and I am lost. Here is the method written in C# ..

public T ParseValue<T>(System.Data.SqlClient.SqlDataReader reader, string column)
{
T result = default(T);
if (!reader.IsDBNull(reader.GetOrdinal(column)))

[code].....

How is this written in VB.NET? How does the method signature change when returning a generic type?

View 2 Replies

Add Item To Generic List Change Value Of All Previous Added Items To Last?

Oct 27, 2010

I'm writing code some that is supposed to be creating a list of objects pulled from a data base. The list returned has every item containing the value of the last one added. When I debug the code is VS2008 I can see each time that the list.add is performed every other item in the list being changed to the one just added. Here is the code I running. The line containing ListAKAs.Add(itemAKA) is the one where the entire list of objects is changed to be identical to the one just added.[code]....

View 1 Replies

Inherit From A Generic Class Without Specifying A Type?

Apr 29, 2009

I have the following sample code in a VB.NET console application. It compiles and works, but feels like a hack. Is there a way to define EmptyChild so that it inherits from Intermediate(Of T As Class) without using the dummy EmptyClass?

Module Module1
Sub Main()
Dim Child1 = New RealChild()[code].....

The other way to do this would be to move the generic code out of the Base class and then create 2 Intermediate classes like this [code]...

Then RealChild would inherit from the generic Intermediate and EmptyChild would inherit from the non-generic Intermediate. My problem with that solution is that the Base class is in a separate assembly and I need to keep the code that handles the generic type in that assembly. And there is functionality in the Intermediate class that does not belong in the assembly with the Base class.

View 1 Replies

.net - Cast To Generic Type At Runtime?

Sep 9, 2010

I'm in the process of creating a function which reflects over the properties in a class and adds them to this data structure I have. Some of the properties are generic types.So say we have DataType(Of T) that has a .Value property of type T:

Dim properties = GetType(MyType).GetFields(Reflection.BindingFlags.Public Or _
Reflection.BindingFlags.Instance)
For Each prop As fieldinfo In properties
Collection.Add(prop.Name,prop.GetValue(poco))
Next

In the collection.Add for primitive types (Integer, String, etc....) I just want to add the type... but in the case of generic I want to add the DataType(Of T).Value. I hope there is some work-around but I don't think there is a way because the type of T can not be determined at compile time right? Ideally DirectCast(prop.getvalue(poco), DataType(Of T)).Value would be possible. This is when you hoped even more dynamics appear than whats in .NET 4.0.

View 2 Replies

.net - Generic Classes (T) - Specifying From A Range Of Type?

Jul 28, 2010

This is the code I'm trying to develop:

Public Structure Statistic(Of t)
Dim maxStat As t
Dim curStat As t

[Code].....

View 2 Replies

C# - Reference Type Key In A Generic Dictionary In .Net?

Mar 16, 2010

I have a mutable class that I'm using as a key to a generic dictionary. Two keys should be equal only if their references are equal. From what I've read, in this case, I don't need to override Equals, GetHashCode , or implement IEqualityComparer.

View 5 Replies

Constrain Generic Type Using Where T Struct?

Mar 24, 2010

I am looking into generics in .NET, and was wondering about the where T : struct constraint. I understand that this allows you to restrict the type used to be a value type. My question is, without any type constraint, you can do a limited number of operations on T. Do you gain the ability to use any additional operations when you specify where T : struct, or is the only value in restricting the types you can pass in?

I guess the question I am actually asking is that if I were to write, (in a discussion about how to use generics), "Now that you have constrained the type argument to value types, you can also do _________ on/with objects of that type", Is there anything to put in that blank? I can think of things for the other constraints, but not this one.

View 3 Replies







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