4

I'm pretty new to C# and coding, so forgive my ignorance. I've tried researching and it seems like what I'm trying to find is possibly something called a "pointer" in C++, but not available in C#? I want a field or property which I can assign an object from a list as a "selected object" and play around with until I "release" it. I've tried this:

    Entity GrabbedEntity;

    List<Entity> Entities;

    if (mouseDown)
        GrabbedEntity = Entities[3];

    if (mouseUp)
        GrabbedEntity = null;

What I'm trying to achieve is basically just releasing the object I assigned to "grabbedEntity", and not actually making "Entities[3]" a null value, I still want to be able to modify the values of grabbedEntity though(mouseDown and =Entities[3] is for simplicity sake, it could be any object in list depending on circumstances). How I've been been working around it is this:

List<Entity> Entities;

List<Entity> _grabbedEntityList = new List<Entity>();
Entity GrabbedEntity
{
    get 
    { 
        if (_grabbedEntityList.Count < 1) return null; 
        else return _grabbedEntityList[0]; 
    }
    set
    {
        if (value == null) _grabbedEntityList.Clear(); 
        else 
            _grabbedEntityList.Clear(); 
            _grabbedEntityList.Add(value);
        }
    }
}

This works exactly as I intend it to, I can Assign "GrabbedEntity" to an object in my "Entities" list, while it's assigned I can do whatever I want to it, and then if I want to release it I just set it to null, but I feel like I'm making a work around for something that actually already exists. Thanks for any responses.

vicdotexe
  • 51
  • 4
  • Are you saying that you just want to have a selected item in a list and while it is selected essentially make it read-only or invisible to readers of the list? – mbrdev Sep 28 '16 at 14:54
  • is it possible for your list to ever have more than one element? – Eric Lippert Sep 28 '16 at 14:54
  • 2
    Why does your first approach not work for you? – poke Sep 28 '16 at 14:55
  • 1
    Please use braces....It's horrible to read without it. – Steve Sep 28 '16 at 14:55
  • This isn't germane to your question, but I wanted to point out a way you should refactor your `GrabbedEntity` setter, just from a general edification point of view. Note that in both the `if` and the `else` you are calling `_grabbedEntityList.Clear();`; thus, you can extract it from your `if-else` block statement entirely (i.e. call it above the `if`). That leaves `if (value != null) _grabbedEntityList.Add(value);` – rory.ap Sep 28 '16 at 15:01
  • I would read up on c# vs c++ references and pointers: http://stackoverflow.com/questions/5919855/c-references-vs-c-sharp-references https://blogs.msdn.microsoft.com/ericlippert/2009/02/17/references-are-not-addresses/ http://www.albahari.com/valuevsreftypes.aspx – RIanGillis Sep 28 '16 at 15:02
  • Sorry guys, apparently I was wrong and my first approach works, I must have been doing something differently before. It works after trying it again. – vicdotexe Sep 28 '16 at 15:09

1 Answers1

4

Your first approach should work just fine.

You say "How I've been been working around it is this:" but you failed to tell us what it is that you feel you need to work around. If you are concerned that GrabbedEntity = null; might set Entities[3]; to null, well, it won't, and if you have actually tried this, you should know that it doesn't.

As for the second approach, the one with the get{} and set{}, I have no idea what you are trying to do, nor how this convoluted mess is meant to accomplish it.

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
  • 2
    Thanks for being blunt, pushed me to go back and look further. I was able to figure out what was wrong. Something was causing my entity to disappear upon releasing the mouse, and I assumed it was the nullification, It ended up being I selected the object incorrectly in the first place. – vicdotexe Sep 28 '16 at 15:18
  • C-:= I am glad you did not take it personally. – Mike Nakis Sep 28 '16 at 15:30