CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 1999
    Location
    Belgium
    Posts
    440

    Question Ref to value type

    Is there any way to get a reference to a value type variable as was possible in C+++ with the & operator ?

    e.g.
    Code:
    private int [] m_MyIntArray=new int[1000];
    
    private void MyFunc(int _nMyParam1,int _nMyParam2,int _nMyParam3)
    {
      m_MyIntArray[_nMyParam1 * 10 - _nMyParam2  + _nMyParam3]=_nParam1 *5;
      if ((m_MyIntArray[_nMyParam1 * 10 - _nMyParam2  + _nMyParam3] > 100) &&
        (m_MyIntArray[_nMyParam1 * 10 - _nMyParam2  + _nMyParam3] < 200))
      {
         .. do something
      }
    }
    Instead of this, I'd like to store the indexed array element in some temporary reference to the element. This for not having to m_MyIntArray[_nMyParam1 * 10 - _nMyParam2 + _nMyParam3] all the time

  2. #2
    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    762

    Re: Ref to value type

    You can use pointers in C# just like you did in C++. You just have to check the option in your project to allow unsafe code.

    Then take a look at this: http://www.c-sharpcorner.com/UploadF...sInCSharp.aspx

  3. #3
    Join Date
    May 2007
    Posts
    1,546

    Re: Ref to value type

    Code:
    int index = _nMyParam1 * 10 - _nMyParam2  + _nMyParam3;
    
    private void MyFunc(int _nMyParam1,int _nMyParam2,int _nMyParam3)
    {
      m_MyIntArray[index]=_nParam1 *5;
    
      if ((m_MyIntArray[index] > 100) &&
        (m_MyIntArray[index] < 200))
      {
         .. do something
      }
    }
    I'd highly recommend you don't use pointers unless absolutely required. In this case, they're not.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured