help I'm drowning in something obvious
-
but I can't see it! I have a constructor
public nerd(int locNo) { location = locNo }
and a method/function that uses that constructorpublic nerd myMethod(int locNo, divNo) { nerd myNerd = new nerd(locNo); //but here I want to change the value of nerd's location property nerd.location = 9999; return nerd; }
But the returned nerd object from myMethod shows its location property still equal to the original locNo, not 9999. So I tried passing in a nerd object using the ref keyword, and got the same result. What is going on here? Yes, the constructor set the value of location, but then I change that value so I should now see the new value, not the old. If I do this using a ref like so://somewhere in the web page code behind I do this: nerd myNerd = new nerd(6666); public nerd myMethod(ref nerd refNerd, int locNo, int divNo) { nerd.location = locNo; } myMethod(ref nerd, 9999, 1);
I still get the old location number, not 9999. Am I crazy? -
but I can't see it! I have a constructor
public nerd(int locNo) { location = locNo }
and a method/function that uses that constructorpublic nerd myMethod(int locNo, divNo) { nerd myNerd = new nerd(locNo); //but here I want to change the value of nerd's location property nerd.location = 9999; return nerd; }
But the returned nerd object from myMethod shows its location property still equal to the original locNo, not 9999. So I tried passing in a nerd object using the ref keyword, and got the same result. What is going on here? Yes, the constructor set the value of location, but then I change that value so I should now see the new value, not the old. If I do this using a ref like so://somewhere in the web page code behind I do this: nerd myNerd = new nerd(6666); public nerd myMethod(ref nerd refNerd, int locNo, int divNo) { nerd.location = locNo; } myMethod(ref nerd, 9999, 1);
I still get the old location number, not 9999. Am I crazy?sharkfish wrote: public nerd myMethod(int locNo, divNo) { nerd myNerd = new nerd(locNo); //but here I want to change the value of nerd's location property nerd.location = 9999; return nerd; } This is obviously not your actual code, as it would fail to compile unless location is a static property. If it is, that's where your problem lies, if it's not, consider posting your actual code, so the actual error can be spotted and pointed out to you. Christian I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
-
sharkfish wrote: public nerd myMethod(int locNo, divNo) { nerd myNerd = new nerd(locNo); //but here I want to change the value of nerd's location property nerd.location = 9999; return nerd; } This is obviously not your actual code, as it would fail to compile unless location is a static property. If it is, that's where your problem lies, if it's not, consider posting your actual code, so the actual error can be spotted and pointed out to you. Christian I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
-
but I can't see it! I have a constructor
public nerd(int locNo) { location = locNo }
and a method/function that uses that constructorpublic nerd myMethod(int locNo, divNo) { nerd myNerd = new nerd(locNo); //but here I want to change the value of nerd's location property nerd.location = 9999; return nerd; }
But the returned nerd object from myMethod shows its location property still equal to the original locNo, not 9999. So I tried passing in a nerd object using the ref keyword, and got the same result. What is going on here? Yes, the constructor set the value of location, but then I change that value so I should now see the new value, not the old. If I do this using a ref like so://somewhere in the web page code behind I do this: nerd myNerd = new nerd(6666); public nerd myMethod(ref nerd refNerd, int locNo, int divNo) { nerd.location = locNo; } myMethod(ref nerd, 9999, 1);
I still get the old location number, not 9999. Am I crazy? -
but I can't see it! I have a constructor
public nerd(int locNo) { location = locNo }
and a method/function that uses that constructorpublic nerd myMethod(int locNo, divNo) { nerd myNerd = new nerd(locNo); //but here I want to change the value of nerd's location property nerd.location = 9999; return nerd; }
But the returned nerd object from myMethod shows its location property still equal to the original locNo, not 9999. So I tried passing in a nerd object using the ref keyword, and got the same result. What is going on here? Yes, the constructor set the value of location, but then I change that value so I should now see the new value, not the old. If I do this using a ref like so://somewhere in the web page code behind I do this: nerd myNerd = new nerd(6666); public nerd myMethod(ref nerd refNerd, int locNo, int divNo) { nerd.location = locNo; } myMethod(ref nerd, 9999, 1);
I still get the old location number, not 9999. Am I crazy?Hmm I'm not sure if this would be right or not (since I'm kind of new to C# myself :)) but in your first listing of code shouldn't you use the object and change the location value wrt that object? I mean rather than doing: nerd.location = 9999; // your code shouldn't you do: myNerd.location = 9999; ? // where myNerd is your object And as for your constructor I think it should be like: public nerd(int locNo) { this.location = locNo; // using 'this' } where the 'this' would point to your current object thereby changing that particular object's location... Good luck as such...