tags:

views:

32

answers:

2

Hi, I'm working on a graphics transformation program, setting up a listbox to read in the names of files in a directory. The code is based off an example from my instructor, so I thought it would work fine, but it seems to create errors everywhere. I googled "error CS1519: Invalid token ‘,’ in class, struct, or interface member declaration", but what I found looked unapplicable. Here it is:

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;

namespace Transformer
{
 public partial class Transformer : Form
 {
  /* Initialize parameters */
  private bool drawAxes = true;
  private bool drawGrid = true;

  private List<ObjectSettings> dispObjects = new List<ObjectSettings>();

  /* Initialize form */

  public Transformer()
  {
   InitializeComponent();
  }

  private void Transformer_Load(object sender, EventArgs e)
  {
  }


  /* Populate available objects listbox */

  private int selFile = 0;
  private string currentDir = Directory.GetCurrentDirectory();

  // errors start around here

  private string[] fileEntries = Directory.GetFiles(currentDir+@"\Objects");
  foreach (string s in fileEntries) {
      int start = s.LastIndexOf(@"\");
      int end = s.LastIndexOf(@".");
      availObjectsListBox.Items.Add(s.Substring(start + 1, end - start - 1));
  } // end foreach
 }
}
+1  A: 

That's because you need to put:

 foreach (string s in fileEntries) {
      int start = s.LastIndexOf(@"\");
      int end = s.LastIndexOf(@".");
      availObjectsListBox.Items.Add(s.Substring(start + 1, end - start - 1));
  }

in a function.

Maybe you can do something like:

private void Transformer_Load(object sender, EventArgs e)
  {

 int selFile = 0;
string currentDir = Directory.GetCurrentDirectory();
string[] fileEntries = Directory.GetFiles(currentDir+@"\Objects");
  foreach (string s in fileEntries) {
      int start = s.LastIndexOf(@"\");
      int end = s.LastIndexOf(@".");
      availObjectsListBox.Items.Add(s.Substring(start + 1, end - start - 1));
    } // end foreach
  }

Note that you put the foreach loop inside the Transformer_Load function; of course, you can put it in any other function. And note that there is no modifier (private) in front of selFile, currentDir and fileEntries variable.

Ngu Soon Hui
A: 

Look where the errors start!

You cant have statements in the class body. It needs to be in a method/property/constructor.

leppie