2

I have created a generic type to act as a pointer so that I can pass by reference. (Perhaps there is a much more simple way of doing this but I want to stress that I am doing this to learn more about generics and passing by reference, not the most efficient way of completing the task, if that makes sense.)

Here is the code I wrote for the generic type

class GenericPointer<T> {
    public T item;
    public void setItem(T i){ item = i; }
    public T getItem(){ return item; }
}

In my program I have created an instance of this type called 'intPointer'. The value 143 is arbitrary.

GenericPointer<int> intPointer = new GenericPointer<int>();
intPointer.setItem(143);
Console.WriteLine(intPointer.getItem());

The above code runs properly, setting and returning the value 143.

I now want to pass this 'intPointer' to a method that increments it and then prints the value again.

So I wrote a method called addone()

public void addone(int i) { i ++; }

Now I want to make the following calls (remembering that I already set the value to 143):

Console.WriteLine(intPointer.getItem());
addone(intPointer);
Console.WriteLine(intPointer.getItem());

What I was expecting to see was 143 then 144 however I get the following errors:

The best overloaded method match for 'Notes.Program.addone(int)' has some invalid arguments

and:

cannot convert from 'Notes.GenericPointer<int>' to 'int'

Any help would be greatly appreciated!

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
Andrew
  • 397
  • 1
  • 3
  • 13
  • 1
    This question is too broad...a proper answer would require a complete explanation of a good chunk of C#... – MiMo Nov 06 '12 at 00:29
  • Why not simply use the ref keyword for this? http://msdn.microsoft.com/en-us/library/14akc2c7.aspx And also quite a good answer here: http://stackoverflow.com/a/3539299/578843 . – Styxxy Nov 06 '12 at 00:48
  • @MiMo I think i have give a pretty specific explanation of my issue here. This site is here for the purpose of learning and solving problems. No one is forcing you to read or be condescending. I don't need someone to hold my hand, just a push in the right direction. Fortunately there are others willing to help. If you don't have anything productive to say then do everyone else a favor and keep your comments to yourself. – Andrew Nov 06 '12 at 01:10

3 Answers3

6

I'll begin by correcting some of your terminology: you're not using pointers. C# does support pointers, but using the unsafe keyword, and they are real pointers (as in, integer memory addresses you can directly manipulate). The code you written is just an example of a boxed object.

.NET supports boxing already, by casting to Object; however it isn't recommended nor needed because the ref keyword solves the problem you're trying to "fix".

Use the ref keyword to describe a value-type parameter that should be passed by-reference instead of by-value. All other semantics remain the same, like so:

void Foo() {
    int x = 123;
    Bar(ref x);
    Console.Write( x ); // prints "124".
}
void Bar(ref int x) {
    x++;
}

I have a few other notes:

  1. C# and .NET conventions dictate that all public members (methods, properties, fields, etc) should have TitleCase, not camelCase (i.e. ensure the first letter is capitalised).
  2. Trivial getter and setter methods are discouraged, used Properties instead (though I note you cannot use ref arguments with properties).
  3. You're getting your error because the type of intPointer is not int, but your class GenericPointer<int>.
Dai
  • 141,631
  • 28
  • 261
  • 374
  • 1
    I believe `ref` needs to be used in the call to Bar, as so: `Bar(ref x);`. – YotaXP Nov 06 '12 at 00:28
  • @Dai first, thanks for the clarification on the terminology, I am still getting the error. except now its saying it cant convert to 'ref int' did i misunderstand? so i made to following changes: 'addone(ref intPointer);' and 'public void addone(ref int i)' '{' 'i ++;' '}' – Andrew Nov 06 '12 at 00:47
  • obviously i havent figured out how to put code in a comment.. ill figure it out one of these days – Andrew Nov 06 '12 at 00:53
  • I'm saying you should get rid of your `GenericPointer` class completely and just use `ref int` instead. – Dai Nov 06 '12 at 01:14
  • @Dai I see. Thanks for the help! I got it to work using your suggestion. – Andrew Nov 06 '12 at 01:16
1

While GenericPointer is wrapping an int, it is not actually an int so it cannot be treated as one. It has properties that are an int.

Imagine if GenericPointer wrapped a string. What would AddOne do to that.

You can act on the properties of the class but not treat the entire class as its generic type.

It would be possible to write an AddOne method that took a Generic Pointer argument and then inspected it for intyness and then added one to the internal item if it was an int. I am sure that is not a good idea.

What are you really trying to achieve with this GenericPointer?

Brody
  • 2,074
  • 1
  • 18
  • 23
  • honestly I am just experimenting with c# as i learn the language. I understand what you're saying about the problem with it being a generic and attempting to perform addition. what i would like to be able to do is pass the value and have the program react the same way it would if i attempted to pass a string to a method that was going to attempt to increment that string. since that is not possible obviously i would get an error. is this even possible? or am i reaching here? – Andrew Nov 06 '12 at 00:57
0

If you want parameters to be reference if they are a value type (string, int, bool, etc.) then make your parameter like this:

public void addone(ref int i)
{
    i++;
}

Then call the method like so:

addone(ref variableInt);

You can also look at this in order to see how to make your classes work as a specific type.

TyCobb
  • 8,909
  • 1
  • 33
  • 53