2

What I'm trying to achieve is say i have an array, i want to be able to modify a specific array element throughout my code, by pointing at it.

for example in C++ i can do this

int main(){
 int arr [5]= {1,2,3,4,5};
 int *c = &arr[3];
 cout << arr[3] <<endl;
 *c = 0;
 cout << arr[3]<<endl;
}

I did some googling and there seems to be a way to do it through 'unsafe', but i don't really want to go that route.

I guess i could create a variable to store the indexes, but I'm actually dealing with slightly more complexity (a list within a list. so having two index variables seems to add complexity to the code.)

C# has a databinding class, so what I'm currently doing is binding the array element to a textbox (that i have hidden) and modifying that textbox whenever i want to modify the specific array element, but that's also not a good solution (since i have a textbox that's not being used for its intended purpose - a bit misleading).

4
  • Any of the following types may be a pointer type: sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, or bool Any enum type. Any pointer type. Any user-defined struct type that contains fields of unmanaged types only. Commented Dec 17, 2012 at 17:06
  • you couldnt create a var to store an index, but in C you would store a pointer in a variable and would be happy with it? Sounds strange, doesnt it? Commented Dec 17, 2012 at 17:06
  • @igrimpe i dunno, a pointer to me tells you specifically what it's for. If i had private int columnIndexOfElement; and private int rowIndexOfElement; it seems to be a bit weird Commented Dec 17, 2012 at 17:10
  • why not update the data at particular index? Commented Dec 17, 2012 at 17:11

4 Answers 4

2

A C# example of how you would like the use to look would help. If I understand what you're asking, a simple class like this might do it. What you're asking for though, doesn't seem like a very good idea. If you showed the larger scope in which you need this, someone might be able to point out a better design where you didn't need this sort of functionality at all.

public class ListElement<T> {
    private IList<T> list;
    private int index;
    public ListElement(IList<T> list, int index) {
        this.list = list;
        this.index = index;
    }
    public T Value {
        get {
            return list[index];
        }
        set {
            list[index] = value;
        }
    }
}

a use of this would look like

int[] arr = new int[] {1,2,3,4,5};
ListElement<int> third = new ListElement<int>(arr, 2);
Console.WriteLine(third.Value);
third.Value = 0;
Console.WriteLine(third.Value);
Sign up to request clarification or add additional context in comments.

1 Comment

You can use IList<T> throughout (with no other changes), and then it will work with either a List or an Array.
0

i'm not sure if this fits exactly, but the problem is that these pointers are not possible in c#.

if you have more complicated lists, you can take a look at LinkedList<T> it provides a performant way if you want to change elements within a list.

4 Comments

Pretty much everything about linked lists aren't performant for anything but the smallest data sets. The asymptotic complexity of certain types of operations are low, but due to the fact that each item is in a separate location on the stack, the loss of memory locality greatly decreases performance for simple tasks such as iterating the sequence.
@Servy you are right, but as the question asked about a list of lists, an alternative data structure could simplify that problem. instead of adding an item to a list within a list, it could be more performant to use a single linked list ... it really depends on the specific application
@user287107 The question doesn't refer to a list of lists at all. It just refers to a single list.
@Servy let me quote: "but I'm actually dealing with slightly more complexity (a list within a list"
0

I came up with a somewhat solution in C#. Granted this is off the cuff, so it may not work in all situations but I did test it briefly on your situation.

class Wrapper<T>
{
    private T[] array;
    private T item;
    private int index;
    public T Item { get { return item; } set { item = value;
        array[Index] = value;
    } }

    public int Index
    {
        get { return index; }
        set
        {
            index = value;
            Item = array[value];

        }    
    }

    public Wrapper(T[] arr)
    {
        array = arr;
    }

}

You can then use the class like this:

    class Program
{
    static void Main(string[] args)
    {
        int[] i = {1, 2, 3, 4, 5};
        i.ToList().ForEach(x => Console.WriteLine(x));
        Wrapper<int> w = new Wrapper<int>(i);
        w.Index = 2;
        w.Item = 5;


        i.ToList().ForEach(x => Console.WriteLine(x));
        Console.ReadLine();
    }
}

This will give the output: 1234512545

It isn't as pretty as the solution in C++ but it will work as you want and provides a more "automatic" version of referencing the array.

1 Comment

There's no reason for the wrapper to keep it's own copy of the value. In fact it shouldn't because it won't see updates to the underlying array. See hatchet's answer.
0

I would wrap your arrays in Objects. In C#, stuff that needs pointer manipulation is usually best done with objects.

The advantage is that objects allow clearer naming and access to more complex data structures. You are right, it is not ideal to pass around sets of indices - the ordering and indexing is easily jumbled.. In fact, I think it was people in your position who decided Object-oriented programming would be a good idea!!

So you have class MyArray { }, and can use the 'object reference' as you would a pointer, plus you can create arrays of MyArray[].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.