2016-08-21 19 views
0

私はこのコード行持っている:問題があるC#のファイルの別の.CSからToolStripMenuItemコントロールへのアクセスが

The name 'checkForUpdatesToolStripMenuItem' does not exist in the current context

、どのように実行します。ただし

checkForUpdatesToolStripMenuItem.Enabled = true; 

を、それは私にエラーを与えます'checkForUpdatesToolStripMenuItem'はフォームの一部ですが、現在作業中の.CSファイルからアイテム(checkForUpdatesToolStripMenuItem)にアクセスしますか?

ありがとうございました。

答えて

0

は、あなたは他のクラスに値を渡すことができます

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void Send_ToolStrip_Data_To_Instance_Class() 
    { 
     My_Other_CS_File other_File = new My_Other_CS_File(); 
     other_File.Act_On_ToolStrip_Item(checkForUpdatesToolStripMenuItem.Enabled); 
    } 
} 
public class My_Other_CS_File 
{ 
    public void Act_On_ToolStrip_Item(bool enabled) 
    { 
     //do something 
    } 
} 
+0

を作成されるまで、他のインスタンスのクラスが存在しません私はあなたがこれらのクラスを使用する方法を理解していません。私は古い学校のCプログラマなので、これを理解するのは変です。これらの授業はどこに入れますか?私のForm.CSコード(最高レベル/メイン)、またはCSファイルで、私は "checkForUpdatesToolStripMenuItem"を動作させようとしています。 – t0rxe

+0

クラスは1つのファイルでも、別々のファイルでも構いません。インスタンスクラスは、作成者としか通信できない独立したオブジェクトのように考えることができます。フォームクラス内からインスタンスクラス(csファイル)を作成する場合は、フォームクラスから何かを渡す必要があります。代わりに、すべてのインスタンスクラスに見える静的クラスを作成することができます(public static class myClass {})実際のコードが1つのファイルにあるのか他のファイルには影響しないのか –

+0

名前空間には、多くの場合、C#に関する限り、コードは同じ名前空間の下にある限り、コードの場所は関係ありません。インスタンスクラス(新しいClassName()で作成されたクラス)は、作成者のみが知ることができます。すべてのクラスに。 –

0
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"; 
} 

}

+0

シンプルなやり方では、クラスは昔の関数に似ています。繰り返しコードが必要な場合や、「機能」を整理する必要がある場合は、クラスに入れて、いつでも呼び出すことができます。しかし、2Dの「関数」とは異なり、クラスにはさまざまな変数やメソッドがあります。それらはミニプログラムのようなもので、あらゆる種類のクールなものを継承して実行できます。しかし、基本的には、それらはハイパーファンクションのようなものです。 –

関連する問題