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

  • SEARCH
  • Home
  • 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

Related Questions

I am using .NET 2.0. I have 2 objects one is: PhoneService and the
I'm using ASP.NET MVC2 and I have the following object structure: public class IDealer
I'm using protobuf-net v2 and have a class that inherits List that I wan't
I'm using Protobuf-net. Suppose I have a list of Gizmo objects serialized and that
I have two websites, both using .Net framework 3.5. One website is hosting a
I have two ArrayLists of different lengths. One contains ints, the other contains objects
I have a situation where I'm serializing some .NET objects using NetDataContractSerializer and storing
I have a few .Net projects that would benefit from using a document/object database
sending mail along with embedded image using asp.net I have already used following but
using .NET 3.5. I have a function I want to make multithreaded calls to.

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.