class 方法+C# 使用的实例化引用对象的最佳位置在哪里
where is the best place to instantiate reference object used by class methods+C#
我有一个简单而愚蠢的问题,但我希望找到一个答案,让我清楚地了解 C# 引用对象以及实例化它们的最佳位置。我已经在 Class 声明之后声明了 openfile
变量,这样我就可以从 class.every 中的任何其他方法引用它,事情正常,直到我按下添加照片按钮并选择一张照片,如果我确实再次按下按钮并取消了显示对话框 openfile.FileName
不保存文件扩展名。为了解决这个问题,我确实将这行代码 openfile = new OpenFileDialog();
移到了 class constructor.Is 以这种方式实例化对象是错误的,它将在 RAM.or 实例化仅在您要使用它的地方放置对象。
private void addPhotoBtn_Click(object sender, RoutedEventArgs e)
{
openfile = new OpenFileDialog();
openfile.Filter = "Images |*.JPG; *.PNG";
if (openfile.ShowDialog() == true)
{
userImage.Source = new BitmapImage(new Uri(openfile.FileName, UriKind.RelativeOrAbsolute));
filename = openfile.SafeFileName;
sourcePath = @openfile.FileName;
targetPath = System.IO.Path.GetDirectoryName(System.IO.Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory())) + "/Images/users/" + getTime + System.IO.Path.GetExtension(openfile.FileName);
}
}
private void saveUserBtn_Click(object sender, RoutedEventArgs e)
{
string fname, lname, username, password, email, phone, address, role, photo = "";
int spec_id; int dept_id;
fname = firstNameTextBox.Text.ToString();
lname = lastNameTextBox.Text.ToString(); ;
username = usernameTextBox.Text.ToString(); ;
password = passwordBox.Password;
email = emailTextBox.Text.ToString(); ;
phone = phoneTextBox.Text.ToString(); ;
address = addressTextBox.Text.ToString();
spec_id = specComboBox.SelectedIndex;
role = roleComboBox.Text.ToString();
if (deptComboBox.SelectedIndex >= 0)
dept_id = int.Parse(deptComboBox.SelectedValue.ToString());
else
dept_id = 0;
// Input Validation
if (state == "add" && sourcePath != null || sourcePath != null && state != "add")
{
photo = new Uri("pack://application:,,/CMMS;component/images/users/") + getTime + System.IO.Path.GetExtension(openfile.FileName);
}
.
.
.
如果要到处访问文件名,不为空,指定一个本地更改并在选择叔叔时保留地址。
在命名空间之后
private const string FileName = string.Empty;
所以你的代码
private void addPhotoBtn_Click(object sender, RoutedEventArgs e)
{
openfile = new OpenFileDialog();
openfile.Filter = "Images |*.JPG; *.PNG";
if (openfile.ShowDialog() == true)
{
userImage.Source = new BitmapImage(new Uri(openfile.FileName, UriKind.RelativeOrAbsolute));
filename = openfile.SafeFileName;
sourcePath = @openfile.FileName;
this.FileName = @openfile.FileName;
targetPath = System.IO.Path.GetDirectoryName(System.IO.Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory())) + "/Images/users/" + getTime + System.IO.Path.GetExtension(openfile.FileName);
}
}
private void saveUserBtn_Click(object sender, RoutedEventArgs e)
{
string fname, lname, username, password, email, phone, address, role, photo = "";
int spec_id; int dept_id;
fname = firstNameTextBox.Text.ToString();
lname = lastNameTextBox.Text.ToString(); ;
username = usernameTextBox.Text.ToString(); ;
password = passwordBox.Password;
email = emailTextBox.Text.ToString(); ;
phone = phoneTextBox.Text.ToString(); ;
address = addressTextBox.Text.ToString();
spec_id = specComboBox.SelectedIndex;
role = roleComboBox.Text.ToString();
if (deptComboBox.SelectedIndex >= 0)
dept_id = int.Parse(deptComboBox.SelectedValue.ToString());
else
dept_id = 0;
// Input Validation
if (state == "add" && sourcePath != null || sourcePath != null && state != "add")
{
photo = new Uri("pack://application:,,/CMMS;component/images/users/") + getTime + System.IO.Path.GetExtension(this.FileName);
}
.
.
.
我有一个简单而愚蠢的问题,但我希望找到一个答案,让我清楚地了解 C# 引用对象以及实例化它们的最佳位置。我已经在 Class 声明之后声明了 openfile
变量,这样我就可以从 class.every 中的任何其他方法引用它,事情正常,直到我按下添加照片按钮并选择一张照片,如果我确实再次按下按钮并取消了显示对话框 openfile.FileName
不保存文件扩展名。为了解决这个问题,我确实将这行代码 openfile = new OpenFileDialog();
移到了 class constructor.Is 以这种方式实例化对象是错误的,它将在 RAM.or 实例化仅在您要使用它的地方放置对象。
private void addPhotoBtn_Click(object sender, RoutedEventArgs e)
{
openfile = new OpenFileDialog();
openfile.Filter = "Images |*.JPG; *.PNG";
if (openfile.ShowDialog() == true)
{
userImage.Source = new BitmapImage(new Uri(openfile.FileName, UriKind.RelativeOrAbsolute));
filename = openfile.SafeFileName;
sourcePath = @openfile.FileName;
targetPath = System.IO.Path.GetDirectoryName(System.IO.Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory())) + "/Images/users/" + getTime + System.IO.Path.GetExtension(openfile.FileName);
}
}
private void saveUserBtn_Click(object sender, RoutedEventArgs e)
{
string fname, lname, username, password, email, phone, address, role, photo = "";
int spec_id; int dept_id;
fname = firstNameTextBox.Text.ToString();
lname = lastNameTextBox.Text.ToString(); ;
username = usernameTextBox.Text.ToString(); ;
password = passwordBox.Password;
email = emailTextBox.Text.ToString(); ;
phone = phoneTextBox.Text.ToString(); ;
address = addressTextBox.Text.ToString();
spec_id = specComboBox.SelectedIndex;
role = roleComboBox.Text.ToString();
if (deptComboBox.SelectedIndex >= 0)
dept_id = int.Parse(deptComboBox.SelectedValue.ToString());
else
dept_id = 0;
// Input Validation
if (state == "add" && sourcePath != null || sourcePath != null && state != "add")
{
photo = new Uri("pack://application:,,/CMMS;component/images/users/") + getTime + System.IO.Path.GetExtension(openfile.FileName);
}
.
.
.
如果要到处访问文件名,不为空,指定一个本地更改并在选择叔叔时保留地址。 在命名空间之后
private const string FileName = string.Empty;
所以你的代码
private void addPhotoBtn_Click(object sender, RoutedEventArgs e)
{
openfile = new OpenFileDialog();
openfile.Filter = "Images |*.JPG; *.PNG";
if (openfile.ShowDialog() == true)
{
userImage.Source = new BitmapImage(new Uri(openfile.FileName, UriKind.RelativeOrAbsolute));
filename = openfile.SafeFileName;
sourcePath = @openfile.FileName;
this.FileName = @openfile.FileName;
targetPath = System.IO.Path.GetDirectoryName(System.IO.Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory())) + "/Images/users/" + getTime + System.IO.Path.GetExtension(openfile.FileName);
}
}
private void saveUserBtn_Click(object sender, RoutedEventArgs e)
{
string fname, lname, username, password, email, phone, address, role, photo = "";
int spec_id; int dept_id;
fname = firstNameTextBox.Text.ToString();
lname = lastNameTextBox.Text.ToString(); ;
username = usernameTextBox.Text.ToString(); ;
password = passwordBox.Password;
email = emailTextBox.Text.ToString(); ;
phone = phoneTextBox.Text.ToString(); ;
address = addressTextBox.Text.ToString();
spec_id = specComboBox.SelectedIndex;
role = roleComboBox.Text.ToString();
if (deptComboBox.SelectedIndex >= 0)
dept_id = int.Parse(deptComboBox.SelectedValue.ToString());
else
dept_id = 0;
// Input Validation
if (state == "add" && sourcePath != null || sourcePath != null && state != "add")
{
photo = new Uri("pack://application:,,/CMMS;component/images/users/") + getTime + System.IO.Path.GetExtension(this.FileName);
}
.
.
.