locked
How its possible to pass one object as a parameter only for one time in c# RRS feed

  • Question

  • User234527241 posted

    Hello friends

    I had a question In SO ,but none of the answers were tricky and satisfied me ,I d like to know your ideas about that issue .I really want to know what is the best design ,and is there any solution without throwing exception in Run time or not ? so I want to ask you please take a looke at this question and let me know what do you think. any comments ,advices or help would be  appreciated.

    Tuesday, June 7, 2011 9:45 AM

Answers

  • User2020552474 posted

    Hi sia,

    There's alot of other issues that need to be considered here - for example you really should be storing the data in a database system, rather than creating Person objects and Factory objects on the fly.  But this is another discussion altogether about n-tier design.

    Back to your described question; using basic class definitions you have a PERSON class, and a FACTORY class.

    I've made it simple.  FACTORY class has a ManagerID instead of a Manager Object (but you get the idea hopefully).

    public class Person {
        public int ID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
    
    public class Factory {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public int ManagerID { get; set; }
    }
    

    Below is the Manager class i'm referring to.  The Manager class holds the list of FACTORY objects.

    There is a ADD method that checks if the Manager is already in the List.
    If yes, then we throw an exception back to the calling program.
    If no, then we create the Factory, and link the ManagerID, then add the Factory to the List.

    public class FactoryManager {
    
        public List<Factory> FactoryList { get; set; }
    
        public FactoryManager() {
                this.FacoryList = new List<Factory>();
        }
    
        public AddFactory(int managerID, int factoryID, string factoryName, string factoryDesc) {
            bool alreadyLinked = FactoryList.Where(i => i.Manager.ID = managerID).Count() > 0);
    
            if (alreadyLinked) {
                 throw new Exception("Manager is already linked to another factory.");
            } else {
                Factory f = new Factory();
                f.ID = factoryID;
                f.Name = factoryName;
                f.Description = factoryDesc;
                f.ManagerID = managerID;
    
                this.FactoryList.Add(f);
            }
    
        }
    
    }
    
    
    public class Program {
    
        public class main() {
    
                try {
                    FactoryManager mgr = new FactoryManager();
                    mgr.AddFactory(1, 1, "Factory1", "Desc1");
                    mgr.AddFactory(1, 2, "Factory1", "Desc1");
                } catch (Exception ex) {
                    Console.Writeline(ex.message);
                }
    
        }
    
    }

    The main program ( if its a console application ) catches the Exception, and displays the message.

    If its a windows app, or Web app - you can display the message in a textbox, or label, or popup dialog.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 10, 2011 4:08 AM

All replies

  • User2020552474 posted

    I think your design needs to change a little bit.

    You have a Person entity

    You have a Factory entity

    You want one Factory to be linked to only one Person correct ?

    As a result, you want one Person to only look after just one Factory, no more than one correct ?

    In your other post, you had your main function create one person. And link this person to many Factory objects - which is what you want to prevent if i read it correctly.  The design you want to consider is having a 'manager' class that maintains your list of Factories, or Person (whichever way you choose) because before a 'person' is linked to a 'factory' this 'manager' class needs to ensure the new factory/person is not already linked.

    This is done by using a linq query, or search through your factory/person collection - looking for the personID, or FactoryID - if it exists, then you know to throw an error message saying there is already a mapping.

    Thursday, June 9, 2011 7:30 PM
  • User234527241 posted

    Hi and thank u for your responce . what is your solution to prevent throwing exception in run time ?

     unfortuantly after  searching I coudnt find any source code and sample that provide a good picture of implementing object oriented relashinships .anyway your clue clarified my mind but if its possible please give me some code .and if you know any source code  or sample please introduce me to  understand the case better . for example I am very intersted to know which parts of .net framework utilizes association OR aggregation OR Composition . definitely examining the .net framework could be helpfull. thank u again.  

    Friday, June 10, 2011 3:36 AM
  • User2020552474 posted

    Hi sia,

    There's alot of other issues that need to be considered here - for example you really should be storing the data in a database system, rather than creating Person objects and Factory objects on the fly.  But this is another discussion altogether about n-tier design.

    Back to your described question; using basic class definitions you have a PERSON class, and a FACTORY class.

    I've made it simple.  FACTORY class has a ManagerID instead of a Manager Object (but you get the idea hopefully).

    public class Person {
        public int ID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
    
    public class Factory {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public int ManagerID { get; set; }
    }
    

    Below is the Manager class i'm referring to.  The Manager class holds the list of FACTORY objects.

    There is a ADD method that checks if the Manager is already in the List.
    If yes, then we throw an exception back to the calling program.
    If no, then we create the Factory, and link the ManagerID, then add the Factory to the List.

    public class FactoryManager {
    
        public List<Factory> FactoryList { get; set; }
    
        public FactoryManager() {
                this.FacoryList = new List<Factory>();
        }
    
        public AddFactory(int managerID, int factoryID, string factoryName, string factoryDesc) {
            bool alreadyLinked = FactoryList.Where(i => i.Manager.ID = managerID).Count() > 0);
    
            if (alreadyLinked) {
                 throw new Exception("Manager is already linked to another factory.");
            } else {
                Factory f = new Factory();
                f.ID = factoryID;
                f.Name = factoryName;
                f.Description = factoryDesc;
                f.ManagerID = managerID;
    
                this.FactoryList.Add(f);
            }
    
        }
    
    }
    
    
    public class Program {
    
        public class main() {
    
                try {
                    FactoryManager mgr = new FactoryManager();
                    mgr.AddFactory(1, 1, "Factory1", "Desc1");
                    mgr.AddFactory(1, 2, "Factory1", "Desc1");
                } catch (Exception ex) {
                    Console.Writeline(ex.message);
                }
    
        }
    
    }

    The main program ( if its a console application ) catches the Exception, and displays the message.

    If its a windows app, or Web app - you can display the message in a textbox, or label, or popup dialog.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 10, 2011 4:08 AM
  • User234527241 posted

    Hi and thank u ,I agree with u that we need an intermediate object , and also adding ID as a new member to our classes is creative . but still i wish  we could  find a way without Need to throw exception in run time. I am going to post a perfect answer of the problem based on your suggestions ,please dont leave me alone and recheck this Thread in the future  .thanks

    Saturday, June 11, 2011 11:33 AM