Click here to Skip to main content
14,843,450 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Have the following structure with two arrays. It compiles OK but unable to access the array structures. Working in VC10. New to C#.


public struct Budget
{
public string budgetItem;
public double budget;
public double actual;
public double[] budgetAmount;
public string[] budgetNote;
}
const int ARRAY_LENGTH = 30;

Budget[] Budget_DB = new Budget[ARRAY_LENGTH];

What I have tried:

Have searched the web on this subject have not found anything helpful.
Posted
Updated 3-Dec-20 10:08am
Comments
Member 8605996 3-Dec-20 16:16pm
   
in my solution 3 I have a working version but it is not very efferent. How do I change solution 3 so that the budgetAmount and budgetNote are independent for each Budget[] index?

Here, a crude example of how you can do it:
c#
using System;
					
public class Program
{
	public static void Main()
	{
		Console.WriteLine("Hello World");
		
		const int ARRAY_LENGTH = 30;
		Budget[] Budget_DB = new Budget[ARRAY_LENGTH];
		
		var bAmount = new double[2];
		bAmount[0] = 1.0;
		bAmount[1] = 2.0;
		
		var bNote = new string[2];
		bNote[0] = "One";
		bNote[1] = "Two";
		
		Budget_DB[0] = new Budget(){
			budgetItem = "Test",
			budget = 100.0,
			actual = 200.0,
			budgetAmount = bAmount,
			budgetNote = bNote
		};
		
		Console.WriteLine(Budget_DB[0].budget);
		Console.WriteLine(Budget_DB[0].budgetAmount[0]);
		Console.WriteLine(Budget_DB[0].budgetNote[0]);
	}
	
	public struct Budget
	{
		public string budgetItem;
		public double budget;
		public double actual;
		public double[] budgetAmount;
		public string[] budgetNote;
	}
}

Output's
Hello World
100
1
One
   
You declared an array of Budget objects but have not assigned any objects to the array. You need to create a Budget object, populate it and then assign that to one of the members of the array and do this ARRAY_LENGTH times
   
I first set up my code to implement budgetAmount and budgetNote using single dimensional array. They issue is the budgetAmount and budgetNote values are seen in every Budget[] index. Would like it to just be 35 independent budgetAmount and budgetNote for each Budget[] index.

Id get the desired effect by creating 2 dimensional arrays, shown below, but now I carry they with every Budget[] index.

How can this be done using single dimensional arrays that are independent?


public struct Budget
{
public string budgetItem;
public double budget;
public double actual;
public double[,] budgetAmount;
public string[,] budgetNote;
}
const int ARRAY_LENGTH = 30;
Budget[] Budget_DB = new Budget[ARRAY_LENGTH]; /* Create unfilled array */


namespace Budget
{
public partial class Form1 : Form
{
public struct Budget
{
public string budgetItem;
public double budget;
public double actual;
public double[,] budgetAmount;
public string[,] budgetNote;
}
const int ARRAY_LENGTH = 30;
Budget[] Budget_DB = new Budget[ARRAY_LENGTH]; /* Create unfilled array */


public Form1()
{
InitializeComponent();

var bAmount = new double[30,35];
for (uint i = 0; bAmount.GetLength(0) < 30; i++)
{
for (int j = 0; bAmount.GetLength(1) < 35; j++)
{
bAmount[i, j] = 0.0;
}
}
//bAmount[0] = 1.0;
//bAmount[1] = 2.0;

/* Create space for budgetNote */
var bNote = new string[30,35];
for (uint i = 0; bNote.GetLength(0) < 30; i++)
{
for (int j = 0; bNote.GetLength(1) < 30; j++)
bNote[i,j] = "";
}
//bNote[0] = "One";
//bNote[1] = "Two";

/* Now create the data base */
for (uint index = 0; index <= 29; index++)
{
Budget_DB[index] = new Budget()
{
budgetItem = "",
budget = 0.0,
actual = 0.0,
budgetAmount = bAmount,
budgetNote = bNote
};
}
}
}
   
Comments
Richard Deeming 4-Dec-20 4:34am
   
How is this supposed to be a "solution" to the question?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900