Altimus Prime
Altimus Prime

Reputation: 2327

C# accessing variables across different objects

Being new to c#, I don't understand how variables are passed between objects. My array variable "filePaths" is coming back null when I exectute this program. It is a basic windows form. I'm working on making a program that will show the words and play the sound.

The specific error is "NullReferenceException was unhandled.

Here is my particular code.

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++;
        }

    }
}

}

Thanks in advance.

Upvotes: 0

Views: 215

Answers (5)

HatSoft
HatSoft

Reputation: 11201

The problem i can see in your code is declare string[] filePaths; at class level and then use it in timer1_Tick Event but string[] filePaths; never get values assigned to it because you have a similar name variable inside button1_Click on line : string[] filePaths = Directory.GetFiles(@directpath, "*.wav"); but the scope of this filePaths array is just inside button1_Click only

So to resolve your issue please change

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

to 

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

I would suggest you use your methods this way more smaller and clear code with less variables :

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++;
        }

    }

If you can use Directory.GetFiles in Form_Load event this way it will get called just once

Upvotes: 0

TheSecretSquad
TheSecretSquad

Reputation: 313

It looks like you are using @ symbol incorrectly. The @ symbol in front of a string or string reference is meant to disable the escaping feature of backslashes (\). Normally you have to escape backslashes with an additional backslash like you currently have (\\).

So...

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

is equivalent to

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

See also : @(at) sign in file path/string

Upvotes: 0

djmati11
djmati11

Reputation: 597

First: you shouldn't start timer before setting the variable.

Two: you don't have to redefine type of variable if defined at the beginning.

Upvotes: 0

Blau
Blau

Reputation: 5762

You are declaring two different variables... in different scopes

remove string[] from the second declaration of filepath if you want access to the global declared filepath

Upvotes: 3

Austin Salonen
Austin Salonen

Reputation: 50225

Lose the @'s when referencing your variables.

You're also declaring filePaths twice. Once in the class (and is never defined) and once in the button click event handler that goes out of scope in that method. You only want to declare it in the class and set it in the method so remove the string[] from the line in the method.

Upvotes: 2

Related Questions