Results 1 to 4 of 4

Thread: Passing object to another class in C#

  1. #1
    Senior Member
    Join Date
    Apr 2005
    Location
    USA
    Posts
    422

    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
    Cannot take the address of, get the size of, or declare a pointer to a managed type ('cell_server.opt') (CS0208)
    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.
    Code:
                opt *point = &o;
                options OptionsWindow = new options(point);
    I can't really see what I'm doing wrong, so any help would be greatly appreciated.
    Attached Files Attached Files

  2. #2
    Senior Member
    Join Date
    Oct 2003
    Location
    MA
    Posts
    1,052
    Would you be able to make a new class that inherits the original or are you trying to pass the data?

  3. #3
    Custom User
    Join Date
    Oct 2001
    Posts
    503
    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:

    Code:
    object o = new object();
    
    MyClass myClass = new MyClass(o);
    Passes the instance of o into the MyClass constructor.

    ac

  4. #4
    Senior Member
    Join Date
    Apr 2005
    Location
    USA
    Posts
    422
    I ended up searching for a long time, and finding this:
    Code:
    options OptionsWindow = new options(ref o)
    
    
    public options(ref opt op)
    {...
    and it seems to be working fine. Are there any problems with using this?

Similar Threads

  1. cmd.exe boxes flashing around screen on startup
    By sphanlon in forum Spyware / Adware
    Replies: 11
    Last Post: April 1st, 2013, 08:05 PM
  2. Windows Error Messages
    By cheyenne1212 in forum Miscellaneous Security Discussions
    Replies: 7
    Last Post: February 1st, 2012, 02:51 PM
  3. Should I be worried....?
    By jerichoholic in forum Spyware / Adware
    Replies: 12
    Last Post: November 30th, 2004, 11:14 AM
  4. HJTlog and the ISP ads
    By coolcamel in forum Spyware / Adware
    Replies: 8
    Last Post: September 29th, 2004, 02:54 PM
  5. Information Leakage from Optical Emanations
    By E5C4P3 in forum Miscellaneous Security Discussions
    Replies: 5
    Last Post: March 7th, 2002, 07:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •