How can i pass a 2d array to another class?
This is a little of what i have at the moment: -
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."
12 1740
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
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?
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.
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?
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
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
This seems to work. Compare your code and let us know: -
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]);
-
}
-
}
-
-
}
-
Awesome! works perfectly:)
ty!
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
In the original piece of code...the blacklist array was never populated before sending?
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!
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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:
...
|
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...
|
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....
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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,...
| |