Issues with ArrayList of Objects C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Richard Cane
    New Member
    • Mar 2012
    • 1

    Issues with ArrayList of Objects C#

    I am having a problem with an ArrayList of Objects.
    ...'object' does not contain a definition for 'NearbyBranchId s' and no extension method 'NearbyBranchId s' accepting a first argument of type 'object' could be found

    Code:
        public class Parser
        {
            public ArrayList ParseBranch(string dataFileLocation)
            {
                var branchList = new ArrayList();
    
                using (var inData = new StreamReader(dataFileLocation))
                {
                    while (!inData.EndOfStream)
                    {
                        var branch = Branch(inData);
    
                        if (branch != null)
                            branchList.Add(branch);
                    }
                }
    
                foreach (var branch in branchList)
                {
                    foreach (var branchID in branch.NearbyBranchIds)
                    {
                        foreach (var nextBranch in branchList)
                        {
                            if (branch == nextBranch)
                                continue;
    
                            if (nextBranch.BranchID == branchID)
                            {
                                branch.NearbyBranches.Add(nextBranch);
                                continue;
                            }
                        }
                    }
                }
    
                return branchList;
            }
    
            private Branch Branch(StreamReader inData)
            {
                var branch = new Branch();
                
                branch.BranchID = Convert.ToInt32(inData.ReadLine());
                branch.Nickname = inData.ReadLine();
                branch.StreetNumber = inData.ReadLine();
                branch.StreetName = inData.ReadLine();
                branch.City = inData.ReadLine();
                branch.County = inData.ReadLine();
                branch.Postcode = inData.ReadLine();
    
                branch.NearbyBranchIds.Add(Convert.ToInt32(inData.ReadLine()));
                branch.NearbyBranchIds.Add(Convert.ToInt32(inData.ReadLine()));
                branch.NearbyBranchIds.Add(Convert.ToInt32(inData.ReadLine()));
    
                int categories = Convert.ToInt32(inData.ReadLine());
                for (int i = 0; i < categories; ++i)
                {
                    var category = ParseCategory(inData);
    
                    if (category != null)
                        branch.Categories.Add(category);
                }
    
                return branch;
            }




    And the constructor:
    Code:
        public class Branch
        {
            public int BranchID { get; set; }
            public string Nickname { get; set; }
            public string StreetNumber { get; set; }
            public string StreetName { get; set; }
            public string City { get; set; }
            public string County { get; set; }
            public string Postcode { get; set; }
            public ArrayList NearbyBranchIds { get; private set; }
            public ArrayList NearbyBranches { get; private set; }
            public ArrayList Categories { get; private set; }
            
            public Branch()
            {
                NearbyBranchIds = new ArrayList();
                NearbyBranches = new ArrayList();
                Categories = new ArrayList();
            }
        }
    Last edited by Frinavale; Mar 20 '12, 06:34 PM.
  • bshelton3
    New Member
    • Mar 2012
    • 3

    #2
    In your Parser class try this:
    Code:
                foreach (Branch branch in branchList)
                {
                    foreach (Int32 branchID in branch.NearbyBranchIds)
                    {
                        foreach (Branch nextBranch in branchList)
                        {
                            if (branch == nextBranch)
                                continue;
    
                            if (nextBranch.BranchID == branchID)
                            {
                                branch.NearbyBranches.Add(nextBranch);
                                continue;
                            }
                        }
                    }
                }

    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:
    Code:
        public class Parser
        {
            public List<Branch> ParseBranch(string dataFileLocation)
            {
                var branchList = new List<Branch>();
    
                using (var inData = new StreamReader(dataFileLocation))
                {
                    while (!inData.EndOfStream)
                    {
                        var branch = Branch(inData);
    
                        if (branch != null)
                            branchList.Add(branch);
                    }
                }
    
                foreach (var branch in branchList)
                {
                    foreach (var branchID in branch.NearbyBranchIds)
                    {
                        foreach (var nextBranch in branchList)
                        {
                            if (branch == nextBranch)
                                continue;
    
                            if (nextBranch.BranchID == branchID)
                            {
                                branch.NearbyBranches.Add(nextBranch);
                                continue;
                            }
                        }
                    }
                }
    
                return branchList;
            }
    
            private Branch Branch(StreamReader inData)
            {
                var branch = new Branch();
    
                branch.BranchID = Convert.ToInt32(inData.ReadLine());
                branch.Nickname = inData.ReadLine();
                branch.StreetNumber = inData.ReadLine();
                branch.StreetName = inData.ReadLine();
                branch.City = inData.ReadLine();
                branch.County = inData.ReadLine();
                branch.Postcode = inData.ReadLine();
    
                branch.NearbyBranchIds.Add(Convert.ToInt32(inData.ReadLine()));
                branch.NearbyBranchIds.Add(Convert.ToInt32(inData.ReadLine()));
                branch.NearbyBranchIds.Add(Convert.ToInt32(inData.ReadLine()));
    
                int categories = Convert.ToInt32(inData.ReadLine());
                 for (int i = 0; i < categories; ++i)
                 {
                     var category = ParseCategory(inData);
     
                     if (category != null)
                         branch.Categories.Add(category);
                 }
      
                return branch;
            }
        }
        public class Branch
        {
            public int BranchID { get; set; }
            public string Nickname { get; set; }
            public string StreetNumber { get; set; }
            public string StreetName { get; set; }
            public string City { get; set; }
            public string County { get; set; }
            public string Postcode { get; set; }
            public List<int> NearbyBranchIds { get; private set; }
            public List<Branch> NearbyBranches { get; private set; }
            public List<int> Categories { get; private set; }
    
            public Branch()
            {
                NearbyBranchIds = new List<int>();
                NearbyBranches = new List<Branch>();
                Categories = new List<int>();
            }
        }
    In my opinion it would be better to use this method.
    Last edited by bshelton3; Mar 23 '12, 02:28 PM.

    Comment

    Working...