🔒 Locked
C# multiple files
Advertisement
Original Post
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.
quote: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> })?
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.
quote:No. Nor do you have to have only one class per .cs file.
Secondly, if I have a class named MyClass, does it HAVE to be in MyClass.cs?
quote:There might be something in the C# FAQ
What's the rule on that, and naming conventions for C# in general.
[Edit: Typo.]
[edited by - Oluseyi on April 17, 2004 8:44:55 PM]
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.
Your code should look something like this:
MyForm.cs
MyClass.cs
The statements
[Edit: Formatting.]
[edited by - Oluseyi on April 17, 2004 12:15:41 AM]
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();need to occur within a method in MyForm.
...
bob.SomeFunction();
[Edit: Formatting.]
[edited by - Oluseyi on April 17, 2004 12:15:41 AM]
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement