Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
|
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 13th, 2005, 03:30 PM
Registered User
 
Join Date: Jul 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Help with an arraylist

Hi all,

I need some help...i am writing a small harness application to test a component written by the dev team in my company. My app basically allows you to build a database command object through the ui - so specify a sql query, db connection and parameters, run this against the component being tested, and then display the results in the UI.

For the parameter collection, my idea was to use an arraylist to pick up the specified parameters (benefits are variable size and also can pick up any data type, which is necessary as parameters will be a range of types).

However, when i execute the code, I get back a custom error message from the component saying "Parameter count is incorrect". However, if i use a normal array it works - so there is something specific about ArrayLists that isn;t working.

So just as a quick example, say i build my arraylist like this:

Code:
ArrayList myArrayList = new ArrayList();
myArrayList.Insert(0, "Test1");
myArrayList.Insert(1, "Test2");
Then this is the code i am using to call the function in the component i'm testing, specifying my arraylist as a parameter (the function accepts a params object]:

Code:
command = new SqlCommand();
command.ConnectionTitle = connTitle;
command.CommandID = cmdId;
command.SqlCommandType = SqlCommandType.CommandFile;
command.AddParameters(myArrayList);
That when i execute the above code, it doesn't work. However, if i use an Array instead of an arraylist, it does work, so:

Code:
string[] myarray = new string[2];
myarray.SetValue("Test1",0);
myarray.SetValue("Test2",1);
That returns results as expected....

Hope it's clear what i'm trying to do. Can anyone help???

Thanks!
Crazy-Nun
 
Old July 13th, 2005, 05:33 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

What kind of class is the SqlCommand in your example?

I see all kind of properties that a normal SqlCommand doesn't have.

And what kind of overloads does AddParameters have? The ArrayList will hold Objects, whereas your array holds strings. Also the order of parameter names and values seem different....

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old July 13th, 2005, 11:53 PM
Registered User
 
Join Date: Jul 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry, i should have said that the component I am testing is a database abstraction so that developers in the team (who are all writing different data-centric applications) do not have to worry about the database platform in use. So the SqlCommand you see in my example should actually be replaced with the name of the component (i didn't put it in my example as I am not yet allowed to reveal the name of this component, but let's call it MyDBAbstraction (with the command object being called MyDBCommand, so this would replce SqlCommand in my example above). It makes more sense as the command could really be SQL, OLEdb, ODBC, Oracle...

Also, what do you mean by overloads? Sorry, I am a beginner so don't know what that means. Do you mean what kind of objects it can hold? Well, i know that the method which i am calling the MyDbAbstraction class, accepts a params object[] keyword - so i would have thought it would accept an arraylist.

Is this enough information for you?

Thanks!

 
Old July 14th, 2005, 02:02 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Although in some respects an ArrayList looks like an array, the two are not the same. So, a method that expects object[] can not simply accept an ArrayList.

Consider this simple example:

    private void Start()
    {
      string [] myStringArray = new string[5];
      ArrayList myArrayList = new ArrayList();
      DoSomething(myStringArray);
      DoSomething(myArrayList);
    }

    private void DoSomething(string[] test)
    {

    }

    private void DoSomething(ArrayList test)
    {

    }

This will compile because the second overload call to DoSomething, the overloaded method (an overload is a method with same name as an existing one, but with a different signature; e.g. the first expects an string array, the other an ArrayList) is used.

If you remove the second DoSomething, you'll get a compile error.

So ArrayLists cannot be passed as string arrays. Tow ways to fix this:

1. Create an overload of the AddParameters that accepts an ArrayList

2. Use the ToArray() method to copy the elements from the ArrayList into a new object array that you can pass to AddParameters.

HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old July 14th, 2005, 03:32 AM
Registered User
 
Join Date: Jul 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Imar, that does help! Thank you so much for your clear explanation! The second option (ToArray) works perfectly.

Thanks again for your time!!!

Crazy-Nun






Similar Threads
Thread Thread Starter Forum Replies Last Post
ArrayList Mogg-Way C# 2005 5 February 12th, 2008 04:05 PM
ArrayList sort collie C# 1 August 28th, 2007 08:08 AM
ArrayList with UserControl rhd110 General .NET 2 August 12th, 2007 12:29 PM
ArrayList Problem erictamlam C# 2005 3 May 12th, 2007 10:04 AM
ArrayList kobystud C# 4 May 25th, 2004 02:05 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.