Pass 2D array to dif. class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kevinyy
    New Member
    • Jul 2008
    • 77

    Pass 2D array to dif. class

    How can i pass a 2d array to another class?
    This is a little of what i have at the moment:
    Code:
    class Ocl
    {
    string[,] blacklist;
    public void test()
    {//blacklist array has data(thats not the problem)
    NewConnection pxy = new NewConnection(socket, blacklist);
    }
        class NewConnection
        {
            string[,] blist;
            public NewConnection(string[,] blacklist)//constructor
            {
                blist = blacklist;
            }
    }
    the problem is when i pass the array, and then try to use later on in NewConnection, it throws the exception: "Object reference not set to an instance of an object."
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Have you considered the scope of the class that the 2D array was created in?

    If the instance of the class that creates the array goes out of scope it's deconstructor is called. Typically when this happens all variables, including the 2D array you have defined, will be deleted/cleaned up. This would mean that the 2D array would also be deleted from the class that is using it (because when passing arrays you are actually just passing a pointer to a memory location)

    When you retrieve the 2D array in the parent/calling class make a copy of it and see if that solves the problem.

    -Frinny

    Comment

    • mldisibio
      Recognized Expert New Member
      • Sep 2008
      • 191

      #3
      The way you have your code now, NewConnection is a private nested class. Was that your intention? Or did you just leave out a brace from class ocl?

      Comment

      • mldisibio
        Recognized Expert New Member
        • Sep 2008
        • 191

        #4
        Also, you are passing in a socket and an array to the NewConnection constructor, but there the constructor only accepts one parameter...per haps that is what your error is referring to?

        Frinavale: I may be wrong, but I believe that the Framework GarbageCollecto r will not clean up any objects if there is still a reference to them. So in this case, even if Ocl goes out of scope, the NewConnection.b list (if it were a field) would still hold a valid reference to the array itself.

        If Ocl went out of scope, then Ocl.blacklist (if it were a field) would return a null reference exception.

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          Originally posted by mldisibio
          The way you have your code now, NewConnection is a private nested class. Was that your intention? Or did you just leave out a brace from class ocl?
          I didn't even notice that!
          I guess I should pay more attention to the {}'s in the future!

          Hmm, so it isn't a scope problem because if the original class was destroyed it so would the private one within it.

          When do you get the Null Reference Exception?

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Originally posted by mldisibio
            Also, you are passing in a socket and an array to the NewConnection constructor, but there the constructor only accepts one parameter...per haps that is what your error is referring to?
            I think you're onto something here...

            Originally posted by mldisibio
            Frinavale: I may be wrong, but I believe that the Framework GarbageCollecto r will not clean up any objects if there is still a reference to them. So in this case, even if Ocl goes out of scope, the NewConnection.b list (if it were a field) would still hold a valid reference to the array itself.
            I'm not sure if the GarbageCollecto r can keep track of what is still referenced...I' m looking into this now.


            Originally posted by mldisibio

            If Ocl went out of scope, then Ocl.blacklist (if it were a field) would return a null reference exception.
            That's why I asked when they were getting the null reference exception...bec ause you don't encounter these exceptions until you try to use something that isn't there.....in the above posted code this is not happening.

            -Frinny

            Comment

            • Kevinyy
              New Member
              • Jul 2008
              • 77

              #7
              Originally posted by mldisibio
              The way you have your code now, NewConnection is a private nested class. Was that your intention? Or did you just leave out a brace from class ocl?
              i left out a brace..haha. ok will still lost in what you guys are suggesting, i know what you mean by the GC not disposing of ref'd things, but as in going out of scope, meaning moving from A to B, A is going out of scope?
              Basically im just wondering what is the proper way in passing a 2D array between classes? i know how to pass a singular array to another class...but 2D's are giving me problems :(
              Thanks for your guys' fast replys

              Comment

              • mldisibio
                Recognized Expert New Member
                • Sep 2008
                • 191

                #8
                This seems to work. Compare your code and let us know:
                Code:
                using System;
                namespace bytes {
                
                  class Program {
                    static void Main() {
                      Ocl myOcl = new Ocl();
                      myOcl.test();
                    }
                  }
                
                  class Ocl {
                
                    string[,] blacklist;
                
                    public void test() {
                      blacklist = new string[3, 2];
                      int start = 65;
                      for (int i = blacklist.GetLowerBound(0); i <= blacklist.GetUpperBound(0); i++) {
                        for (int j = blacklist.GetLowerBound(1); j <= blacklist.GetUpperBound(1); j++) {
                          Console.WriteLine("{0},{1} = {2}", i, j, (char)start);
                          blacklist[i, j] = ((char)start++).ToString();
                        }
                      }
                      Console.WriteLine("BlackList[2,0] in Ocl: {0}", blacklist[2, 0]);
                      NewConnection pxy = new NewConnection(blacklist);
                    }
                  }
                
                
                  class NewConnection {
                    string[,] blist;
                    public NewConnection(string[,] blacklist) {
                      blist = blacklist;
                      Console.WriteLine("BlackList[2,0] in NewConnection: {0}", blist[2, 0]);
                    }
                  }
                
                }

                Comment

                • Kevinyy
                  New Member
                  • Jul 2008
                  • 77

                  #9
                  Awesome! works perfectly:)
                  ty!

                  Comment

                  • Frinavale
                    Recognized Expert Expert
                    • Oct 2006
                    • 9749

                    #10
                    Originally posted by mldisibioI
                    may be wrong, but I believe that the Framework GarbageCollecto r will not clean up any objects if there is still a reference to them.
                    You're right.

                    The GC (garbage collector) tracks and releases memory allocations for you during runtime. When you remove all references to a memory allocation it'll clean up the memory.

                    However, it does not do this for resources. It'll remove objects in memory that are still referencing resources (like database handlers or connections...t his is why you should implement the IDisposable interface when your objects use resources). I think that's where I got confused...that , and it's been a long time since I've thought about it.

                    -Frinny

                    Comment

                    • Plater
                      Recognized Expert Expert
                      • Apr 2007
                      • 7872

                      #11
                      In the original piece of code...the blacklist array was never populated before sending?

                      Comment

                      • mldisibio
                        Recognized Expert New Member
                        • Sep 2008
                        • 191

                        #12
                        Frinavale:
                        Ya' know, for all I've read about Garbage Collection and implementing IDisposable, your two sentences above still made me see it in a new light. Thanks!

                        Comment

                        • Kevinyy
                          New Member
                          • Jul 2008
                          • 77

                          #13
                          Originally posted by Plater
                          In the original piece of code...the blacklist array was never populated before sending?
                          it was, i had just simplified the example code for ease of reading.

                          Comment

                          Working...