存储while循环生成的变量

时间:2016-07-01 09:59:08

标签: arrays vb.net csv variables datagrid

我有一个解析csv文件的while循环,并将变量插入到一系列数组中。这是通过我主表单上的按钮调用的。

这些变量用于图表(在同一个子图中),也用于数据网格,它位于单独的表格中。

第一次单击按钮时,一切正常,但是如果我再次单击它,则不会填充单独表单上的数据网格,因为我正在丢失变量。

因此,在解析CSV文件的while循环中,我有:

class UserStoriesController < ApplicationController

  before_action :set_project
  before_action :set_user_story, except: [:index, :new, :create]

  def index
    @user_stories = @project.user_stories.rank(:row_order).all
  end

  def update_row_order
    @user_story.row_order_position = user_story_params[:row_order_position]
    @user_story.save

    render nothing:true # this is a POST action, updates sent via AJAX, no view rendered
  end

  def create
    @user_story = @project.user_stories.create(user_story_params)
    redirect_to @project
  end

  def new
    @user_story = UserStory.new
    @user_story.project = @project
    # Set other important default values for display now
  end 

  def destroy
    if @user_story.destroy
      flash[:success] = "User story deleted"
    else
      flash[:error] = "User story could not be deleted"
    end
    redirect_to @project
  end

  def complete
    @user_story.update_attribute(completed_at, Time.now)
    redirect_to @project, notice: "User story completed functionality complete"
  end

  def update
    respond_to do |format|
      if @project.user_stories.update(@project, user_story_params)
        format.html { redirect_to project_path(@project), notice: 'User story was successfully updated.' }
        format.json { render :show, status: :ok, location: @user_story }
      else
        format.html { render :edit }
        format.json { render json: @user_story.errors, status: :unprocessable_entity }
      end
    end
  end

  def edit
    @project = Project.find(params[:project_id])
  end

  def show
  end

 private

  def set_project
    @project = Project.find(params[:project_id])
  end

  def set_user_story
    @user_story = @project.user_stories(params[:id])
  end

  def user_story_params
    params[:user_story].permit(:param1, :param2, :param3, :row_order_position)
  end
end

我已经尝试将变量公开,但是因为它们是数组,并且是从while循环构造的,所以我似乎无法公开访问它们。

我的代码(为简洁起见编辑)

frmNumericChart.DataGridView1.Rows.Add(freq, dBu, dbnorm, ScaleFactor)

如何将这些数组变量设为全局?

请原谅我的伪劣代码 - 我这样做是为了一个爱好并随着我的学习而学习。

1 个答案:

答案 0 :(得分:0)

我通过在模块中添加公共类来解决这个问题。

在“公共类”中,我有“公共共享子”,我希望从代码中的任何位置访问。

示例:

Public Module Module1

<various public variables here>

Public Class MyData

        Public Shared Sub debugTempFile()
            ' DEBUG
            Dim mytempFolder As String = Path.GetTempPath()
            Dim MyOutFile As String = mytempFolder + "outfile.txt"
            Dim myfile As System.IO.StreamWriter
            myfile = My.Computer.FileSystem.OpenTextFileWriter(MyOutFile, True)
            myfile.WriteLine(debugdata)
            myfile.Close()
            ' END DEBUG
        End Sub
End Class
End Module

可以使用call语句从任何地方调用此Public Sub:

Call MyData.debugTempFile()

我希望这对我这样的其他初学者有用。

相关问题