1

基本的に、オブジェクト指向プログラミングの紹介として、WriteLine の内容を WinForm の Label ボックスに移動しようとしています。構文エラーがあると思いますが、書き込み行があるメソッドが無効であることはわかっています。したがって、これを機能させるための助けをいただければ幸いです。これは私が行った試みの 1 つにすぎません。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ConsoleHelloWorld.Program;


namespace WindowsFormHelloWorld
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            string words = new ConsoleHelloWorld.Program.Main(words);
            label1.Text = words;

        }
    }
}

これは私が参照しているコードです。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleHelloWorld
{
    public static class Program
    {
        public static void Main(string[] args)
        {
            string words = "hello world";
            Console.WriteLine(words);
            Console.ReadLine();
        }
    }
}
4

2 に答える 2

0

変数wordsローカル変数です。つまり、そのスコープは、宣言されているメソッド、つまりMain. そのメソッドの外では参照できません。

変数にアクセスできるようにするには、変数を public フィールド (またはクラスのプロパティ) にする必要があります。そのクラス型のインスタンスを持たずにアクセスする必要がある場合は、このフィールドを宣言する必要がありますstatic。次に、コードは次のようになります。

public static class Program
{
    public readonly static string Words = "hello world";
    //...   
}

Windows アプリケーション プロジェクトが、Windows アプリケーション フォーム内のコンソール アプリケーション プロジェクトへの参照を持っていると仮定すると、次のように記述できます。

string words = ConsoleHelloWorld.Program.Words;

また、コンソール アプリケーションを起動する必要はありません。さらに、このように実行することもできません (コンソール アプリケーションと Windows アプリケーションの両方を起動する場合は、Cuong Le の回答を検討してください)。

于 2013-01-31T08:04:02.403 に答える
0

動作するようになりました。間違ってルートダウンしていたことが判明しました。(ルート化は Android のファイル構造に少し似ていると思っていました。) 基本的に、Hello World 変数を保持する別のクラスを作成し、それを呼び出して、文字列をコンソールまたは WinForm アプリに出力するかどうかを指定しました。

コードの最初のブロック:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleHelloWorld
{


    public static class Program
    {
        public static class Hello
        {
            public static string words = "Hello World";
        }
       public static void Main(string[] args)
       {
           string word = ConsoleHelloWorld.Program.Hello.words;

           Console.WriteLine(word);
           Console.ReadLine();

       }
   }
}

クラスを呼び出す WinForm:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ConsoleHelloWorld; //Program.Hello;


namespace WindowsFormHelloWorld
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            string word = ConsoleHelloWorld.Program.Hello.words;
            label1.Text = word;

        }
    }
}

将来のプロジェクトでは、独自のプロジェクト内のさまざまな環境でコードを実行し続けるだけです。

于 2013-02-04T16:06:37.950 に答える