regular
regular

Reputation: 25

Pointing to array element

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).

Upvotes: 2

Views: 815

Answers (4)

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);

Upvotes: 2

Sanjay Manohar
Sanjay Manohar

Reputation: 7026

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[].

Upvotes: 0

Jetti
Jetti

Reputation: 2458

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.

Upvotes: 0

user287107
user287107

Reputation: 9417

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.

Upvotes: 0

Related Questions