List To IList Polymorphism Attempt Results In InvalidCastException?

Jan 29, 2010

In a program I am writing (in VB.NET, 2008), I have 2 classes, "Receipt" and "Group", each with similar properties, both implementing a custom interface entitled "IIDUser". In my program, I have a number of lists of "Receipt" and "Group", declared like:Friend receipts as List(Of Receipt)

I have written a few functions that apply equally to managing both lists of receipts and groups, declared like this one:Friend Function getNextAvailableUserLabel(ByRef theList As IList(Of IIDUser), ByVal startLabel As Short) As Short

Then called like this:getNextAvailableUserLabel(receipts, s)

I did all this in an attempt for interface-based polymorphism, as I thought writing 2 identical functions (except for that one would be for lists of receipts while the other for lists of groups) would be bad practice (and waste time). However, I get an "InvalidCastException" during runtime. It is explained, "Unable to cast object of type 'System.Collections.Generic.List`1[Receipt_Sorter_v2.Receipt]' to

[Code]...

View 4 Replies


ADVERTISEMENT

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

Unable To Cast From List(of Object) To IList(of Interface)?

Apr 19, 2011

I'm new to generics and am having some issues when using with interfaces.I'm developing with VS2008 - .NET 3.5 Ultimately, I'd like to create an interface that inherits the IList(Of T) interface and add a function definition to the derived interface that adds a new element of type T to the list and returns the index of the newly added item.

I have everything figured out but am stuck on a casting issue. If I execute the following line of code at runtime:CType(New clsObjectFactoryList(Of clsIDNamePair), ifObjectFactoryList(Of ifIDNamePair))

View 9 Replies

Overload Resolution Failed Error On Attempt To Add Item To Linked List

Apr 25, 2011

This overload resolution failed error that occurs when I try to add the newly created integer array as an item in a generic linked list as per the following?

Imports System.Collections.Generic
Public Class GraphData
Dim mInput As Byte()
Public Data() As Integer
Dim SignalData As New LinkedList(Of Integer)(Data)
Private Sub Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
[Code] .....

View 2 Replies

OrderBy Nullable Properties In A Generic List Throwing InvalidCastException?

Jun 13, 2012

I need to return an a generic list in the correct order for my project, and I'm getting InvalidCastException errors. Here is the code:

Dim lDt As List(Of Comment) = RemapCommentsForE1(so.CommentList). _
OrderBy(Function(x) x.CreateDate.Value). _
ThenBy(Function(x) x.Sequence). _

[code].....

View 2 Replies

Generate A List Of Random Integer With A Click Of The Button And Put The Results In A List Box Using .Net?

Nov 18, 2009

If I can generate a list of random integer with a click of the button and put the results in a list box using VB.Net but how do I randomly change several integer number generated by button 1 by clicking button 2? How I retain the results of button1 and change the results when clicking button2?I try before but the two button function code cannot relate to one another.

View 1 Replies

Get Results To Post To One Row In A List Box?

Nov 20, 2009

i am trying to get my results to post to one row in a list box for each one but instead it just keep adding up with a million rows and adding each result properly,

Dim A08count As String
Dim A01count As String
Dim totalA08 As Integer

[Code].....

View 1 Replies

Save SQL Results Into A List?

Jan 25, 2011

In my database one user can have multiple order. i want to get all orders for a user and save it in a list.[code]...

View 1 Replies

Place Results Into A List View?

May 22, 2009

I can't remember how I make an Access query, so could someone give me some help?

Also, how do I place the results into a List View?

View 4 Replies

Populating A List Box With Query Results?

Dec 9, 2009

I am working on making a Windows based application within Visual Basic 2008 Express that could potentially be used for a music school as a project. This application is connected and bound to a Microsoft Access database. Within this application I have a form for adding a new appointment reservation. This form has a combo box that the user can select the lesson type (eg. "I want a piano lesson"). Upon being changed, a list box would be populated with the names of all instructors that have that selection as either their primary or secondary talent.So, my database is bound, the query is written (correctly...i hope), and I don't know how to 1) select the table column that I want displayed in the list box and 2) actually display the query results within the list box. Is this making sense? Probably not, bu

Private Sub cmbLessonType_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbLessonType.TextChanged
'populate the lboInstructor box

[code]....

View 5 Replies

[2008] Number Results In A List Box?

Feb 26, 2009

I want to list the results in my list box in a numbering format, something like this:

1: Result #1
2: Result #2
3: Result #3

How do you create this type of format in a list box.

View 18 Replies

Adding Items To List Results In Duplicates?

Jul 29, 2009

I have this code to return a list of fund sources for our organization.

Dim FundSourceList As New List(Of FundSource)
Dim fs As New FundSource
If results.Count > 0 Then[code].....

The problem is that when I loop through the resulting FundSourceList all it shows is the last value. For example, if I have three fund sources (state, federal, athletic), then when I use this code to loop through all I get listed is athletic, athletic, athletic.

For Each FundSource In FundSources
Debug.Print(FundSource.FundDescription)
Next

So I change the code to this. I moved the creation of the fs variable inside the loop.

Dim results = From result In dsResult.Tables(0) Select result
Dim FundSourceList As New List(Of FundSource)
If results.Count > 0 Then[code]....

This works fine but now I'm creating a new class over and over again. It seems a little inefficient to me. Can I not create the class outside the loop and use it over and over again?

View 3 Replies

Linq Return Results Contaiined In A List?

Mar 23, 2011

How can i select using linq from a datatable where the the only results I want back (ID's) are contained in a list of integers?

View 7 Replies

List Results From Command Line In Listbox

Mar 29, 2011

I am trying to develop an application and am still learning VB. I have a commandline that will list some applications that I need to make selectable to run certain processes based on what is selected. I have already found the MSDN stuff on using the RedirectStandardOutput class to add to streamwriter. What I need to do is read each line and add to a listbox so that the user can select each application. How to read each line of the stream and add to a listbox.

Here is that code.
' Define the namespaces used by this sample.
Imports System
Imports System.Text
Imports System.IO
Imports System.Diagnostics
[Code] .....

View 8 Replies

Query SQL Server Table Results Into A List?

Apr 13, 2012

I am trying to create a CLR SQL Stored procedure that uses a query and adds the results to a list in VB.NET. Through debugging I determined that my add statement for the reader into the List is somehow converting my data types and throwing an error when I attempt to execute the compiled Stored Procedure.The columns I am querying are Integers

Code:
Imports System
Imports System.Data

[code].....

View 3 Replies

Search Dataset And Populate List Box With Results?

Jul 28, 2011

I do however have a slight background with VBA particularly with MS Access, so I am not completely lost.I am recreating my Access database application in VB.net.I created a new project and added an Access database through the wizard, which automatically created a dataset for the database. On one of my forms I have, esentially, a search form. What I want to happen is have a text box, and as I start typing display a search result from my customers table in the list box under it. So for example, I type "A" and all customer names that start with "A" are displayed in the list box.I continue typing with a "p" (full string is now "Ap"), then all customer names that start with "Ap" are displayed and so on...

I did search on my own first before posting this and found a few tips but cant really get it to work the way I want. This is what I have... I know I would put this code in the keypress event of my test box, but just until I get it working I attached it to a button to search. (I basically got this online and not even sure its the correct usage.

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'Create a dataview[code].....

The item in red is where i get an error. it sucessfully builds and i open the find form enter a last name that I know is in the customers table, but I get the error "Cannot find Column ["whatever name i typed inthe text box"].So first of all is this the correct way of going about what I want to achieve?If so how can I get it to work?BTW the next step for the user would be to double click the customer in the list box they are searching for to open the customer Details form for that customer, so I would need an ID or Index attached to that item to be able to open that record in another form.

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

Using Polymorphism On Member Of Interface?

May 15, 2009

Okay this is a bit of a noobish question, but I ran across this today and it somewhat puzzles me WHY it would be this way.

Consider the following structure

Public Class Employee
Implements IPerson
Private _MyManager As Manager

[Code].....

Here is my question: I am wanting to have every person implement the IPerson interface, whether they are an employee, boss, or manager. However, they each have an attribute that refers to their leader (employees have a manager, managers have a boss, etc). Each implementation of their leader could be potentially differnt other than they fact that they implement the IAuthorityFigure interface. I am wanting to have the IPerson interface have a property for the IAuthorityFigure, but it throws a compiler error for me when I implement this b/c the IAuthorityFigure is not the same type as Boss or Manager (even though they implement the interface).

View 2 Replies

VS 2005 Program Using Polymorphism?

Oct 29, 2011

i have assignment which using polymorphism to get answer. Below here is answer from the question.anyone give me a code using polymorphism to get this answer

i= 100, j= 200, k= 300
value for i =100
value for(i+j)/2= 150
value for(i+j+k)/3 =200

View 2 Replies

VS 2010 Polymorphism Gone Awry?

May 20, 2011

I have a class that has two methods called "Add." One method excepts a FileInfoA object and the other excepts a DirectoryInfoA objectSo in my code, I'm adding FIAs and DIAs to property lists. But, it throws an exception because it's trying to process a FIA as a DIA, which means that the FIA is being added to the group of DIAs.I commented out the adding of DIAs to see if it would work and it did. When the DIA is commented out, the FIAs are filtered properly and the code executes properly.This is how my methods are declared:

VB.NET
''' <summary> ''' Adds a DirectoryInfoA class and adds it to one of the appropriate list. ''' </summary> ''' <param name="di">DirectoryInfo</param> '''

[code]....

View 4 Replies

Getting Multiple Selections From List Box To Display In The Results Label?

Nov 5, 2009

My name is Tim I'm having a problem getting my multiple selections from my list box to display in the results label and there is no examples in the text any how I'm thinking I need to use a loop to get it done. is this evern in the ball park?

Option Explicit On
Option Strict On
Option Infer Off
Public Class MainForm

[code].....

View 5 Replies

Listbox, Objects, Inheritance And Polymorphism

Jan 31, 2012

I wasn't quite sure how to label this but that should cover it all. I am using Linq to Sql classes to query a SQL Server table. That part is working fine. I'd like to add the records to a listbox and not just text. I can do this and it works. To get it to display something meaningful in the listbox, I have to override the ToString() function.

So, I've taken the base class tblVendor and created a new sub class tblVendorListObject that inherits tblVendor and overrides ToString to return Me.vendorName. I then use a foreach loop to iterate through the records returned by the query. Next, I create a new instance of tblVendorListObject and attempt to assign the current item in the foreach loop to it.

[Code]...

View 4 Replies

Polymorphism And Shadowing Inherited Members?

Nov 17, 2009

I have a couple of small classes to represent parts in a search filter. If the searched value equals NonValue the filter is supposed to do nothing. This is defined in a Base Class:

Private Class BaseFilter
Protected NonValue As Object
Protected sQueryStringBase As String = "AND {0} {1} {2} "[code]......

When I then create a StringFilter and check for allowed value:

Dim stf As New StringFilter()
stf.CheckNonValue(MyString)

I get a NullReferenceException (NonValue = Nothing) , when I expected the NonValue object to be String.Empty. Is this a bug in my code, or am I trying to achieve polymorphism in a wrong way?

View 3 Replies

Loop To Ping A List Of Ip's Return The Results And Save Them To An Access Db

Aug 6, 2009

Sorry for new thread--i have this code, basically a loop to ping a list of ip's return the results and save them to an access db:

[Code]...

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

[Code]...

View 24 Replies

VB 2010 - Assignment - Text File - List All Of The Data From Results.txt In The Listbox

May 4, 2012

I'm studying VB 2010 at uni and struggling with this assignment, I'm stressing now with one week to go before its time to hand it in and still not knowing how to do it (even though i've read the whole textbook!)

I have copied the instructions and the files I have been given.

1 List all of the data from Teams.txt in a Listbox. Include suitable headings.

2 List all of the data from Results.txt in the Listbox (use the same Listbox as in 1 above). Include suitable headings. Note that you cannot assume that the file contains only results for the first 3 weeks of the season. Your program should be able to process a results file with more or fewer results.

3 List all of the data from Results.txt in the Listbox. However, in this case the actual team names should be output (your program will need to get this data from Teams.txt. The data should be formatted so that it is aligned correctly. Include suitable headings.

View 4 Replies

Cleanly Deal With Different Behavior Based On Polymorphism?

Mar 24, 2011

Suppose I have an interface IFoo with implementation classes VideoFoo, AudioFoo, and TextFoo. Suppose further that I cannot modify any of that code. Suppose that I would then like to write a function that acts differently based on the runtime type of IFoo, such as

[Code]...

Then I have to do my "If TypeOf ... Is" again. How can I refactor this to take advantage of the polymorphism of the implementations of IFoo without manually checking the types? (in VB.NET, though my question applies to C# too)

View 3 Replies

Polymorphism - Convert Square Object To A Shape

Sep 16, 2011

In VB.NET, say I have a function Public Function Foo(ByVal currentShape as Shape) Instead up passing in a Shape object, I pass in a subclass of Shape called Square like such: Dim square As Square = new Square() Foo(square). Do I need to convert my Square object to a Shape object before passing it in? If so, how do I do this?

View 4 Replies

Use The IList(Of T).Max Function?

Jul 8, 2011

How do I use the IList(Of T).Max function in my example below?

Dim myList as IList(Of Integer)
For x = 1 to 10
myList.add(x)
Next

'Error: 'Max' is not a member of 'System.Collections.Generic.IList(Of Integer)'
MsgBox(myList.Max())

View 2 Replies

Vb Wpf Database Applicationwin - Name Entered By The User From A Textbox And Display Query Results In A List Box

Apr 10, 2009

How do you carry out a query from a criteria e.g name entered by the user from a textbox. and then display the query results in a list box..

View 2 Replies

Definition - Class - Object - Inheritance - Polymorphism - Abstraction

Mar 8, 2010

Can you define this :

Class
Object
Inheritance
Polymorphism
Abstraction

And could you make it easy to understand I'm having a hard time to understand those terms.

View 7 Replies







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