I have Simple and stupid Question however I hope to find an answer that will make my mind clear about C# refrence objects and where is the best place to instantaite them. i have declared the openfile
Varaible after Class Declaration so that i can refrence it from any other method inside the class.every thing works fine until i press the add photo button and choose a photo if i did press the button again and Canceled the show dialog the openfile.FileName
does not save the file extension. to solve the problem i did move this line of code openfile = new OpenFileDialog();
to the class constructor.Is it wrong to instantaite the object in that way that it will hold aspace in RAM.or instantaite the object only in the place where you are going to use it.
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);
}
.
.
.
If you want to access the Filename everywhere and not empty, specify a local change and hold the address while choosing the uncle. After Namespace
private const string FileName = string.Empty;
And so your code
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);
}
.
.
.