Wednesday, June 26, 2024

Return Type Overloading in C++

Small tricks to accomplish return type overload in c++. In cpp one can not overload return type. If you follow this trick you can overload return type using operator overloading :)


struct Car {

    string name;

    string brand;

};


struct Bus {

    string name;

    string brand;

};


struct Transport 

{

    Car get_car()

    { 

        Car oCar;

        oCar.name="Axio"; 

        oCar.brand="Toyota";

        return oCar; 

    }

    

    Bus get_bus()

    {

        Bus oBus;

        oBus.name="Greenline"; 

        oBus.brand="Hundai";

        return oBus; 

    }

    

    auto get_transport()

    {

        struct result

        {

          Transport * trans;

          operator Car() { return trans->get_car(); }

          operator Bus() { return trans->get_bus(); }

        };

        

        return (result{this});

    }

};


int main() {

    

    Transport tt;

    Car myCar = tt.get_transport();

    Bus myBus = tt.get_transport();

    

    cout << myCar.name << " == " << myCar.brand << endl;

    cout << myBus.name << " == " << myBus.brand << endl;


    return 0;

}


============

You can not use below code it will get compile error:

struct Transport 

{

    Car get_transport() { return (Car{});}

    Bus get_transport() { return (Bus{});}

};

Tuesday, June 5, 2012

.Net 3.5 OpenFileDialog issue in Windows SP3

Recently me and my co-worker found a strange issue while developing something where we used a c++ dll imported from .net project. Inside the c++ dll we have used some relative paths which is needed on each operation to load resources. If we use OpenFileDialog in c# before calling the method of the c++ dll it changes the current directory and the dll not able to find the path. Here is the source code for better understanding:

  OpenFileDialog opd = new OpenFileDialog(); 


 if (opd.ShowDialog() == DialogResult.OK) 
 {


 }


 moo(@"D:\Images\DB\fao.bmp"); // moo is a method of c++ dll imported here 

Above code unfortunately do not work on my co-workers machine. He is using windows xp sp3. The fun part is that the same code works properly in my machine which is windows 7. We both used same .net framework. In my co-worker's machine if he uses the below code it works perfectly:

  /*
 OpenFileDialog opd = new OpenFileDialog(); 


 if (opd.ShowDialog() == DialogResult.OK) 
 {
 } 
 */


 moo(@"D:\Images\DB\fao.bmp"); // moo is a method of c++ dll imported here



Wednesday, January 18, 2012

Make visual studio 2008 winform design view faster

How to make faster winform design view faster:

The setting is in Tools -> Options -> Windows Forms Designer, set "AutoToolboxPopulate" to false.

See the image below:


Design view of visual studio 2008 is too slow (control movement slow) -- How to fix [web view]

I was struggling for this issue. Lets see how we can fix it quickly:

Download Updated VS2008 Hotfix KB967253

Download and Install

Issue:

If you work in design view of visual studio 2008, you will notice it is tremendously slow. In a word it is quite impossible to design a large form :(. The whole purpose of using visual studio is defeated here. We like vs for its quick design facilities.... Actually it a known issue of that vs version. So to fix it we need to download and install the above fix from microsoft.

Saturday, October 22, 2011

Select all text using ctrl + A in a TextField (multiline) C#

This is quite simple .... just add the keyDown event like below:

this.txtField.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtField_KeyDown);

You should add this event for those textfield only where you want to add this functionality ... Now just add below simple code and enjoy select all feature :D

private void txtField_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && (e.KeyCode == Keys.A))
{
((TextBox)sender).SelectAll();
e.SuppressKeyPress = true;
e.Handled = true;
}

}

Try and enjoy :)

PC Magazine Tips and Solutions

PC World: Latest Technology News

PCWorld.com - Most Popular Downloads of the Week