Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. help I'm drowning in something obvious

help I'm drowning in something obvious

Scheduled Pinned Locked Moved C#
questionhelp
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sharkfish
    wrote on last edited by
    #1

    but I can't see it! I have a constructor public nerd(int locNo) { location = locNo } and a method/function that uses that constructor 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; } 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?

    C L F 3 Replies Last reply
    0
    • S sharkfish

      but I can't see it! I have a constructor public nerd(int locNo) { location = locNo } and a method/function that uses that constructor 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; } 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?

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      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

      S 1 Reply Last reply
      0
      • C Christian Graus

        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

        S Offline
        S Offline
        sharkfish
        wrote on last edited by
        #3

        Thanks for your reply. I found my error. It was something unrelated to the issue I posted. Apologies for wasting your time.

        1 Reply Last reply
        0
        • S sharkfish

          but I can't see it! I have a constructor public nerd(int locNo) { location = locNo } and a method/function that uses that constructor 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; } 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?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          I think you should specify this in your myMethod: myNerd.location = 9999; return myNerd; instead of nerd.location = 9999; return nerd; (which will not work unless it is a static class)

          1 Reply Last reply
          0
          • S sharkfish

            but I can't see it! I have a constructor public nerd(int locNo) { location = locNo } and a method/function that uses that constructor 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; } 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?

            F Offline
            F Offline
            Fang Illusion
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups