2012-07-04 6 views
0

C#에 익숙하지 않아 개체간에 변수가 전달되는 방식을 이해할 수 없습니다. 이 프로그램을 실행할 때 배열 변수 "filePaths"가 null로 돌아옵니다. 기본 Windows 양식입니다. 나는 단어를 보여주고 소리를 낼 프로그램을 만들고있다.다른 개체간에 변수에 액세스하는 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++; 
     } 

    } 
} 

} 사전에

감사합니다. 참조 할 때

+1

옵티머스 프라임이 너무 컴퓨터를 사용하고 있습니다 : 그래서

...

string directpath = "C:\\Users\\Optimus Prime\\Documents\\vocabaudio\\"; 

string directpath = @"C:\Users\Optimus Prime\Documents\vocabaudio\"; 

페이지에 해당? 그 멋진 남자. – DarthVader

+0

내가 가지고있는 선언 문제와 내가 가진 문제를 벗어나는 캐릭터를 설명해 주신 모든 분들께 감사드립니다. 내 새끼가 나를 배신했다. –

+0

u는'noob'을 호출합니다. – DarthVader

답변

3

당신은 두 개의 서로 다른 변수를 선언하는 ... 다른 범위에

+0

작동하는 답변을 주셔서 감사 드리며 설명을 위해 이해합니다. 이제 내 다음 실수로 ... –

2

는 @의 패 귀하의 변수.

귀하는 또한 01을 선언하고 있습니다.두 번. 클래스에 한 번 (정의되지 않음) 및 해당 메서드의 범위를 벗어나는 버튼 클릭 이벤트 핸들러에서 한 번. 클래스에서 선언하고 메소드에 설정하기 만하면 메소드의 행에서 string[]을 제거하십시오.

+0

괜찮습니다. 버튼 아래에있는 값을 가져 오는 방법에 대한 아이디어는 타이머 아래에서 쉽게 읽을 수 있습니까? –

0

먼저

제거 문자열 [] 파일 경로의 두 번째 선언에서 전역 선언 파일 경로에 대한 액세스를 원하는 경우 : 시작 안 타이머를 사용하여 변수를 설정하십시오.

두 가지 : 처음에 정의 된 변수 유형을 다시 정의 할 필요가 없습니다.

0

귀하의 코드에서 볼 수있는 문제는 string [] filePaths; 클래스 수준에서 다음 timer1_Tick 이벤트에서 사용하지만 문자열 [] filePaths; button1_Click 내에서 유사한 이름 변수를 가지고 있기 때문에 값을 할당받지 못합니다. string [] filePaths = Directory.GetFiles (@directpath, "* .wav"); 그러나이 filePaths 배열의 범위는 단지를 Button1_Click 안에 만

So to resolve your issue please change 

string[] filePaths = Directory.GetFiles(@directpath, "*.wav"); 

to 

filePaths = Directory.GetFiles(@directpath, "*.wav"); 

당신이 덜 변수이 방법이 더 작고 명확한 코드를 당신의 방법을 사용하는 것이 좋습니다 것입니다 : 당신이 사용할 수있는 경우

public void button1_Click(object sender, EventArgs e) 
{ 
    timer1.Enabled = true; 
} 

    private void timer1_Tick(object sender, EventArgs e) 
    { 
     filePaths = Directory.GetFiles(directpath, "*.wav"); 
     if (counter < filePaths.Length) 
     { 
      label1.Text = filePaths[counter]; 
      SoundPlayer simpleSound = new SoundPlayer(filePaths[counter]); 
      simpleSound.Play(); 
      counter++; 
     } 

    } 

Form_Load 이벤트의 Directory.GetFiles는 한 번 호출됩니다.

0

@ 심볼을 잘못 사용하고있는 것처럼 보입니다. 문자열 또는 문자열 참조 앞에있는 @ 기호는 백 슬래시의 이스케이프 기능 (\)을 사용하지 않도록 설정합니다. 일반적으로 현재 (\\)와 같은 백 슬래시가있는 백 슬래시를 이스케이프 처리해야합니다. @(at) sign in file path/string

관련 문제