Basics Of Overloading Operators in C++

Ever wanted to compare two Dogs? Well now you can!

class Dog
{
  private:
    int m_age;
    string m_name;
  public:
    Dog(const int age = 1, const string name = "Un-named")
    {
      m_age = age;
      m_name = name;
    }

    //Here's where it gets interesting...
    //Basic syntax
    bool operator ==(const Dog rhs); //rhs = right-hand side

};

bool Dog::operator ==(const Dog rhs)
{
  return (m_age == rhs.m_age && m_name == rhs.m_name);
}

//Implementation...

Dog spike(3, "Spike"), brownie(7, "Brownie");
//Notice that brownie is on the right-hand side
//of the == operator
bool dogs_are_equal = (spike == brownie); //false

Advertisements

How to save settings to an internal config in visual C# (.NET)

In this tutorial I teach you how to save settings to an internal config!

Saving settings to a config is really easy in C#, so let’s begin the tutorial!

1. Right-click on the project root in the Solution Explorer:

Capture

2. Select “Properties” from the list:

Capture

3.  You should see something similar to this:

Capture

4. Set the names, types, and default values to whatever you like. DO NOT change the scope to “Application” if you plan on being able to edit the settings from the code.

5. Getting and Setting the property:
    Getting:  [Variable Type] [Variable Name] = Properties.Settings.Default.[Name of Property];

    Setting:  Properties.Settings.Default.[Name of Property] = [String, Int, etc. (As long as it is the same type as the property)];

Capture

In this example, I am checking if the config property LastSaveDirectory is not empty. If it is not empty, set the SaveFileDialog‘s Initial Directory to the stored property.

 

*BONUS*

How to loop through all Properties and display them in a MessageBox:

Capture

NOTE: I would recommend a String Builder if you plan on having a lot of properties.

Going border-less full-screen in visual C# (.NET)

Going border-less full-screen in C#.NET is actually really, really easy.

1. Set the FormBorderStyle to None: FormBorderStyle = FormBorderStyle.None;

2. Set the WindowState to Maximized: WindowState = FormWindowState.Maximized;

You may or may not have to adjust the width/height and location of various controls on the form.

To return the form to normal:

1. Set the FormBorderStyle to Sizable: FormBorderStyle = FormBorderStyle.Sizable;

2. Set the WindowState to Normal: WindowState = FormWindowState.Normal;

Again, you may or may not have to adjust various properties of controls on the form after doing this.

 

NOTE: I only have to adjust a few properties when going full-screen, and the form returns said properties to their original state when returning to normal mode.

Writing text to files in visual C# (.NET)

Ok, ok. Writing text to files with C# is pretty easy. You just throw in that good old System.IO.File.WriteAllLines([path], [string array]);

But, what if you don’t want to explicitly ask the user (or yourself) for the path every time?
That’s where the SaveFileDialog class comes in handy.
Instead of having to ask the user for the path each time, you can simply do this instead:
1. Instantiate the class: SaveFileDialog SFD = new SaveFileDialog();
2. Set a filter if you so desire: SFD.Filter = "Text File|*.txt|All Files|*.*"; (This causes that handy-dandy drop-down of different file types to appear like when you save files with notepad and other editors.)
3. Set the title of the save dialog: SFD.Title = "Save the File";
4. Show the save dialog: SFD.ShowDialog();
5. Check to make sure the file name is not empty: if (SFD.FileName != "") { System.IO.File.WriteAllLines(SFD.FileName, [string array]); }
Notice how you never have to prompt the user for the file path, you can easily retrieve it by referencing SFD.FileName, as the file name is actually the path to the file, including it’s name.


For opening files, you can use the OpenFileDialog which works almost exactly like SaveFileDialog.