Zeos6
Zeos6

Reputation: 273

C# String Property and string literal concatenation issue

I am a bit new at C# and I have run into a string concatenation issue. I am hoping someone might be able to give me a hint and help me resolve this. I have searched Google extensively and have spent more than a week on this so any help/advice would be greatly appreciated.

I have created a custom PathEditor for a string property. The property basically allows the user to key in a file to use in the app. If the file typed in is correct, it shows in the property cell as it should. What I am trying to do is output to the property cell an error message if the file typed in does not exist - I check this in my file validator. Here is the string literal issue.

If I use:

return inputFile+"Error_";

this works OK and I get the outpur file123.txtError_ in the property grid cell.

If I use:

return "Error_"+inputFile;

I get only the inputFile without the literal "Error_". Sot he property grid cell shows file123.txt in the property grid cell.

I have checked and inputFile is a string type. Any ideas as to why this is happening?

Also, is there any way to change to font, and/or, color of the message output? I tried to change the background of the property grid cell and I understand that this is not possible to do.

Thank you. Z

More of the code:

[
Description("Enter or select the wave file. If no extension, or a non .wav extension, is specified, the default extension .wav will be added to the filename."),
GridCategory("Sound"),
Gui.Design.DisplayName ("Input Sound"),
PathEditor.OfdParamsAttribute("Wave files (*.wav)|*.wav", "Select Audio File"),
Editor(typeof(PathEditor), typeof(System.Drawing.Design.UITypeEditor))
]
public string InputWavefile
{
    get { return System.IO.Path.GetFileName(inputtWavefile); }
    set 
    {
        if (value != inputWavefile)  // inputWavefile has been changed
        {                           
            // validate the input stringg
             _inputWavefile = FileValidation.ValidateFile(value);

            // assign validated value
            inputWavefile = _inputWavefile;
       }
    }
}

Upvotes: 2

Views: 400

Answers (3)

jim31415
jim31415

Reputation: 8808

I'm guessing your filename looks something like this, C:\Folder\FileName.txt when you start out.

In your FileValidation.ValidateFile() method you

return "Error_" + InputFileName;

it now looks like this: Error_C:\Folder\FileName.txt. So, when you run the line below,

get { return System.IO.Path.GetFileName( _inputWavefile ); }

it strips off the path and returns the filename only, FileName.txt.

Even when the filename is not valid, you are still running System.IO.Path.GetFileName() on it.

Upvotes: 1

Sergei Z
Sergei Z

Reputation: 535

Assuming this is a PropertyGrid in winforms app. Then it's neither a string concatenation issue, nor PropertyGrid issue, as could be proven by the following snippet. So you need to look elsewhere in your code:

public partial class Form1 : Form {
    PropertyGrid pg;
    public Form1() {
        pg = new PropertyGrid();
        pg.Dock = DockStyle.Fill;
        this.Controls.Add(pg);

        var inputFile = "some fileName.txt";
        var obj = new Obj();
        obj.One = "Error_" + inputFile;
        obj.Two = inputFile + "Error_";
        pg.SelectedObject = obj;
    }
}


class Obj {
    public string One { get; set; }
    public string Two { get; set; }
}

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500035

My guess is that you've got a funky character at the start of inputFile which is confusing things - try looking at it in the debugger using inputFile.ToCharArray() to get an array of characters.

The string concatenation itself should be fine - it's how the value is being interpreted which is the problem, I suspect...

Upvotes: 2

Related Questions