Object Modelling problem

Hi Guys,
            I am making my first steps in C# and would like to design a little object model to power my future client side development. I am in the accounting industry so my objects revolve around three major aspects ; transaction, security, and account. As a first step I would like to create two Classes Account and Security and using those accomplish this simple task -using Account# and Security id I would like to retrieve the number of shares of a cirtain security in a given account.

The info is in a SQL Server database. For ex,
Database: my_Database
Table: my_Table, looks like

Account      Security      Shares
12345         abcde          100

 
I would like to retrieve 100 shares from a client code in the following form

Account.Security.Shares


I created Account Class with a constructor to assign the account# and Number property to access the number instance variable. I also created Security Class with one propery and one overloaded constructor to assign the security number. They are below....

 
 public class Account : Object
    {
        //declare instance variables
        private int number;
       
        //default  constructor
        public Account()
        {

        }
       //overload 1
        public Account(int accountNumber)
        {
            number = accountNumber;
            //also get the connection the database with account information
        }

       
        public string Number      
      {
            get
            {
                return number;
               
            }
            set
            {
                number =value;

            }

        }
       
}
 class Security
    {
        //declare instance variables
        private string id;
       

        public Security()
        {

        }
        public Security(string securityID)
        {
           id= securityID;
       
        }

   public string Cusip  
      {
            get
            {
                return id;
               
            }
            set
            {
                id =value;

            }

        }

    }

now in the client code I am initializing both Account object and Security object

Account account=new Account(12345);
Security security=new Security("abcde");

Now the question is what is missing to implement the desired functionality? What do I add or change to access # of shares
thru Account.Security.Shares    structure?   Alaso where in the code would you open the connection object to SQL  ?


Thank you very much,
Serge.
AutofreakAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mrichmonCommented:
I am a bit confused on what you want.

Based on the database layout  as well as your line about Account.Security.Shares comment it seems that Account and Security are related.

It seems to me that the structure would look something like:

public class Account
{
  private int accountNumber;
  private List<Security> securities;

  public Account(int accountNumber)
  {
       // Lookup account in database something like
      DataSet ds = DataLayer.Account.GetAccount(accountNumber);
       this.accountNumber = accountNumber;
      foreach(DataRow row in ds.Tables["Securities"])
      {
          this.securities.Add(new Security(row));
      }
   }

  public int AccountNumber { get { return this.accountNumber; } }
 // etc.
 
}

Then the security object would contain a shares object.

Then you could access like:

Account account=new Account(12345);

account.Securities["abcde"].Shares

Something like that?
0
AutofreakAuthor Commented:
Hi Mrichmon,
                      account.Securities["abcde"].Shares is the format I am looking for. Just a few questions...

1. Does that meen that an chierarchy is implemented? For a comparison let's take Excel's object model where the chierarchy  lookes like the following Application.Workbook(name).Worksheet(name).Range(name).Cell(row,column).Value . Is you solution providing for the same object modelling logic?

2. --> private List<Security> securities;
   What does this line mean? is it where you are making Class Security become Class Account's subClass?

3. I have posted your Class Account  code to my project but I can't access Security Class like account.Secirity("abcde"), the object is not there.

Thank you,
Serge
0
mrichmonCommented:
Well that code is not exact, as I was trying to get an idea of what you were looking for.

To truly get that syntax, now that I know that is what you want, you need to have a slight modification.

You need to have a class called AccountSecurities or something like that that inherits from a Collection like this:

public class AccountSecurities : Collection<Security>
{
  // In here you will need to overload anything the Collection class requires.
 
   //plus something like this to overload the indexer
public Security this[string securityCode]
{
      get
      {
            int index;
            for(index= 0; index< this.Count; index++)
            {
                  if(this[index].securityCode == securityCode)
                        break;
            }
            return(this[index]);
      }
      set
      {
            int index;
            for(index= 0; index< this.Count; index++)
            {
                  if(this[index]== securityCode)
                        break;
            }
            this[index] = value;
      }
}

}
0
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

AutofreakAuthor Commented:
mrichmon,
                Sorry, I am not sure I understand what the code is accomplishing here. Could you verbalize the algorithm please so that ? Also please provide an example of how to use the Class from the client code.

Thank you
Serge

0
mrichmonCommented:
I do not know what you mean by algoritm..  It is not an algorithm, but just a model.

The account contains a collection of securities.  A security contains a collection of shares - or an int that is a count - whatever you want.  

You create an account by specifying the account number

Account myaccount = new Account("12345");

That acount loads in all securites with each security loading in shares.

Then you access a security in an account by

Security mySecurity = myaccount["securityID"];

You access shares as mySecurity.Shares;

Also myaccount["securityID"].SHares;

Doe that help?

0
AutofreakAuthor Commented:

Thank you,
                I get the explanation but I need a complete code for the model to see how it all works. I am too inexperienced in C# to be able to complete the problem from the suggested model on my own. Can I ask you to write the whole code that works. That way I would see how the "cogs spi" and be able to experiment and learn on my own ;-).

Thanks,
Serge
0
mrichmonCommented:
I can't write "the whole complete" thinkg for you as I don't know your requirements.

Half of this involves connecting to a database and reading in data.

WHy don't you try to start writing it and post when you hit specific problems you cannot solve.
0
AutofreakAuthor Commented:
Ok , Perfect!
           How many classes are going to be involved? From what I see understand there 2 already - Account and AccountSecurities. One for ADO.NET connection. Another one with Main(). 4 so far, right?

Serge
           
0
mrichmonCommented:
Correct.  And one Security which represents an individual security.  Then if the Shares is a class you need one for that, unless it is just acount then it can be a member (int) of the security class.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AutofreakAuthor Commented:
I decided to stop pursuing this question and graded it C since nobody came out with a concrete answer. I am providing the link to my similar question that experts were able to finalize with an excellent mark.

 
http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_21825297.html
0
mrichmonCommented:
I really think the grading should not have been a C.  You say there was no concrete answer, but I gave you as much as possible without you providing more input as to your sepcific requirements and database information, which I mentioned in the comment on May 1st.
0
AutofreakAuthor Commented:
         I provided weechap with the same specs and received all the answeres I needed. Note that I didn't ask to help me with the database manipulations therefore had not provided DB specifics, although dummy DB names would do had you shosen to expand on that too.
               The question was about class design patterns and had a clear problem definition. I think there was enough onfo given in the problem to come up with what weechap accomplished.
                   Having said that I respect you opinion. It often happens that communication imperfections get in the way and quality is lost

Regards,
Serge

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.