Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 13th, 2005, 03:30 PM
Registered User
 
Join Date: Jul 2005
Location: , , .
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
Reply With Quote
  #2 (permalink)  
Old July 13th, 2005, 05:33 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 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.
Reply With Quote
  #3 (permalink)  
Old July 13th, 2005, 11:53 PM
Registered User
 
Join Date: Jul 2005
Location: , , .
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!

Reply With Quote
  #4 (permalink)  
Old July 14th, 2005, 02:02 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 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.
Reply With Quote
  #5 (permalink)  
Old July 14th, 2005, 03:32 AM
Registered User
 
Join Date: Jul 2005
Location: , , .
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

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 08:51 PM.


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