1

I have a method inside a class that will receive a PictureBox and a String, so the user can select the image for the PictureBox and at the same time for the program to know what file plus extension of the chosen file for later use.

The string it will be set, for example as 1.png but on the where I call this method the string it will be as "" rly don't understand why this is happening.

On the GerirDoc.cs I define the string as String _imgFile = "" at the beginning of the Form and have this code:

DocImg docImg = new DocImg();
docImg.selectImage(_imgFile, this.pictureBoxDoc);

The class DocImg

class DocImg
{

    public int Hwnd { get; private set; }

    public void selectImage(String imgFile, PictureBox imgBox)
    {
    OpenFileDialog openFileDialog = new OpenFileDialog();
    openFileDialog.Title = "Escolher imagem";
    openFileDialog.Filter = "Image files (*.jpg, *.jpeg, *.png) | *.jpg; *.jpeg; *.png";
    openFileDialog.Multiselect = false;

    if (openFileDialog.ShowDialog() == DialogResult.OK)
    {
        imgFile = openFileDialog.SafeFileName;
        DialogResult dialogResult = MessageBox.Show("Deseja passar a imagem para o aparelho se tiver ligado ao computador?", "Informação", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
        imgBox.Image = Image.FromFile(openFileDialog.FileName);
        switch (dialogResult)
        {
            case DialogResult.Yes:
                Shell shell = new Shell();
                Folder folder = shell.BrowseForFolder((int)Hwnd, "Selecione o caminho para a pasta \"Imagens\"", 0, 0);
                if (folder != null)
                {
                    FolderItem _destinationDir = (folder as Folder3).Self;
                    if (String.Equals(_destinationDir.Name, "Imagens"))
                    {
                        try
                        {
                            folder.CopyHere(openFileDialog.FileName, null);
                            MessageBox.Show("Imagem guardada com sucesso", "Informação", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.ToString());
                        }
                    }
                    else
                    {
                        MessageBox.Show("A pasta de destino tem que ser a pasta \"Imagens\" que se está dentro de adbRetail");
                    }
                }
                break;
            case DialogResult.No:
                MessageBox.Show("De lembrar que a imagem só ira aparecer correctamente se tiver na pasta correcta do aparelho", "Informação", MessageBoxButtons.OK, MessageBoxIcon.Information);
                break;
        }
    }
}

}

I did put a breakpoint just after imgFile = openFileDialog.SafeFileName; and the variable imgFile as the value of the filename, ex 1.png, but on GerirDoc.cs after choosing the file of the image, _imgFile doesn't have any value.

On GerirDoc.cs I only put _imgFile = "" when the form is created. Why is this happening ? Since I send the string inside it and since in DocImg if the user chose a image it should have the filename, ex 1.png and not be empty

Camadas
  • 509
  • 1
  • 5
  • 21
  • Strings are immutable and `_imgFile` is just a reference to a String. So `_imgFile` in `GerirDoc.cs` will not be changed by anything you do in `selectImage` – Sentry Mar 21 '18 at 12:04
  • https://stackoverflow.com/questions/43649221/c-sharp-passing-string-as-parameter-to-method-and-update-in-it-not-reflect-origi – Sentry Mar 21 '18 at 12:07

1 Answers1

3

As I can understand you want method to change your local variable. You should change it to:

public void selectImage(out string imgFile, PictureBox imgBox)

notice the parameter modifier out. And then call this method like this:

docImg.selectImage(out _imgFile, this.pictureBoxDoc);

The out keyword causes arguments to be passed by reference. It is like the ref keyword, except that ref requires that the variable be initialized before it is passed.

More reference here: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/out-parameter-modifier

Markiian Benovskyi
  • 2,137
  • 22
  • 29
  • Thanks it worked, but on the DocImg I did still have to put the imgFile as `imgFile = ""` since it giving an error – Camadas Mar 21 '18 at 12:12
  • @Camadas Depending on the code where you use your `_imgFile` later, you may need to have it explicitly assigned. – Markiian Benovskyi Mar 21 '18 at 12:14
  • Its just to see if its equals to "" or not, the reason that I define as "" at the beginning of the form, so if its empty to select the image that is inside the object Documents (another class) to get the file image from there (since a new one wasn't chosen) and update the row of the db of that document – Camadas Mar 21 '18 at 12:16
  • @Camadas You can use `String.IsNullOrEmpty` for these checks https://msdn.microsoft.com/en-us/library/system.string.isnullorempty(v=vs.110).aspx – Markiian Benovskyi Mar 21 '18 at 12:18
  • 1
    Ty, all ways learning, it makes more sense `String.IsNullOrEmpty` for that part of the code. – Camadas Mar 21 '18 at 12:22