Skip to main content
gamedev.net
🔒 Locked

C# multiple files

Started by Khaos Apr 17, 2004 at 6:25 PM 7 replies 500+ views
Original Post
Khaos
Khaos
What is the process for combining your .cs files? I have MyClass.cs and MainForm.cs. MainForm calls an instance of the class MyClass defined in MyClass.cs. They are both, right now, in the MyFormProject namespace; that is, I put "namespace MyFormProject" at the beginning of each file and included all the code under it. Do I need to do that? Both files are in the combine. I get a "missing namespace or assembly listing" error. I''m confused. Secondly, if I have a class named MyClass, does it HAVE to be in MyClass.cs? What''s the rule on that, and naming conventions for C# in general. Any help? Thank you much.
Oluseyi
Oluseyi
quote:
Original post by Khaos
They are both, right now, in the MyFormProject namespace; that is, I put "namespace MyFormProject" at the beginning of each file and included all the code under it.
That's not quite clear. Do you merely declare the namespace (namespace MyFormProject;) or do you define the class within the namespace (namespace MyFormProject { <class definition> })?

quote:
Secondly, if I have a class named MyClass, does it HAVE to be in MyClass.cs?
No. Nor do you have to have only one class per .cs file.

quote:
What's the rule on that, and naming conventions for C# in general.
There might be something in the C# FAQ

[Edit: Typo.]

[edited by - Oluseyi on April 17, 2004 8:44:55 PM]
Khaos
Khaos
quote:
Original post by Oluseyi
That''s not quite clear. Do you merely declare the namespace (namespace MyFormProject;) or do you define the class within the namespace (namespace MyFormProject { <class definition> })?


Thank you for your very helpful replies, first off. I very much like that second link.

Anyways, I did this in my MyClass.cs file:

namespace MyFormProject
{
class MyClass
{
public void MyFunction()
{ ... }
}
}

My MainForm.cs file has the code the compiler set up for me when I made my form, for the most part. It is also, in the same way, by default though, in the MyFormProject namespace. I copied that idea into my MyClass.cs file exactly, hoping that would solve my problem. It didn''t. Same missing namespace or assembly listing error.

In MainForm.cs I do this:

MyClass bob = new MyClass();
...
bob.MyFunction();

And there is where I get the error. I''ve tried messing around with namespaces quite a lot. I''ve looked through my books as well. Using statements? I don''t think so, but I''ll ask.

I''m sure someone here has used more than one .cs file in a project. How do you do it?

Thanks again.
Oluseyi
Oluseyi
Your code seems fine, and you say both files are in the combine (using SharpDevelop?)

Could you just post the full error message, along with the portion of MyForm.cs where you reference MyClass? Scope and typos are the only things that come to mind at this point.
yaroslavd
yaroslavd
Put this:


MyClass bob = new MyClass();
...
bob.MyFunction();


in the MyFormProject, too. Then, MyClass will be able to access other classes in its namespace.
Oluseyi
Oluseyi
Your code should look something like this:

MyForm.cs

using Forms = System.Windows.Forms;

namespace MyFormsProject {
class MyForm : Forms.Form {
// member declarations


public MyForm() {
InitializeComponent();
// any other initialization

}

void InitializeComponent() {
// initialize form

}

[STAThread]
public static void Main(string[] args) {
Application.Run(new MyForm());
}
}
}

MyClass.cs
 
namespace MyFormProject {
class MyClass {
public MyClass() {
// _ctor

}

public void SomeFunction() {
// blah

}
}
}

The statements
MyClass bob = new MyClass();
...
bob.SomeFunction();
need to occur within a method in MyForm.

[Edit: Formatting.]

[edited by - Oluseyi on April 17, 2004 12:15:41 AM]
yaroslavd
yaroslavd
quote:

using Forms = System.Windows.Forms;



You can do that?
Oluseyi
Oluseyi
Oh yeah!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.