Reference or pointer

EzBlue

New member
Joined
Dec 8, 2016
Messages
2
Programming Experience
Beginner
I want a singleton Problem with a "square" 2x2.
I want to be able to refer to the case by row.
I want to be able to refer to the row by case.
I know I could easily do this in C++ with pointers but it seems like a bad habit to do.
I don't understand how to link my "row" and my "case" together.
The same logic will be there for column but isn't describe in the code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Debugging
{
    class Program
    {
        static void Main(string[] args)
        {
            Problem.Instance().Show();
            Problem.Instance().Change();
            Problem.Instance().Show();
        }


        public class Problem
        {
            private Case[] cases = null;
            private Row[] rows = null;
            // same logic with private Column[] columns = null;


            static Problem instance = null;
            private Problem()
            {
                cases = new Case[4];
                rows = new Row[2];


                int i = 0;
                for (i = 0; i < 4; i++)
                    cases[i] = new Case();


                for (i = 0; i < 2; i++)
                    rows[i] = new Row(i);


            }
            public static Problem Instance()
            {
                if (instance == null)
                    instance = new Problem();
                return instance;
            }
            public Case LinkToRow(int i, Row r)
            {
                cases[i].LinkToRow(r);
                return cases[i];
            }
            public void Show()
            {
                rows[0].Show();
            }
            public void Change()
            {
                cases[0].Change();
                cases[1].Change();
            }
        }
        public class Row
        {
            private Case[] cases = null;
            public Row(int i)
            {
                cases = new Case[2];
                cases[0] = Problem.Instance().LinkToRow(0, this);
                cases[1] = Problem.Instance().LinkToRow(1, this);
            }
            public void Show()
            {
                Console.WriteLine("{0},{1}", cases[0].Val, cases[1].Val);
            }
        }
        public class Case
        {
            private int val;
            private Row r = null;


            public Case()
            {
            }
            public void LinkToRow(Row rr)
            {
                r = rr;
            }
            public int Val { get { return val; } }
            public void Change()
            {
                val++;
            }
        }
    }
}
 
Last edited by a moderator:
For future reference, please use formatting tags when posting code snippets. I have done it for you on this occasion and I think you can see how much more readable the code is.

As for the problem, are you saying that you want to be able to do this sort of thing:
var myCase = myProblem.Rows(0).Cases(0);
var myRow = myProblem.Cases(1).Rows(1);
i.e.
 
Yes, that is what I want to do.
I want to be able to change the value of the "Case" and then the value would affect the "Row". I don't want a copy of the object the moment I do the "linking". But at the moment I am propably missing more than one thing.
 
If you're using classes then you're already not getting a copy of the object. I think that you may be trying to solve a problem that doesn't exist.

Classes are called reference types because you only ever hold a reference to the object. Structures, on the other hand, are called value types because you hold the value itself. Structure variables contain the object itself so assigning a structure instance from one variable to another creates a copy of the object. Class variables contain a reference to the object so assigning a class instance from one variable to another creates a copy of the reference, not the object, so both variables will then refer to the same object. The idea of references is that you can basically treat them as though they were the object in code but, under the hood, they behave like pointers.
 
Back
Top Bottom