Accessing classes

This is a discussion on Accessing classes within the C# Programming forums, part of the General Programming Boards category; I have a problem in C# that I have faced a few times before, but I have always managed to ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    98

    Accessing classes

    I have a problem in C# that I have faced a few times before, but I have always managed to work around it. Basically, I need to, from one class, access another class that has not been created in the current class. Both classes are in the same namespace.

    Example:

    Code:
    //Form1.cs
    ClassTwo testClass;
    
    public void Initialize()
    {
    	testClass = new ClassTwo();
    	testClass.doTest();
    }
    
    public void aTest(string text)
    {
    	this.Text=text;
    }
    //End of Form1.cs
    
    
    //ClassTwo.cs
    
    public void doTest()
    {
    	Form1.aTest("This is a test");
    }
    
    //End of ClassTwo.cs

    This is basically what I want to do. But it won't allow me to access the method(s) in Form1. How can I do this without creating a second instance of Form1?

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    You can create a static method in Form1. Info on static classes and members.

  3. #3
    Registered User AloneInTheDark's Avatar
    Join Date
    Feb 2008
    Posts
    74
    You can pass the class to the other class :

    Code:
    public class SomeClass() { 
    public const test = "Hello world!";
    }
    
    public void doTest(SomeClass SC){
    try{
    Console.Write( SC.test );
    }
    catch(Exception e){
    Console.Write( e.ToString() ); // SC is null or something.
    }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help accessing classes and derived classes
    By hobbes67 in forum C++ Programming
    Replies: 8
    Last Post: 07-14-2005, 02:46 PM
  2. accessing classes and reversing inputs
    By 2fastwrx in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2004, 08:16 AM
  3. 2 Classes Accessing Eachother
    By Chronom1 in forum C++ Programming
    Replies: 2
    Last Post: 10-11-2003, 05:10 PM
  4. Replies: 8
    Last Post: 07-27-2003, 01:52 PM
  5. Accessing Classes in Classes
    By GrNxxDaY in forum C++ Programming
    Replies: 18
    Last Post: 07-30-2002, 01:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21