You can probably use foreach to put each item in the arraylist into hashtable?
foreach (object item in myArrayList)
{
//add item into your hashtable
}
Main Topics
Browse All TopicsI am writing a program that takes data that is entered and stores it in a hashtable, then copies it to an arraylist and puts it in an XML file with serialization... this part of the program is all working fine...
what I am having trouble with is loading the file back in from the XML file to the arraylist and then to the hashtable... I believe that I am copying back from the XML file to the arraylist just fine, but I don't know how to copy from the arraylist to the hashtable..
This is a homework program so I am not looking for the coded answer but just something that can guide me in the right direction... I have been stuck at this point for a while and have every other part of the program working...
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Looking at this further I believe I have to use foreach and copy each instance into a class instance, and then copy the class instance to the hashtable... I have two problems with this though..
First:
i have this in a try/catch statement, and for some reason the line here that fills the array list from the stream causes it to catch every time... I'm not sure why this is...
FileStream fs = new FileStream(Application.Sta
SoapFormatter sf = new SoapFormatter();
theTeams = (ArrayList)(sf.Deserialize
Second:
I'm not sure how to take the information from the arraylist and put it in the class instance... I think that I would put something like aTeam.teamName, but I don't know what it should equal..
Hm... I'm not quite sure how you are using the arraylist and hashtable.
1. What error did you get when deserializing it? Is theTeams an arraylist or a class instance?
2. Again, how did you put the class instances into arraylist? We just have to do the reverse right?
Here is an example of serializing/deserializing a class, hope it may help
http://www.devhood.com/tut
When deserializing I don't receive an error, but it goes to the catch... I'm not sure how to tell what the issue is there...
I put the class instances into the arraylist with the line
theTeams.Add(aTeam);
theTeams is the arraylist, and aTeam is the class instance
I am going to meet with my teacher tomorrow afternoon, but I was just hoping to have the program completed prior to the meeting... He will help me if I can't figure it out... I am going to look through the deserializing information you just sent me... hopefully it helps... if you would like me to post code I will... maybe that would help...
In the catch block you can see the message of the exception, right?
When it goes to catch...
try
{
//deserialize
}
catch(Exception ex)
{
MessageBox.Show(ex.Message
}
You can do this to get each item in the arraylist into a class instance
foreach (object item in theTeams)
{
aTeam = item;
//do something with aTeam...
}
I wouldn't mind looking at your code if it's not too long =P
jk, yea just post it here hehe
I think you will be able to help me a lot by looking at the code.. I'm sure you will find it very simple.. I think the main problem here is I don't know how to explain my problem very well..
Also, after looking further into things I don't believe the deserialization is the problem.. I think everything is working except I need to add a foreach statement to move things from the arraylist to class instances and then to the hashtable... I've never used a foreach statement before so I'm having trouble understanding what syntax I should be putting in it... the foreach statement will be in btnLoad_Click
I really appreciate your help.... I'm sure you have better things to be doing, but its frustrating learning programming :)
namespace NFL
{
public partial class frmNFL : Form
{
public frmNFL()
{
InitializeComponent();
}
private string teamFile = Application.StartupPath + @"\NFLTeams.xml";
Hashtable Teams = new Hashtable();
Team aTeam;
string teamKey;
ArrayList theTeams = new ArrayList();
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnClear_Click(object sender, EventArgs e)
{
txtTeam.Clear();
txtLocation.Clear();
txtStadium.Clear();
txtConference.Clear();
txtDivision.Clear();
}
private void btnSaveRecord_Click(object
{
try
{
// Create new Class Instance
aTeam = new Team();
aTeam.teamName = txtTeam.Text;
aTeam.location = txtLocation.Text;
aTeam.stadium = txtStadium.Text;
aTeam.conference = txtConference.Text;
aTeam.division = txtDivision.Text;
teamKey = txtTeam.Text;
Teams.Add(teamKey.GetHashC
// Add new instance to arraylist
theTeams.Add(aTeam);
}
catch
{
MessageBox.Show("Problem Saving Record", "Team Record", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void btnRetrieve_Click(object sender, EventArgs e)
{
try
{
aTeam = new Team();
teamKey = txtTeam.Text;
aTeam = (Team)Teams[teamKey.GetHas
txtTeam.Text = aTeam.teamName;
txtLocation.Text = aTeam.location;
txtStadium.Text = aTeam.stadium;
txtConference.Text = aTeam.conference;
txtDivision.Text = aTeam.division;
}
catch
{
MessageBox.Show("Problem Retrieving Record", "Team Record", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void btnLoad_Click(object sender, EventArgs e)
{
try
{
FileStream fs = new FileStream(teamFile, FileMode.Open);
SoapFormatter sf = new SoapFormatter();
theTeams = (ArrayList)(sf.Deserialize
fs.Close();
// now instances must be removed from arraylist and added to hash table using the team as
// the teamkey for each instance, as they are deserialized
btnRetrieve.Enabled = true;
btnLoad.Enabled = false;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message
}
}
private void btnSaveTable_Click(object sender, EventArgs e)
{
FileStream fs = new FileStream(teamFile, FileMode.Create);
SoapFormatter sf = new SoapFormatter();
sf.Serialize(fs, theTeams);
fs.Close();
}
}
[Serializable]
public class Team
{
public string teamName;
public string location;
public string stadium;
public string conference;
public string division;
}
}
Hehe it is frustrating but it is fun once you solve it XD
I think this should do it
.....
// now instances must be removed from arraylist and added to hash table using the team as
// the teamkey for each instance, as they are deserialized
foreach (Object teamObject in theTeams)
{
string _teamKey = ((Team)teamObject).teamNam
Teams.Add(_teamKey.GetHash
}
theTeams.Clear(); // Clear the arraylist
.....
foreach would simply loop over each item in the ArrayList
The foreach loop above can also be written as something like this
for(int i=0; i<theTeams.Length; i++)
{
Object teamObject = theTeams[i];
string _teamKey = ((Team)teamObject).teamNam
Teams.Add(_teamKey.GetHash
}
Business Accounts
Answer for Membership
by: HardiPosted on 2007-10-29 at 17:01:21ID: 20174093
Hi brassmon,
How did you copy from the hashtable into arraylist?