4

I have created a boolean algebraic simplifier. It simplifies expressions and I am content with it. However, I am trying to add a feature that allows users to check if two expressions are equivalent. For this I have created a new form that allows the user to input two expression by clicking buttons. To do this, I thought it best to simplify both expressions and then compare the two for equivalency. As I have got lots of subroutines and code that works for simplification in another form, I thought making the form a child form of the form with the code in would allow me to call the subroutines instead of copying them onto the form. I have made these protected in the parent form. I have inherited like so:

public partial class Expression_Equivalency_Form : Expression_Simplifier

However, when I click onto the form designer, this error appears and I cannot view the graphical interface of the form:

"Could not find file File Path"

The file is in the debug folder which is within the bin folder within the folder containing the program and is recongised in the parent class. The file is read from and appeneded by the parent form without issue. I have tried to research this but have been unable to find a solution. Does anyone know one?

I have read to the file and appended to it. I have also used the following code to remove any blank lines from my text file:

File.WriteAllLines("PreviousExpressionInputs.txt", 
  File.ReadAllLines("PreviousExpressionInputs.txt").Where(l => !string.IsNullOrWhiteSpace(l)));

Code that writes to the file:

using (BinaryWriter Writer = new BinaryWriter(File.Open("PreviousExpressionInputs.txt", 
    FileMode.Append)))
{
    Writer.Write(expressionandanswertowritetotextfile);                    
}

Code that reads from the file:

foreach (string line in File.ReadLines("PreviousExpressionInputs.txt"))
{
    try
    {
        LinesInFile.Add(line);
    }
    catch (Exception)
    {
    }
}
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
B.Iqbal
  • 139
  • 6
  • Maybe you have cleanup code that remove the file after it was used? It will be re-copied on every new build and usually you restart the programm by hitting the "recompile/start again" button. – Christopher Feb 22 '19 at 23:01
  • 1
    What file is not found, is this a cs file? an assembly? the actually designer at design time? – TheGeneral Feb 22 '19 at 23:05
  • @MichaelRandall It is a simple text file. It contains the original expression and the simplified expression. I output the 10 latest inputted expressions onto a table panel which can be clicked on to show the simplification of those expressions. – B.Iqbal Feb 22 '19 at 23:12
  • @Christopher I do not think so. My experience with using files is minimal. I have appended the file, read from it, and made it so that the file has no blank lines. I have added the line of code used to remove the blank lines into my original question in case this is causing an issue. – B.Iqbal Feb 22 '19 at 23:13
  • 1
    Please add the code you are using that opens the file – TheGeneral Feb 22 '19 at 23:17
  • @MichaelRandall Ok – B.Iqbal Feb 22 '19 at 23:22
  • 1
    Inheritance is wrong in this case. Move "shared" code into classes that both forms can use, an Expression class, if you will. Your write code assumes the file exists with your ReadAllLines bit. That's a dangerous assumption. Use full path names, too. Empty Try-Catches are a big no-no, as well. – LarsTech Feb 22 '19 at 23:25
  • @LarsTech Thank you for the advice. Out of curiousity, why is it better to have a common class in this case than use form inheritance? – B.Iqbal Feb 22 '19 at 23:27
  • Based on what you described, it doesn't sound like a case for using inheritance since you only want the "shortcut" to code you need to use. Using Form inheritance is tricky — the designer will fight you. – LarsTech Feb 22 '19 at 23:29
  • 1
    File paths in code should always be fully qualified, where possible (e.g. `c:\temp\bob.txt` not `bob.txt`). Otherwise the code will stop working if the current directory of your application changes or is not what you expect. – mjwills Feb 23 '19 at 00:03
  • 1
    Please don't ever write `catch (Exception) { }` that's very bad. – Enigmativity Feb 23 '19 at 00:20
  • Code which you have in constructor of the base form will run in design mode of the derived form. Read [this answer](https://stackoverflow.com/a/32299687/3110834) to learn more about designer. – Reza Aghaei Feb 23 '19 at 09:45
  • @Enigmativity Why is that? – B.Iqbal Feb 24 '19 at 01:04
  • Because they swallow all exceptions and make debugging exceptionally hard. You should only ever capture _specific_ exceptions that you can _meaningfully_ handle. – Enigmativity Feb 24 '19 at 06:31
  • @Enigmativity Thank you for the advice. – B.Iqbal Feb 25 '19 at 20:48

1 Answers1

1

Consider following facts:

  • When you open a form in design mode, the constructor of its base class will run.
  • When you look for a relative file name, the path will be resolved relative to the current working directory of the application.
  • When the form is in design mode, the current application is Visual Studio and its working directory is where the devenv.exe is located.

It describes why you cannot find your text files. Because you have some code in the constructor of your base form(or fir example load event handler of the base form) which looks for the file and since the filename is relative, its looking for the file in the Visual Studio working directory and could not find file.

How to prevent the problem? Check DesignMode property to prevent running the code:

public partial class MyBaseForm : Form
{
    public MyBaseForm()
    {
        InitializeComponent();
    }

    private void MyBaseForm_Load(object sender, EventArgs e)
    {
        MessageBox.Show("This will show both in run-time and design time.");

        if (!DesignMode)
            MessageBox.Show("This will show just in run-time");
    }
}

Create the derived form and open it in designer to see what happens:

public partial class Form1 : MyBaseForm
{
    public Form1()
    {
        InitializeComponent();
    }
}

To learn more about how designer works take a look at this post.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398