不确定如何'呼叫'变量

本文关键字:变量 呼叫 不确定 | 更新日期: 2023-09-27 18:07:57

我有三个方法,分别是getUserID, getgazeIDupdateHeatmapURL

这是getUserID

private static int getUserID()
{
    int returnValue = -1;
    try
    {
        TextReader tr = new StreamReader("C:''Users''L31304''Desktop''user.txt");
        string checkedSubject = tr.ReadLine();
        tr.Close();
        MySqlCommand selectUser = new MySqlCommand();
        selectUser.Connection = c;
        selectUser.CommandText = "SELECT userID from user WHERE name= @personName";
        selectUser.CommandType = CommandType.Text;
        selectUser.Parameters.Add("@personName", MySqlDbType.VarChar).Value = checkedSubject;
        returnValue = (int)selectUser.ExecuteScalar();
        Console.WriteLine("returnValue for User-" + returnValue);
        return returnValue;
    }
    catch (Exception e)
    {
        Console.WriteLine("returnValue Exception-" + e.ToString());
        return returnValue;
    }
}

getgazeID

 private static int getgazeID(int userID)
{
    int returnValueGaze = -1;
    try
    {
        MySqlCommand selectGaze = new MySqlCommand();
        selectGaze.Connection = c;
        selectGaze.CommandText = "SELECT gazeID from gazeperiod WHERE userID = @userID";
        selectGaze.CommandType = CommandType.Text;
        selectGaze.Parameters.Add("@userID", MySqlDbType.Int64).Value = userID;
        returnValueGaze = (int)selectGaze.ExecuteScalar();
        Console.WriteLine("returnValue for Gaze-" + returnValueGaze);
        return returnValueGaze;
    }
    catch (Exception e)
    {
        Console.WriteLine("returnValue Exception for gazePeriod-" + e.ToString());
        return returnValueGaze;
    }
}

这是updateheatmapURL

private static int updateHeatmapURL()
{
    try
    {
        MySqlCommand selectGaze = new MySqlCommand();
        selectGaze.Connection = c;
        selectGaze.CommandText = "UPDATE gazeperiod(heatmapURL) VALUES (@heatmapURL) WHERE userID = @userID AND gazeID = @gazeID";
        selectGaze.CommandType = CommandType.Text;
        selectGaze.Parameters.Add("@heatmapURL", MySqlDbType.VarChar).Value = dlg.FileName;
        selectGaze.Parameters.Add("@userID", MySqlDbType.Int64).Value = userID;
        selectGaze.Parameters.Add("@gazeID", MySqlDbType.Int64).Value = gazeID;
        selectGaze.ExecuteScalar();
        Console.WriteLine("heatmapURL - " + dlg.FileName);
    }
    catch (Exception e)
    {
        Console.WriteLine("Exception for heatmapURL-" + e.ToString());
    }
}

这就是dlg的来源。

 public static bool ExportImageToFile(Image image)
{
  SaveFileDialog dlg = new SaveFileDialog();
  dlg.Title = "Please enter filename for image...";
  dlg.InitialDirectory = Environment.SpecialFolder.MyDocuments.ToString();
 dlg.Filter = "JPEG Format - jpg|*.jpg|Bitmap Format - bmp|*.bmp|Graphics Interchange Format - gif|*.gif|Portable Networks Graphic - png|*.png|Tag Image File Format - tif|*.tif|Windows MetaFile Format - wmf|*.wmf";
  dlg.FileName = "*.jpg";
  dlg.AddExtension = true;}

但是,userID、gazeID和dlg。文件名表示:

'名称在当前上下文中不存在。'

我如何在updateURL中调用它使它存在?

public static bool ExportImageToFile(Image image)
{
  SaveFileDialog dlg = new SaveFileDialog();
  dlg.Title = "Please enter filename for image...";
  dlg.InitialDirectory = Environment.SpecialFolder.MyDocuments.ToString();
  //dlg.InitialDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "new_folder2");
  bool saveToServer = false;
  //check.... 
  if (System.IO.File.Exists("C:''Users''L31304''Desktop''user.txt"))
  {
      dlg.InitialDirectory = @"''111.11.111.111'c$'Users'L31303'person'EyeTrackerWeb'WebContent'uploadheatmap";
      saveToServer = true;
  }
        //set bool to true
    //end if
  dlg.Filter = "JPEG Format - jpg|*.jpg|Bitmap Format - bmp|*.bmp|Graphics Interchange Format - gif|*.gif|Portable Networks Graphic - png|*.png|Tag Image File Format - tif|*.tif|Windows MetaFile Format - wmf|*.wmf";
  dlg.FileName = "*.jpg";
  dlg.AddExtension = true;
  dlg.RestoreDirectory = true;
  if (dlg.ShowDialog() == DialogResult.OK)
  {
    ImageFormat format;
    switch (dlg.FilterIndex)
    {
      case 1:
        format = ImageFormat.Jpeg;
        break;
      case 2:
        format = ImageFormat.Bmp;
        break;
      case 3:
        format = ImageFormat.Gif;
        break;
      case 4:
        format = ImageFormat.Png;
        break;
      case 5:
        format = ImageFormat.Tiff;
        break;
      case 6:
        format = ImageFormat.Wmf;
        break;
      default:
        format = ImageFormat.Jpeg;
        break;
    }
    try
    {
      image.Save(dlg.FileName, format);
      Console.WriteLine("file name is" + dlg.FileName);
      if (saveToServer == true)
      {
          connectDB();
          OpenConnection();
         int userID = getUserID();
         int gazeID =  getgazeID(userID);
         CloseConnection();
      }
      else
      { 
      }
        //if bool == true, then do the following
        //select userID from user table WHERE name is name from text file
        //select gazePeriodID from gazePeriod where userID the above selected userID
        //update image path to gazePeriod in heatmapimage
        //delete text file
    }
    catch (Exception ex)
    {
      VGExceptionMethods.HandleException(ex);
      return false;
    }
  }
  return true;

}
private static int getUserID()
{
    int returnValue = -1;
    try
    {
        TextReader tr = new StreamReader("C:''Users''L31304''Desktop''user.txt");
        string checkedSubject = tr.ReadLine();
        tr.Close();
        MySqlCommand selectUser = new MySqlCommand();
        selectUser.Connection = c;
        selectUser.CommandText = "SELECT userID from user WHERE name= @personName";
        selectUser.CommandType = CommandType.Text;
        selectUser.Parameters.Add("@personName", MySqlDbType.VarChar).Value = checkedSubject;
        returnValue = (int)selectUser.ExecuteScalar();
        Console.WriteLine("returnValue for User-" + returnValue);
        return returnValue;
    }
    catch (Exception e)
    {
        Console.WriteLine("returnValue Exception-" + e.ToString());
        return returnValue;
    }
}
private static int getgazeID(int userID)
{
    int returnValueGaze = -1;
    try
    {
        MySqlCommand selectGaze = new MySqlCommand();
        selectGaze.Connection = c;
        selectGaze.CommandText = "SELECT gazeID from gazeperiod WHERE userID = @userID";
        selectGaze.CommandType = CommandType.Text;
        selectGaze.Parameters.Add("@userID", MySqlDbType.Int64).Value = userID;
        returnValueGaze = (int)selectGaze.ExecuteScalar();
        Console.WriteLine("returnValue for Gaze-" + returnValueGaze);
        return returnValueGaze;
    }
    catch (Exception e)
    {
        Console.WriteLine("returnValue Exception for gazePeriod-" + e.ToString());
        return returnValueGaze;
    }
}
public class Form1 : Form
{
   private static Form1 _instance;
   public Form1()
   {
       this.InitializeComponent();
       _instance = this;
   }
   private static int updateHeatmapURL()
   {
       try
       {
           MySqlCommand selectGaze = new MySqlCommand();
           selectGaze.Connection = c;
           selectGaze.CommandText = "UPDATE gazeperiod(heatmapURL) VALUES (@heatmapURL) WHERE userID = @userID AND gazeID = @gazeID";
           selectGaze.CommandType = CommandType.Text;
           var userID = getUserID();
           selectGaze.Parameters.Add("@heatmapURL", MySqlDbType.VarChar).Value = _instance.dlg.FileName;
           selectGaze.Parameters.Add("@userID", MySqlDbType.Int64).Value = userID;
           selectGaze.Parameters.Add("@gazeID", MySqlDbType.Int64).Value = getgazeID(userID);
           selectGaze.ExecuteScalar();
           Console.WriteLine("heatmapURL - " + _instance.dlg.FileName);
       }
       catch (Exception e)
       {
           Console.WriteLine("Exception for heatmapURL-" + e.ToString());
       }
   }
}

类是

public class Images
{
  private static MySqlConnection c;
  private static string server;
  private static string database;
  private static string uid;
  private static string password;

不确定如何'呼叫'变量

试试这个

//update these lines in updateHeatmapURL mthod
// dlg.File name is not accessable because updateHeatmapURL method is static
// use instance to access dlg or remove static, if you remove static then you need to remove it from other two methods as well
var userId = getUserID();
selectGaze.Parameters.Add("@userID", MySqlDbType.Int64).Value = userId;
selectGaze.Parameters.Add("@gazeID", MySqlDbType.Int64).Value = getgazeID(userId);

编辑
public class Form1 : Form
{
    private static Form1 _instance;
    public Form1()
    {
        InitializeComponent();
        _instance = this;
    }
    private static int updateHeatmapURL()
    {
        ...
        selectGaze.Parameters.Add("@heatmapURL", MySqlDbType.VarChar).Value = _instance.dlg.FileName;
        var userId = getUserID();
        selectGaze.Parameters.Add("@userID", MySqlDbType.Int64).Value = userId;
        selectGaze.Parameters.Add("@gazeID", MySqlDbType.Int64).Value = getgazeID(userId);
        ...
    }
}