I'm sure this is really simple, but I can't figure it out.
I'm working on a project using Visual Studio 8 and I created a new C# class using a separate C# file. I want to create an instance of that class using a different file, but I can't figure out how import it.
Loading
Posted Jul 9, 2008, 11:45 AM
namespace People
{
public class Person
{
// Class members declared here
}
}
namespace Main
{
// Namespace members here
}
if so, you will need to either include a "using People;" statement before the namespace, or using the fully qualified class name including the namespace name, like this:
// including the People namespace
using People;
namespace Main
{
public class MyClass
{
Person person = new Person();
}
}
// OR, using the fully qualified name
// Note that NO "using" statement has been declared
namespace Main
{
public class MyClass
{
People.Person person = new People.Person();
}
}
Another question is, are these files in the same project? Or are they in two different projects? If theyre in two different projects, you will need to add a reference to the project you are attempted to use members from.
Right click the project you want to use the members in.
Select "Add Reference..."
Then, based on where your project is (if its in the same solution, or if its in a separate library or .dll file) you would find it, and add a reference to it and use it as we stated about in the namespaces part.
You may also want to right click your Solution and click "Rebuild All". This will relink and rebuild all projects in the solution so if you made any changes to external libraries it will relink them and you will have access to your new objects.
Hope this helps :)
ChrisPosted Jul 9, 2008, 8:10 AM
namespace.Person myPerson = new Person();
but it did work when I went to Project --> Add Class in Visual Studio, even though everything I had typed was exactly what was in the Visual Studio class outline. It must do something behind the scenes to link them together.
Thanks.
stevePosted Jul 9, 2008, 5:44 AM
Have you tried going to the main menu in Visual 2008 and looking under the "Project" tab? Try to either "Add Class" or "Add Existing Item" or "Add Reference", maybe Visual 2008 will do the work for you.
Ali ZaidiPosted Jul 9, 2008, 2:43 AM
write like
namespaceName.className ob=new namespaceName.className();
ob.MethodORvariable;
ChrisPosted Jul 8, 2008, 11:05 PM
Person myPerson = new Person();
it says that it doesn't recognize the name "Person". I get this error:
"The type or namespace name 'Person' could not be found (are you missing a using directive or an assembly reference?)"
Do I have to do something to 'link' the two classes or package them together somehow? The files are in the same directory. Do I need to do something with namespace? The Form1 class is enclosed in a namespace?
Ryan AlfordPosted Jul 8, 2008, 10:11 PM
if so, here is how you would instantiate a class from a seperate file:
// This is in a seperate file
public class Person
{
public string Name;
}
// This is in the Form's .cs file
public partial class Form1 : Form
{
private void Form1 _Load(object sender, EventArgs e)
{
Person myPerson = new Person();
myPerson.Name = "John Doe";
MessageBox.Show(myPerson.Name);
}
}