作爲c#的新手,我不明白變量是如何在對象之間傳遞的。當我執行這個程序時,我的數組變量「filePaths」會返回null。這是一個基本的窗體。我正在製作一個能夠顯示文字並播放聲音的程序。C#訪問跨不同對象的變量
特定的錯誤是「的NullReferenceException是未處理。
這裏是我的特殊代碼。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Media;
namespace Kindersect
{
public partial class form1 : Form
{
string[] filePaths;
string directpath = "C:\\Users\\Optimus Prime\\Documents\\vocabaudio\\";
int counter = 0;
int c = 0;
public form1()
{
InitializeComponent();
}
public void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
string[] filePaths = Directory.GetFiles(directpath, "*.wav");
foreach(string k in filePaths)
{
c++;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if (counter < c)
{
label1.Text = filePaths[counter];
SoundPlayer simpleSound = new SoundPlayer(filePaths[counter]);
simpleSound.Play();
counter++;
}
}
}
}
在此先感謝。
正在使用您的計算機擎天柱嗎?那是個很酷的男人。 – DarthVader
感謝大家解釋我所擁有的聲明問題以及我所遇到的角色轉義問題。我的新鮮感背叛了我。 –
你叫'noob' – DarthVader