【问题标题】:C# / Windows Forms: error cs1519C#/Windows 窗体:错误 cs1519
【发布时间】:2009-11-30 06:05:36
【问题描述】:

我正在开发一个图形转换程序,设置一个列表框来读取目录中的文件名。该代码基于我的讲师的示例,因此我认为它可以正常工作,但似乎到处都会产生错误。我在 Google 上搜索了“错误 CS1519:类、结构或接口成员声明中的无效令牌‘,’”,但我发现看起来不适用。这里是:

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

【问题讨论】:

    标签: c# windows forms


    【解决方案1】:

    那是因为你需要放:

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

    在函数中。

    也许你可以这样做:

    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
      }
    

    请注意,您将foreach 循环放在Transformer_Load 函数中;当然,您可以将其放在任何其他功能中。请注意,selFilecurrentDirfileEntries 变量前面没有修饰符 (private)。

    【讨论】:

      【解决方案2】:

      看看错误从哪里开始!

      你不能在类体内有语句。它需要在方法/属性/构造函数中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-09
        相关资源
        最近更新 更多