Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 420999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T18:56:25+00:00 2026-05-12T18:56:25+00:00

I am using .NET 2.0. I have 2 objects one is: PhoneService and the

  • 0

I am using .NET 2.0. I have 2 objects one is: PhoneService and the other one is ChartAccount. The relationship between this to is many to many.

public class PhoneService
{    
    private List<ChartAccount> chartAccounts;
    private ChartAccount chartAccount;    
    public Int64 ID    
    {       get { return id; }     
        set { id = value; }    
    }    
    public bool Add()    { ... }    
    public bool Update()    { ... }
}

public class ChartAccount
{    
    public Int64 ID    
    {       get { return id; }     
        set { id = value; }    
    }    
    public bool Add()    { ... }    
    public bool Update()    { ... }    
    public bool Allocate()    
    {       
        // this will save data for the bridge table only    
    }
}

In my clients, i have the following code:

PhoneService service = new PhoneService(Int64.Parse(dataItem.OwnerTableView.DataKeyValues[dataItem.ItemIndex]["ServiceID"].ToString()));
if (service.ChartAccounts == null)
{
    Allocation allocation = new ChartAccountAllocation(Int64.Parse(drpdwnlstMainChartAccountAllocation.SelectedValue));
    int i=0;
    foreach(AllocationItem allocationItem in allocation.Items)
    {
        service.ChartAccounts[i].Name = allocationItem.Value2;
        service.ChartAccounts[i].SplitPercentage = Decimal.Parse(allocationItem.Value1);
        service.Allocate();
        i++;
     }
}

I got an error on the following line saying that Use the “new” keyword to create an object instance.
service.ChartAccounts[i].Name = allocationItem.Value2;

Because CharAccounts is a collection of ChartAccount object and it’s inside Service object so how do I define this thing then?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T18:56:26+00:00Added an answer on May 12, 2026 at 6:56 pm

    In your foreach loop, you’re doing service.ChartAccounts[i].Name = allocationItem.Value2, but we already know service.ChartAccounts is null (else you wouldn’t have passed the initial if condition.

    Before your foreach loop, you need to initialize service.ChartAccounts, by doing serviceChartAccounts = new List<ChartAccount>(). Then, before you set each value, you need to create a new ChartAccount before you set its Name property.

    I’d probably rewrite it something like this:

    if (service.ChartAccounts == null)
    {
        Allocation allocation = new ChartAccountAllocation(Int64.Parse(drpdwnlstMainChartAccountAllocation.SelectedValue));
    
        List<ChartAccount> acctList = new List<ChartAccount>();
        foreach(AllocationItem allocationItem in allocation.Items)
        { 
            ChartAccount acct = new ChartAccount();
            acct.Name = allocationItem.Value2;
            acct.SplitPercentage = Decimal.Parse(allocationItem.Value1);
            acctList.Add(acct);
        }
        service.ChartAccounts = acctList;
        service.Allocate(); // I have no idea what this does, so this may be incorrect here
    }
    

    The important thing to note is that you create the list, and create the ChartAccount objects before you use them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 250k
  • Answers 250k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You're missing some fundamentals about Linux linked lists. The following… May 13, 2026 at 9:13 am
  • Editorial Team
    Editorial Team added an answer Take a look at the "Son of Grab" sample. It… May 13, 2026 at 9:13 am
  • Editorial Team
    Editorial Team added an answer if (!empty($_POST)) { // handle post data } May 13, 2026 at 9:13 am

Related Questions

I currently have a class and I'm trying to create an easy GUI to
I'm using .Net 2.0 and this is driving me crazy but there's probably some
From what I have read best practice is to have classes based on an

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.