namespace SO_Question_win
{ // EVERYTHIGを同じ名前空間の下だけでなく、はるかにC#が懸念している販売用ファイルにあるかもしれません。私たちは、便宜のために異なるファイルを使用します。 パブリック部分クラスForm1:フォーム { //これはプロジェクトのメインフォームのフォームクラスです。これは、プログラムの開始時にスレッドが行く場所です。彼らは
public Form1()
{
InitializeComponent();
}
private void hello()
{
//I can only call hello from inside this class.
My_Other_CS_File other_File = new My_Other_CS_File();
//you created this instance class here. It will only exist until "hello" finishes running, then it will disappear.
string hello = "hi";
//the only way to get the string hello to the class you created is to pass it.
other_File.SayHello(hello);
//other_file is done. It will disappear now if you want it again you will have to create a new instance
}
}
public class My_Other_CS_File
{
public void SayHello(string hi)
{
//here, the string Hi is passed from the form class
Console.WriteLine(hi);
//even though this class was created by the form class, it has access to any public static classes.
Console.WriteLine(Global.helloString);
}
}
public static class Global
{
//anything marked "public static" here will be visible to any class under the same namespace. This class is niether created nor destroyed - it always exists. hat's the difference bewtween static and instance.
public static string helloString = "howdy";
}
}
を作成されるまで、他のインスタンスのクラスが存在しません私はあなたがこれらのクラスを使用する方法を理解していません。私は古い学校のCプログラマなので、これを理解するのは変です。これらの授業はどこに入れますか?私のForm.CSコード(最高レベル/メイン)、またはCSファイルで、私は "checkForUpdatesToolStripMenuItem"を動作させようとしています。 – t0rxe
クラスは1つのファイルでも、別々のファイルでも構いません。インスタンスクラスは、作成者としか通信できない独立したオブジェクトのように考えることができます。フォームクラス内からインスタンスクラス(csファイル)を作成する場合は、フォームクラスから何かを渡す必要があります。代わりに、すべてのインスタンスクラスに見える静的クラスを作成することができます(public static class myClass {})実際のコードが1つのファイルにあるのか他のファイルには影響しないのか –
名前空間には、多くの場合、C#に関する限り、コードは同じ名前空間の下にある限り、コードの場所は関係ありません。インスタンスクラス(新しいClassName()で作成されたクラス)は、作成者のみが知ることができます。すべてのクラスに。 –