Problems passing object array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tcomer

    Problems passing object array

    This is what I'm trying to do:

    class MyClass
    {
    private MyObject [] object;

    public MyClass()
    {
    object = new MyObject[10];
    MyFunction(ref object);
    ....

    protected MyFunction(ref MyObject [] object)
    {
    object[0].myVar= "value";
    ....

    this is giving me a NullReferenceEx ception on the statement in
    MyFunction. The constructor of the object initializes all variables to
    a default value.

  • Jon Skeet [C# MVP]

    #2
    Re: Problems passing object array

    tcomer <tcomer@gmail.c omwrote:
    This is what I'm trying to do:
    >
    class MyClass
    {
    private MyObject [] object;
    >
    public MyClass()
    {
    object = new MyObject[10];
    MyFunction(ref object);
    ...
    >
    protected MyFunction(ref MyObject [] object)
    {
    object[0].myVar= "value";
    ...
    >
    this is giving me a NullReferenceEx ception on the statement in
    MyFunction. The constructor of the object initializes all variables to
    a default value.
    Creating an array of a class type doesn't create any instances of that
    type - it creates an "empty" array with as many "slots" as you've asked
    for, with the value in each "slot" being null to start with. It sounds
    like you want to do:

    for (int i=0; i < object.Length; i++)
    {
    object[i] = new MyObject();
    }

    before your method call.

    (By the way, I assume you're not *really* using "object" as a variable
    name... I really hope not!)

    --
    Jon Skeet - <skeet@pobox.co m>
    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
    If replying to the group, please do not mail me too

    Comment

    • Dave Sexton

      #3
      Re: Problems passing object array

      Hi Jon,
      (By the way, I assume you're not *really* using "object" as a variable
      name... I really hope not!)
      It wouldn't even compile unless it's escaped with the literal token "@"
      anyway:

      protected MyFunction(ref MyObject[] @object) { ... }

      But I agree, that's ridiculous :)

      --
      Dave Sexton

      "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
      news:MPG.1fb706 64fa038b9298d5c f@msnews.micros oft.com...
      tcomer <tcomer@gmail.c omwrote:
      >This is what I'm trying to do:
      >>
      >class MyClass
      >{
      > private MyObject [] object;
      >>
      >public MyClass()
      >{
      > object = new MyObject[10];
      > MyFunction(ref object);
      >...
      >>
      >protected MyFunction(ref MyObject [] object)
      >{
      > object[0].myVar= "value";
      >...
      >>
      >this is giving me a NullReferenceEx ception on the statement in
      >MyFunction. The constructor of the object initializes all variables to
      >a default value.
      >
      Creating an array of a class type doesn't create any instances of that
      type - it creates an "empty" array with as many "slots" as you've asked
      for, with the value in each "slot" being null to start with. It sounds
      like you want to do:
      >
      for (int i=0; i < object.Length; i++)
      {
      object[i] = new MyObject();
      }
      >
      before your method call.
      >
      (By the way, I assume you're not *really* using "object" as a variable
      name... I really hope not!)
      >
      --
      Jon Skeet - <skeet@pobox.co m>
      http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
      If replying to the group, please do not mail me too

      Comment

      • tcomer

        #4
        Re: Problems passing object array

        No, that was just the name that I used in the example.. I know better
        ;)

        That solved my problem, thanks a lot! I've moved from C++ to C# and I'm
        still trying to get used to the differences between the two. Much
        appreciated!

        Comment

        Working...