Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Two Classes, One Namespace

Status
Not open for further replies.

blairacuda

Technical User
Sep 3, 2009
51
US
OK, this is driving me crazy. I know it has to be something simple.

I have two classes in the same namespace. I can't access the methods in one class from the other class. But I can create the object...

Here is an example:

--First.cs--
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Simple
{
class First
{
Second s = new Second();
}
}

--Second.cs--
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Simple
{
public class Second
{
string name = "Chris";

public Second();

public void displayName()
{
MessageBox.Show(name);
}
}
}

I can create the object but not call displayName(). It isn't available... Where in the world am I messing up?

Thanks in advance,
Chris

Chris Blair
Crystal, InstallShield, branching out in other programming realms.
 
i believe it's the ctor of class Second that's causing the problem. I haven't seen the notation
Code:
class Second
{
  public Second();
}
before and could be the problem. either delete the ctor (since it's the default) or change it to
Code:
class Second
{
  public Second()
  {
  }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
No go. I tried both suggestions (re-write and delete) and neither one worked for me.

Thank you though

Chris Blair
Crystal, InstallShield, branching out in other programming realms.
 
I'm assuming all your references are good?

It's all about scope. Where are you trying to call s.DisplayName()? You can't do this in the body of the 'First' class, you have to do it inside a method. i.e.,
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Simple
{
	public class First
	{
		Second _second = new Second();

		public First()
		{
			this._second.displayName();
		}
	}
}

namespace Simple
{
    public class Second
    {
        string _name = "Chris";

        public Second();

        public void DisplayName()
        {
            MessageBox.Show(this._name);
        }
    }
}

You may also want to look up the scoping of private member variables, properties etc. as well as some patterns and practices in order to separate logic and display.

Rhys

"The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it"
Terry Pratchett
 
Just to point out one error in Rhys666 example and im sure its only a mistype, but
Code:
this._second.displayName();
should be
Code:
this._second.DisplayName();
I would be so stingy if c# wasn't :)
 
No worries, I had tried to tidy up some of the casing from the original and just missed one. Good spot :)

Rhys

"The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it"
Terry Pratchett
 
Hey guys, thanks. I'm gonna have to do some more learnin' with scopes but you gave me a great direction to go in.

By the by, Rhyss, love the quote from Pratchett.

Chris Blair
Crystal, InstallShield, branching out in other programming realms.
 
As a beginning, you might want to simplify the problem by pulling both class files together into one file, and making sure they work that way. Then you can experiement with your original idea, which is to put multiple class files in the same namespace. I don't believe that's where you have the problem; I believe the problem as has been said is that you haven't called the method properly. Divide and conquer...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top