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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:51:11+00:00 2026-06-12T14:51:11+00:00

I have an array of classes (Voxel) in a class. I use the following

  • 0

I have an array of classes (Voxel) in a class. I use the following methods to add to and remove from the array. A memento pattern is used to store the action for each method so it can be undone/redone at any point.

    public void AddVoxel(int x, int y, int z)
    {
        int index = z * width * height + y * width + x;

        frames[currentFrame].Voxels[index] = new Voxel();

        // Undo/Redo history
        undoRedoHistories[currentFrame].Do(new AddMemento(index));
    }

    public void RemoveVoxel(int x, int y, int z)
    {
        int index = z * width * height + y * width + x;

        // Undo/Redo history
        undoRedoHistories[currentFrame].Do(new RemoveMemento(index, frames[currentFrame].Voxels[index]));

        frames[currentFrame].Voxels[index] = null;      // Does not update 'voxelSelected' reference
    }

In a separate class I wish to have a reference to a particular voxel in the array of voxels held by the above class.

private Voxel voxelSelected = null;

As a reference type I would like this value to automatically know when the part of the array it “points” to holds a voxel or is null. This is important when using an undo command because a voxel can be removed from the array and become null, or vice versa.

To get a voxel from the array I use the following method.

public Voxel GetVoxel(int x, int y, int z)
{
    return frames[currentFrame].Voxels[z * width * height + y * width + x];
}

I then set the reference to the voxel as follows.

public void SetVoxelSelected(ref Voxel voxel)
{
    voxelSelected = voxel;
}

    voxelMeshEditor.AddVoxel(0, 0, 0);

    var voxel = voxelMeshEditor.GetVoxel(0, 0, 0);  // Copies rather than references?

    SetVoxelSelected(ref voxel);

    Console.WriteLine(voxelSelected == null); // False

    voxelMeshEditor.RemoveVoxel(0, 0, 0);

    Console.WriteLine(voxelSelected == null); // False (Incorrect intended behaviour)

How can I correctly reference a voxel in the array so the voxelSelected value automatically updates when the array updates.

  • 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-06-12T14:51:12+00:00Added an answer on June 12, 2026 at 2:51 pm

    How can I correctly reference a voxel in the array so the voxelSelected value automatically updates when the array updates.

    Don’t store a Voxel, but store the coordinates you need to get the voxel.

    Instead of

    Voxel voxelSelected;
    public void SetVoxelSelected(ref Voxel voxel) { voxelSelected = voxel; }
    

    have

    int selectedX, selectedY, selectedZ
    public void SetVoxelSelected (int x, int y, int z) { selectedX = x; ... }
    public Voxel GetVoxelSelected() { return voxelMeshEditor.GetVoxel(x, y, z) }
    

    giving

    SetVoxelSelected(0, 0, 0);                     //Set selected
    Console.WriteLine(GetVoxelSelected() == null); // False, CORRECT !
    voxelMeshEditor.RemoveVoxel(0, 0, 0);          //REMOVE
    Console.WriteLine(GetVoxelSelected() == null); // True
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a static array of classes similar to the following: public class Entry
I have an array of classes of this type: public class IndexVO { public
I have following classes: class Department { private string departmentId; private string departmentName; private
I have an array which I store different classes which again hold different arrays.
I have an array of classes with a property Date , i.e.: class Record
I have an array of objects which all derive from the class BaseStudent. BaseStudent**studentlist
I currently have an array of type Class which holds only sub-classes of type
I have the following classes in my c++ program: game_entity game_creature which inherits from
So i have 2 classes, users and health readings. i made a array of
I write perl classes, but I don't know how to have a array or

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.