tags:

views:

42

answers:

3

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  A: 

Instantiate the list in the Service object

private List<ChartAccount> chartAccounts = new List<ChartAccount>;

Charlie Brown
When insert the code: List<ChartAccount> chartAccounts = new List<ChartAccount>; I got the following error: "Error 17 A new expression requires (), [], or {} after type"
dewacorp.alliances
Shoudl be `new List<ChartAccount>();`
Nader Shirazie
I am using .NET 2.0 BTW.
dewacorp.alliances
@Nader Good catch, I typed that off the cuff too fast
Charlie Brown
A: 

On the second line of your client sample you check for null:

if (service.ChartAccounts == null)

but then within the block following that null check you do:

service.ChartAccounts[i].Name = allocationItem.Value2;

This is logically inconsistent. If service. ChartAccounts is null then you cannot possibly use and indexer to query for a value. i think you meant to do this:

if (service.ChartAccounts != null)

i'm guessing that your code inside the if block will not be executed because your service.ChartAccounts object is indeed null. You need to trace back up your call stack and make sure you instantiated and populated the collection properly. That, and get yourself a copy of ReSharper immediately.

Paul Sasik
+1  A: 

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.

Nader Shirazie