Results 1 to 4 of 4

Thread: convert from 'ref double[]' to 'ref object'

Threaded View

  1. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: convert from 'ref double[]' to 'ref object'

    The problem is that you are passing the array by reference as a more general type. If it was by value it wouldn't be an issue but you can't change the type like that with a parameter passed by reference. The problem is because within the method the argument is an object reference so you could assign any type of object to it. It's supposed to be a double[] when the method completes though, so that is obviously a problem. Take a look at this example:
    Code:
    private void Form1_Load(object sender, EventArgs e)
    {
        Double[] arr = new Double[10];
    
        // The parameter would go in as a Double[]...
        this.PassByRef(ref arr);
    }
    
    private void PassByRef(ref object arr)
    {
        // ...and be changed to a string here.  That can't be allowed to happen.
        arr = "Hello World";
    }
    The compiler will not let you do that as it cannot guarantee type safety. You can change the type of a parameter passed by reference to a more specific type with a cast, but not to a less specific type.
    Last edited by jmcilhinney; Apr 7th, 2006 at 05:50 AM. Reason: Added "with a cast" to last sentence.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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