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 1093007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:46:01+00:00 2026-05-16T23:46:01+00:00

I’m currently building a semi-complicated calculator which is basically a conversion from an Excel

  • 0

I’m currently building a semi-complicated calculator which is basically a conversion from an Excel spreadsheet I’ve been provided.

I’ve nailed most of it but there’s a part in the Excel spreadsheet where multiple calculations occur between 6 rows and 7 columns, but the issue is that the calculations happen in no particular order what-so-ever.

So for example, Row0[Column1] is calculated using (Row2[Column4] * Row2[Column5]) and Row1[Column4] is calculated using (Row4[Column2] / Row5[Column1]) and so forth.. you get the idea.

I’ve thought about using a 2D array, but am afraid that the values will calculate in a particular order, thus having no value when they are reached. As far as I’m aware, Row1 will be calculated first, then Row2, Row3, etc.

So, without creating a variable for each cell in my excel spreadsheet (and ordering it appropriately), is there a way I can calculate this using C#?

I would really appreciate any help, advice, pointers, whatever you think may be possible – I’d love to hear it!

EDIT After implementing the Lazy class provided by @dtb, I’ve got the following code. It’s a straight copy of what’s in the Excel spreadsheet I’ve been provided, including pointers & calculations.

var sr = new Lazy<decimal>[6, 6];
sr[0, 0] = new Lazy<decimal>(() => sr[1, 0].Value - eNumber);
sr[0, 3] = new Lazy<decimal>(() => sr[0, 4].Value - sr[1, 0].Value - sr[1, 4].Value);
sr[0, 4] = new Lazy<decimal>(() => sr[0, 0].Value * edD);
sr[0, 5] = new Lazy<decimal>(() => sr[0, 0].Value);

sr[1, 0] = new Lazy<decimal>(() => sr[1, 5].Value);
sr[1, 4] = new Lazy<decimal>(() => sr[1, 0].Value * edD);
sr[1, 5] = new Lazy<decimal>(() => sr[2, 0].Value + sr[2, 5].Value);

sr[2, 0] = new Lazy<decimal>(() => eNumber * rRate);
sr[2, 4] = new Lazy<decimal>(() => sr[2, 0].Value * hdD);
sr[2, 5] = new Lazy<decimal>(() => sr[1, 5].Value);

sr[3, 1] = new Lazy<decimal>(() => sr[2, 5].Value);

sr[4, 2] = new Lazy<decimal>(() => eNumber * (ePc / 100) + sr[2, 0].Value * (hlPc / 100) - sr[3, 1].Value);

sr[5, 0] = new Lazy<decimal>(() => (sr[0, 0].Value + sr[1, 0].Value + sr[2, 0].Value) / ePerR);
sr[5, 2] = new Lazy<decimal>(() => sr[5, 0].Value / rLifecycle);
sr[5, 4] = new Lazy<decimal>(() => sr[5, 2].Value);
sr[5, 5] = new Lazy<decimal>(() => sr[5, 0].Value + sr[5, 2].Value - sr[5, 4].Value);

However I get the following error
ValueFactory attempted to access the Value property of this instance.

Googling the error has returned a bunch of spammy search type websites.

Marko

  • 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-16T23:46:01+00:00Added an answer on May 16, 2026 at 11:46 pm

    Have a look at Lazy Evaluation:

    var table = new Lazy<int>[2, 2];
    
    table[0, 0] = new Lazy<int>(() => table[1, 1].Value * 2);
    table[0, 1] = new Lazy<int>(() => 42);
    table[1, 0] = new Lazy<int>(() => 100);
    table[1, 1] = new Lazy<int>(() => table[0, 1].Value + table[1, 0].Value);
    
    for (int i = 0; i < 2; i++)
    for (int j = 0; j < 2; j++)
    {
        Console.WriteLine("Row = {0}  Column = {1}  Value = {2}",
                                 i,            j,           table[i, j].Value);
    }
    

    Note how the content of the table cells are defined in arbitrary order. It figure out the order itself, as long as there are no circular dependencies between cells.

    Output:

    Row = 0  Column = 0  Value = 284
    Row = 0  Column = 1  Value = 42
    Row = 1  Column = 0  Value = 100
    Row = 1  Column = 1  Value = 142
    

    It becomes slightly more readable with LINQ-to-Lazy:

    var table = new Lazy<int>[2, 2];
    
    table[0, 0] = from t in table.AsLazy()
                  from x in t[1, 1]
                  select 2 * x;
    table[0, 1] = 42.AsLazy();
    table[1, 0] = 100.AsLazy();
    table[1, 1] = from t in table.AsLazy()
                  from a in t[0, 1]
                  from b in t[1, 0]
                  select a + b;
    

    using

    static class LazyExtensions
    {
        public static Lazy<TResult> SelectMany<TSource, TCollection, TResult>(this Lazy<TSource> source, Func<TSource, Lazy<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> resultSelector)
        {
            return new Lazy<TResult>(() => resultSelector(source.Value, collectionSelector(source.Value).Value));
        }
    
        public static Lazy<TSource> AsLazy<TSource>(this TSource value)
        {
            return new Lazy<TSource>(() => value);
        }
    }
    

    Custom replacement for .NET 4.0’s Lazy<T> Class:

    sealed class MyLazy<T>
    {
        private readonly Func<T> valueFactory;
        private T value;
        private bool valueCreated;
    
        public MyLazy(Func<T> valueFactory)
        {
            if (valueFactory == null)
            {
                throw new ArgumentNullException("valueFactory");
            }
            this.valueFactory = valueFactory;
        }
    
        public bool IsValueCreated
        {
            get { return this.valueCreated; }
        }
    
        public T Value
        {
            get
            {
                if (!this.valueCreated)
                {
                    this.value = this.valueFactory();
                    this.valueCreated = true;
                }
                return this.value;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.