473,399 Members | 3,302 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Pass 2D array to dif. class

Kevinyy
77
How can i pass a 2d array to another class?
This is a little of what i have at the moment:
Expand|Select|Wrap|Line Numbers
  1. class Ocl
  2. {
  3. string[,] blacklist;
  4. public void test()
  5. {//blacklist array has data(thats not the problem)
  6. NewConnection pxy = new NewConnection(socket, blacklist);
  7. }
  8.     class NewConnection
  9.     {
  10.         string[,] blist;
  11.         public NewConnection(string[,] blacklist)//constructor
  12.         {
  13.             blist = blacklist;
  14.         }
  15. }
  16.  
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."
Sep 25 '08 #1
12 1740
Frinavale
9,735 Expert Mod 8TB
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
Sep 25 '08 #2
mldisibio
190 Expert 100+
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?
Sep 25 '08 #3
mldisibio
190 Expert 100+
Also, you are passing in a socket and an array to the NewConnection constructor, but there the constructor only accepts one parameter...perhaps that is what your error is referring to?

Frinavale: I may be wrong, but I believe that the Framework GarbageCollector 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.blist (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.
Sep 25 '08 #4
Frinavale
9,735 Expert Mod 8TB
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?
Sep 25 '08 #5
Frinavale
9,735 Expert Mod 8TB
Also, you are passing in a socket and an array to the NewConnection constructor, but there the constructor only accepts one parameter...perhaps that is what your error is referring to?
I think you're onto something here...

Frinavale: I may be wrong, but I believe that the Framework GarbageCollector 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.blist (if it were a field) would still hold a valid reference to the array itself.
I'm not sure if the GarbageCollector can keep track of what is still referenced...I'm looking into this now.



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...because 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
Sep 25 '08 #6
Kevinyy
77
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
Sep 25 '08 #7
mldisibio
190 Expert 100+
This seems to work. Compare your code and let us know:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. namespace bytes {
  3.  
  4.   class Program {
  5.     static void Main() {
  6.       Ocl myOcl = new Ocl();
  7.       myOcl.test();
  8.     }
  9.   }
  10.  
  11.   class Ocl {
  12.  
  13.     string[,] blacklist;
  14.  
  15.     public void test() {
  16.       blacklist = new string[3, 2];
  17.       int start = 65;
  18.       for (int i = blacklist.GetLowerBound(0); i <= blacklist.GetUpperBound(0); i++) {
  19.         for (int j = blacklist.GetLowerBound(1); j <= blacklist.GetUpperBound(1); j++) {
  20.           Console.WriteLine("{0},{1} = {2}", i, j, (char)start);
  21.           blacklist[i, j] = ((char)start++).ToString();
  22.         }
  23.       }
  24.       Console.WriteLine("BlackList[2,0] in Ocl: {0}", blacklist[2, 0]);
  25.       NewConnection pxy = new NewConnection(blacklist);
  26.     }
  27.   }
  28.  
  29.  
  30.   class NewConnection {
  31.     string[,] blist;
  32.     public NewConnection(string[,] blacklist) {
  33.       blist = blacklist;
  34.       Console.WriteLine("BlackList[2,0] in NewConnection: {0}", blist[2, 0]);
  35.     }
  36.   }
  37.  
  38. }
  39.  
Sep 25 '08 #8
Kevinyy
77
Awesome! works perfectly:)
ty!
Sep 26 '08 #9
Frinavale
9,735 Expert Mod 8TB
may be wrong, but I believe that the Framework GarbageCollector 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...this 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
Sep 26 '08 #10
Plater
7,872 Expert 4TB
In the original piece of code...the blacklist array was never populated before sending?
Sep 26 '08 #11
mldisibio
190 Expert 100+
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!
Sep 26 '08 #12
Kevinyy
77
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.
Sep 26 '08 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Kenny | last post by:
Hello, can anyone tell me how to pass an array to a function ? I have this function , part of my class. It works if I do not put in int a everywhere , but obviously , I need to add an array so I...
41
by: Berk Birand | last post by:
Hi, I am just learning about the array/pointer duality in C/C++. I couldn't help wondering, is there a way to pass an array by value? It seems like the only way to do is to pass it by...
1
by: Mark Dicken | last post by:
Hi All I have found the following Microsoft Technet 'Q' Article :- Q210368 -ACC2000: How to Pass an Array as an Argument to a Procedure (I've also copied and pasted the whole contents into...
16
by: Ekim | last post by:
hello, I'm allocating a byte-Array in C# with byte byteArray = new byte; Now I want to pass this byte-Array to a managed C++-function by reference, so that I'm able to change the content of the...
7
by: liyang3 | last post by:
Hi, I have Class A, B and C. Class A has an instance of B. Class B has an instance of C. In the instance of C, it generates some data that need to be passed back to Class A. But Class C...
1
by: Williams.sam | last post by:
I am wondering if there is a way (maybe via Marshalling ) to pass an array of Objects of some C# defined class to VB. I am already launching a C# form from VB with simple call backs using a basic...
3
by: Brett | last post by:
I have several classes that create arrays of data and have certain properties. Call them A thru D classes, which means there are four. I can call certain methods in each class and get back an...
4
by: _Mario.lat | last post by:
Hallo, I have a little question: In the function session_set_save_handler I can pass the name of function which deal with session. In Xoops code I see the use of this function like that: ...
24
by: =?Utf-8?B?U3dhcHB5?= | last post by:
Can anyone suggest me to pass more parameters other than two parameter for events like the following? Event: Onbutton_click(object sender, EventArgs e)" Event handler: button.Click += new...
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.