December 1st, 2008 01:57 AM
Passing object to another class in C#
I'm having a problem getting an object I have to pass to another class. I'm trying to have it pass the address to the new class, so I can still use the first instance of the object in the new class. I'm having troubles though, and can't seem to get it to compile. If you want to look, I've uploaded the whole project, I'm working in Sharp Develop.
in case you don't, heres an example of what i'm trying to do.
I have a class called opt in which I've created an object called o.
I have a second class called options that I want to pass the object into when I create an options object. The compiler complains about the managed code
so I used unsafe. This still didn't work, so I tried creating a pointer before I passed it to the class which still didn't work.
Cannot take the address of, get the size of, or declare a pointer to a managed type ('cell_server.opt') (CS0208)
I can't really see what I'm doing wrong, so any help would be greatly appreciated.
opt *point = &o;
options OptionsWindow = new options(point);
December 6th, 2008 05:18 PM
Would you be able to make a new class that inherits the original or are you trying to pass the data?
December 6th, 2008 08:05 PM
If you are trying to do what I think you're trying, forget using * and &. In C# objects are always passed by reference rather than by value. So:
Passes the instance of o into the MyClass constructor.
object o = new object();
MyClass myClass = new MyClass(o);
December 9th, 2008 12:56 AM
I ended up searching for a long time, and finding this:
and it seems to be working fine. Are there any problems with using this?
options OptionsWindow = new options(ref o)
public options(ref opt op)
By sphanlon in forum Spyware / Adware
Last Post: April 1st, 2013, 08:05 PM
By cheyenne1212 in forum Miscellaneous Security Discussions
Last Post: February 1st, 2012, 01:51 PM
By jerichoholic in forum Spyware / Adware
Last Post: November 30th, 2004, 10:14 AM
By coolcamel in forum Spyware / Adware
Last Post: September 29th, 2004, 02:54 PM
By E5C4P3 in forum Miscellaneous Security Discussions
Last Post: March 7th, 2002, 06:35 AM