473,395 Members | 1,761 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Issues with ArrayList of Objects C#

I am having a problem with an ArrayList of Objects.
...'object' does not contain a definition for 'NearbyBranchIds' and no extension method 'NearbyBranchIds' accepting a first argument of type 'object' could be found
Expand|Select|Wrap|Line Numbers
  1.     public class Parser
  2.     {
  3.         public ArrayList ParseBranch(string dataFileLocation)
  4.         {
  5.             var branchList = new ArrayList();
  6.  
  7.             using (var inData = new StreamReader(dataFileLocation))
  8.             {
  9.                 while (!inData.EndOfStream)
  10.                 {
  11.                     var branch = Branch(inData);
  12.  
  13.                     if (branch != null)
  14.                         branchList.Add(branch);
  15.                 }
  16.             }
  17.  
  18.             foreach (var branch in branchList)
  19.             {
  20.                 foreach (var branchID in branch.NearbyBranchIds)
  21.                 {
  22.                     foreach (var nextBranch in branchList)
  23.                     {
  24.                         if (branch == nextBranch)
  25.                             continue;
  26.  
  27.                         if (nextBranch.BranchID == branchID)
  28.                         {
  29.                             branch.NearbyBranches.Add(nextBranch);
  30.                             continue;
  31.                         }
  32.                     }
  33.                 }
  34.             }
  35.  
  36.             return branchList;
  37.         }
  38.  
  39.         private Branch Branch(StreamReader inData)
  40.         {
  41.             var branch = new Branch();
  42.  
  43.             branch.BranchID = Convert.ToInt32(inData.ReadLine());
  44.             branch.Nickname = inData.ReadLine();
  45.             branch.StreetNumber = inData.ReadLine();
  46.             branch.StreetName = inData.ReadLine();
  47.             branch.City = inData.ReadLine();
  48.             branch.County = inData.ReadLine();
  49.             branch.Postcode = inData.ReadLine();
  50.  
  51.             branch.NearbyBranchIds.Add(Convert.ToInt32(inData.ReadLine()));
  52.             branch.NearbyBranchIds.Add(Convert.ToInt32(inData.ReadLine()));
  53.             branch.NearbyBranchIds.Add(Convert.ToInt32(inData.ReadLine()));
  54.  
  55.             int categories = Convert.ToInt32(inData.ReadLine());
  56.             for (int i = 0; i < categories; ++i)
  57.             {
  58.                 var category = ParseCategory(inData);
  59.  
  60.                 if (category != null)
  61.                     branch.Categories.Add(category);
  62.             }
  63.  
  64.             return branch;
  65.         }




And the constructor:
Expand|Select|Wrap|Line Numbers
  1.     public class Branch
  2.     {
  3.         public int BranchID { get; set; }
  4.         public string Nickname { get; set; }
  5.         public string StreetNumber { get; set; }
  6.         public string StreetName { get; set; }
  7.         public string City { get; set; }
  8.         public string County { get; set; }
  9.         public string Postcode { get; set; }
  10.         public ArrayList NearbyBranchIds { get; private set; }
  11.         public ArrayList NearbyBranches { get; private set; }
  12.         public ArrayList Categories { get; private set; }
  13.  
  14.         public Branch()
  15.         {
  16.             NearbyBranchIds = new ArrayList();
  17.             NearbyBranches = new ArrayList();
  18.             Categories = new ArrayList();
  19.         }
  20.     }
Mar 20 '12 #1
1 1640
In your Parser class try this:
Expand|Select|Wrap|Line Numbers
  1.             foreach (Branch branch in branchList)
  2.             {
  3.                 foreach (Int32 branchID in branch.NearbyBranchIds)
  4.                 {
  5.                     foreach (Branch nextBranch in branchList)
  6.                     {
  7.                         if (branch == nextBranch)
  8.                             continue;
  9.  
  10.                         if (nextBranch.BranchID == branchID)
  11.                         {
  12.                             branch.NearbyBranches.Add(nextBranch);
  13.                             continue;
  14.                         }
  15.                     }
  16.                 }
  17.             }

The ArrayLists store their contents as the type 'object' and they are returning their contents as type 'object'. You could use generic List<T> and specify the types of the content that you want to store in the list. This would also prevent boxing/unboxing of basic data types like integers (better performance).

Here is the code using the generic lists:
Expand|Select|Wrap|Line Numbers
  1.     public class Parser
  2.     {
  3.         public List<Branch> ParseBranch(string dataFileLocation)
  4.         {
  5.             var branchList = new List<Branch>();
  6.  
  7.             using (var inData = new StreamReader(dataFileLocation))
  8.             {
  9.                 while (!inData.EndOfStream)
  10.                 {
  11.                     var branch = Branch(inData);
  12.  
  13.                     if (branch != null)
  14.                         branchList.Add(branch);
  15.                 }
  16.             }
  17.  
  18.             foreach (var branch in branchList)
  19.             {
  20.                 foreach (var branchID in branch.NearbyBranchIds)
  21.                 {
  22.                     foreach (var nextBranch in branchList)
  23.                     {
  24.                         if (branch == nextBranch)
  25.                             continue;
  26.  
  27.                         if (nextBranch.BranchID == branchID)
  28.                         {
  29.                             branch.NearbyBranches.Add(nextBranch);
  30.                             continue;
  31.                         }
  32.                     }
  33.                 }
  34.             }
  35.  
  36.             return branchList;
  37.         }
  38.  
  39.         private Branch Branch(StreamReader inData)
  40.         {
  41.             var branch = new Branch();
  42.  
  43.             branch.BranchID = Convert.ToInt32(inData.ReadLine());
  44.             branch.Nickname = inData.ReadLine();
  45.             branch.StreetNumber = inData.ReadLine();
  46.             branch.StreetName = inData.ReadLine();
  47.             branch.City = inData.ReadLine();
  48.             branch.County = inData.ReadLine();
  49.             branch.Postcode = inData.ReadLine();
  50.  
  51.             branch.NearbyBranchIds.Add(Convert.ToInt32(inData.ReadLine()));
  52.             branch.NearbyBranchIds.Add(Convert.ToInt32(inData.ReadLine()));
  53.             branch.NearbyBranchIds.Add(Convert.ToInt32(inData.ReadLine()));
  54.  
  55.             int categories = Convert.ToInt32(inData.ReadLine());
  56.              for (int i = 0; i < categories; ++i)
  57.              {
  58.                  var category = ParseCategory(inData);
  59.  
  60.                  if (category != null)
  61.                      branch.Categories.Add(category);
  62.              }
  63.  
  64.             return branch;
  65.         }
  66.     }
  67.     public class Branch
  68.     {
  69.         public int BranchID { get; set; }
  70.         public string Nickname { get; set; }
  71.         public string StreetNumber { get; set; }
  72.         public string StreetName { get; set; }
  73.         public string City { get; set; }
  74.         public string County { get; set; }
  75.         public string Postcode { get; set; }
  76.         public List<int> NearbyBranchIds { get; private set; }
  77.         public List<Branch> NearbyBranches { get; private set; }
  78.         public List<int> Categories { get; private set; }
  79.  
  80.         public Branch()
  81.         {
  82.             NearbyBranchIds = new List<int>();
  83.             NearbyBranches = new List<Branch>();
  84.             Categories = new List<int>();
  85.         }
  86.     }
In my opinion it would be better to use this method.
Mar 23 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: TT ( Tom Tempelaere ) | last post by:
Hi At one point in my application I have a single ArrayList object that I need to break up in two Arraylist objects: the beginning part up to an index, and the ending part from a certain index. I...
3
by: SpotNet | last post by:
Hi NewsGroup; Trying to construct a Multidimensional (2D more specifically) populated from an OleDbDataReader. Reason I'm using an ArrayList is I do not know the size of the first dimension...
3
by: Daves | last post by:
is it possible? if so - how would you do it?
0
by: stigbn | last post by:
When a DataSet is used as data source for a DataGrid one can get hiearachical datagrid by using relations among the DataTables of the DataSet. (By hierarchical datagrid, I mean columns that can be...
15
by: Sam | last post by:
Hi, I have the following code : Dim rNodeList As New ArrayList Dim rNodeListNew As New ArrayList For iCntr As Integer = 0 To tvRelations.Nodes.Count - 1...
8
by: lgbjr | last post by:
Hi All, I have 10 arraylists in a VB.NET app. Based on the state of various checkboxes and radiobuttons, a set of data is read into these arraylists. I don't know how many of the arraylists will...
15
by: GTi | last post by:
If I use: ArrayList TimeScale = new ArrayList(); TimeScale.Capacity = 1000; TimeScale="test 1" The last line trow me an error: Index was out of range. Must be non-negative and less than the...
16
by: vbnewbie | last post by:
I need to create a number of arraylists at run time. The number of arraylists to be created is unknown at design time. The name of the first newly created arraylist is the first element of an...
2
by: wbosw | last post by:
I have a Arraylist (SearchSkillsArray) that contains objects. I want to place those objects into a listbox. Here i'm using get enumerator to loop through the arraylist and add the objects to the...
1
by: gvi | last post by:
Hello everybody, Here is the scenario I am calling a webservice which is returning an object type. I need to bind that to the datagrid. So I thought I would ctype the object type to arraylist...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.