reference to int

This is a discussion on reference to int within the C# Programming forums, part of the General Programming Boards category; Lets say I have an int in one class. I want an int to another class that "points" to the ...

  1. #1
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853

    reference to int

    Lets say I have an int in one class. I want an int to another class that "points" to the int of the first class. In C++ I would use a pointer. In C# I should use a reference? Like:
    Code:
    class A
    {
        public int a;
    }
    
    class B
    {
       public Int32 b = new Int32();
    }
    
    A a = new A();
    B b = new B();
    a.a = 1;
    b.b = a.a;
    a.a = 12;
    // is b.b no 12 or 0?
    Or is just Int32 another way to say int?
    Will this work? I suspect it wont...
    What is the best way to do it if the above is wrong?

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Lets say I have an int in one class. I want an int to another class that "points" to the int of the first class.
    While there are possible workarounds, you should consider using a better design in the first place. A pointer to another instance's member is not exactly a good thing.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Why not a good thing? I can thing of a few possible examples that would make it the best way. Lets say for example you have a class that monitors in a way other classes. And you want to be able to read a member from them of type of int. That and only that. The rest of the class is just a wrapper to add more functionality. You could of course have references to the objects and read from there the int, but why do that when you can have a reference to the int directly?
    I have already made a workaround

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by C_ntua View Post
    Why not a good thing? I can thing of a few possible examples that would make it the best way. Lets say for example you have a class that monitors in a way other classes. And you want to be able to read a member from them of type of int. That and only that. The rest of the class is just a wrapper to add more functionality. You could of course have references to the objects and read from there the int, but why do that when you can have a reference to the int directly?
    I have already made a workaround
    Then you should have (excuse C++ style):
    Code:
    class monitorbase
    {
    public:
        virtual int GetValue() = 0;
    };
    
    class tempmonitor: public monitorbase
    {
    private:
        int value;
    public:
        int GetValue() { return value; }
    }
    And then have a reference to a monitorbase, and call GetValue() to read the monitor.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    what you want to do is impossible with C#, if you're talking about primitive types like int, uint, long, etc.

    with these types, the only 'reference' stuff you can do is with the 'ref' keyword for methods.

    if using proper class design is not an option, you could wrap an int with a class (similar to how java does it)
    Code:
    class Integer {
      public int Value;
    }
    then store an instance of Integer and point the instance of the new class to the instance of the old class.

  6. #6
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Quote Originally Posted by C_ntua View Post
    Why not a good thing? I can thing of a few possible examples that would make it the best way. Lets say for example you have a class that monitors in a way other classes. And you want to be able to read a member from them of type of int. That and only that. The rest of the class is just a wrapper to add more functionality. You could of course have references to the objects and read from there the int, but why do that when you can have a reference to the int directly?
    I have already made a workaround
    Because it's never smart to let strangers touch your privates.

    If you have a class that monitors objects of a second class, it should do so by a reference to the object and the object's public interface.

    You don't want to couple classes tightly. That's bad design. Changing the implementation of one class would necessitate changing the implementation of the other.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  7. #7
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Hmm, yeah I get what you mean. Thanx

  8. #8
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Quote Originally Posted by C_ntua View Post
    Why not a good thing? I can thing of a few possible examples that would make it the best way. Lets say for example you have a class that monitors in a way other classes. And you want to be able to read a member from them of type of int. That and only that. The rest of the class is just a wrapper to add more functionality. You could of course have references to the objects and read from there the int, but why do that when you can have a reference to the int directly?
    I have already made a workaround

    In this case why not just create an event that notifies other interested classes (if any) that something has changed, whatever you may want that to be.

  9. #9
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    That's another workaround. I ll consider which is best for my situation

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 09:36 PM
  2. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-24-2007, 12:05 AM
  3. Converted from Dev-C++ 4 to Dev-C++ 5
    By Wraithan in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2005, 07:45 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 12:58 PM
  5. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 07:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21