Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 ArrayList help - Stumped!

Author  Topic 

ann
Posting Yak Master

220 Posts

Posted - 2007-12-19 : 10:44:29
I am using .net 1.1. I have a custom class which contains an arraylist. When I add anything to my arraylist, it increments correctly. When I try to access the arraylist I end up with a count of 0. Here is my class code:

private ArrayList _arrVDealers = new ArrayList();

public Vehicle()
{
// TODO: Add constructor logic here
}

public void AddDealer(Dealer dealer)
{
_arrVDealers.Add(dealer);
}
public ArrayList arrVDealers
{
set { _arrVDealers = value; }
get { return this._arrVDealers ; }
}


Here is my calling code:

Trade.Text = ((Vehicle)arrVehicles[_RowCounter]).TradeInfo.ToString();
arrDs = ((Vehicle)arrVehicles[_RowCounter]).arrVDealers;

the TradeInfo is correct - the arrVDealers returns 0, but incremented correctly when I was adding the dealer.

Can anyone please help me with this - I am totally stumped!
Thanks

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-12-19 : 10:52:58
I can't make heads or tails of what you are doing. You show code for a Vehicle class but in your example code you are calling using some arrVehicles variable that apparently is some other class. You also never show the code that actually adds any dealers.


- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

ann
Posting Yak Master

220 Posts

Posted - 2007-12-19 : 11:05:17
You also never show the code that actually adds any dealers.

I add the dealer class here:
public void AddDealer(Dealer dealer)
{
_arrVDealers.Add(dealer);
}

I don't know why having the dealer class is important - can you explain?
All I want to do is have an arraylist of another class within my Vehicle class. I have to use an ArrayList as this is for a third party control I use for a report and only recognizes arraylist.

I know I'm not implementing this properly, that's why I'm asking for help. I need a class that contains an arraylist of another object, then I want to call the that arraylist

Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-12-19 : 11:10:47
That is the method that adds the dealer, you never show any code that actually calls this method.

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-12-19 : 11:10:48
either you don't have any elemets at that time in the array list or you're using another instance of the class.
what does your debugger say?

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
SSMS Add-in that does a few things: www.ssmstoolspack.com <- new version out
Go to Top of Page

ann
Posting Yak Master

220 Posts

Posted - 2007-12-19 : 11:23:57
My report contains heirachle vehicles with dealers. I add the vehicles to an arraylist and loop through to get my vehicle information - the results which are correct. While I am looping through the vehicles, I need to find and display all the associated dealers for each vehicle, that is why I have an arraylist of dealers within the vehicle. If I'm looping through the arraylist of vehicles and they return the correct value, I don't get why I loose the dealers or why there would be a new instance of vehicle since I'm trying to access the dealers when I am looping through the vehicle:

for(i = 0:......)
{
Trade.Text = ((Vehicle)arrVehicles[_RowCounter]).TradeInfo.ToString();
arrDs = ((Vehicle)arrVehicles[_RowCounter]).arrVDealers;
}


The tradeInfo is getting the correct data, the arrVDealers is 0, but should contain dealers because when I add a dealer to the vehicle class, it increments correctly.

private ArrayList _arrVDealers = new ArrayList();

public Vehicle()
{
// TODO: Add constructor logic here
}

public void AddDealer(Dealer dealer)
{
_arrVDealers.Add(dealer);
}
public ArrayList arrVDealers
{
set { _arrVDealers = value; }
get { return this._arrVDealers ; }

}

Maybe I'm adding the dealer incorrectly?? I just don't know :(
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-12-19 : 11:40:58
shouldn't it be:
Trade.Text = ((Vehicle)arrVehicles[i]).TradeInfo.ToString();
arrDs = ((Vehicle)arrVehicles[i]).arrVDealers;

and do use it like this since it's easier to understand:

for(...)
{
Vehicle vehicle = (Vehicle)arrVehicles[i];
Trade.Text = vehicle.TradeInfo.ToString();
arrDs = vehicle.arrVDealers;
}



_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
SSMS Add-in that does a few things: www.ssmstoolspack.com <- new version out
Go to Top of Page

ann
Posting Yak Master

220 Posts

Posted - 2007-12-19 : 11:51:42
Spirit - yes, yours is much easier to read, but neither way is correct as far as retrieving the array of dealers. For the heck of it, I did it your way, and it still returns 0 dealers
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-12-19 : 11:58:01
did you change _RowCounter to i?

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
SSMS Add-in that does a few things: www.ssmstoolspack.com <- new version out
Go to Top of Page

ann
Posting Yak Master

220 Posts

Posted - 2007-12-19 : 12:06:06
I am actually using _RowCounter within my code, but typed in the for i for this messageboard to save on the typing. If there was something wrong with the _RowCounter, then I would never have been able to pull any vehicle information (which I can and works fine) - the issue is with the dealers.
Thanks
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-12-19 : 12:18:21
well without the full code it's hard to help. i don't see anything obvious that would be wrong.
are you setting arrVDealers anywhere with the property?

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
SSMS Add-in that does a few things: www.ssmstoolspack.com <- new version out
Go to Top of Page

ann
Posting Yak Master

220 Posts

Posted - 2007-12-19 : 12:23:18
this is a stripped down version of the vehicle class:


public class Vehicle
{
private string vInfo = "";
private int vID ;
private ArrayList _arrVDealers = new ArrayList();

public Vehicle()
{
// TODO: Add constructor logic here
}

public ArrayList AddDealer(Dealer dealer)
{
_arrVDealers.Add(dealer);
}
public ArrayList arrVDealers
{
set { _arrVDealers = value; }
get { return this._arrVDealers ; }

}

public int VID
{
get { return this.vID ; }
set {vID = value; }
}
public string VInfo
{
get { return this.vInfo ; }
set {vInfo = value; }
}
}


EDIT by spirit1: put the code in [c o d e] tags
Go to Top of Page

ann
Posting Yak Master

220 Posts

Posted - 2007-12-19 : 12:25:46
sorry the add method should be a void - I've been tinkering with this so long

public void AddDealer(Dealer dealer)
> {
> _arrVDealers.Add(dealer);
> }
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-12-19 : 12:28:32
oh by the way use [c o d e] [/ c o d e] blocks (witout spaces) for your code.

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
SSMS Add-in that does a few things: www.ssmstoolspack.com <- new version out
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-12-19 : 12:31:40
there's nothing wrong with this class.
show us the code that uses and fills the class.

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
SSMS Add-in that does a few things: www.ssmstoolspack.com <- new version out
Go to Top of Page

ann
Posting Yak Master

220 Posts

Posted - 2007-12-19 : 12:32:34
Thanks for the tip - it certainly makes it easier to read... now if only it would fix my problem!
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-12-19 : 13:26:40
Even after all this, you still have not shown any code that actually calls the AddDealer() method to add items to the ArrayList...

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page
   

- Advertisement -