Showing posts with label Visual Studio. Show all posts
Showing posts with label Visual Studio. Show all posts

14 June 2017

GUID generator in Visual Studio

While writing scripts at times you need to use GUID value as a constant value, etc. There are many options to generate GUID. You could write your own batch script to generate the GUID or you could use other program to generate a GUID.

However if you are using Visual Studio IDE, there is readily an option available to generate the GUID.

Steps to generate GUID in visual Studio

  1. On the tool menu, select CREATE GUID menu item. It opens up CREATE GUID window

  2. In the CREATE GUID window, click on the copy button to copy the generate GUID to clipboard
  3. If you want to generate one more GUID, click on the New GUID button

Sample GUID generation output

 // {7154E924-A0AD-4042-A638-AE022911BEF2}
IMPLEMENT_OLECREATE(<<class>>, <<external_name>>, 
0x7154e924, 0xa0ad, 0x4042, 0xa6, 0x38, 0xae, 0x2, 0x29, 0x11, 0xbe, 0xf2);

GUID window

Generate-GUID-in-visual-Studio-IDE

19 July 2015

running an application as ADMIN always

Often you are required to run an application as an Administrator, otherwise, the application might not start at all or it might not work to its full extent. On windows platform, few such examples include but not limited to Visual Studio, Internet Information Services (IIS), etc. To debug an application you need to start visual studio as an ADMIN and for IIS, you need to be an ADMIN even to open it.

Sometimes it so happens that, you start Visual Studio (VS) and when you try to debug you get a warning saying, you cannot debug, because Visual Studio (VS) is not running as an ADMIN. Then you need to exit VS and start again as an Admin. So wouldn't be great if you able to start such app always as an ADMIN by default?.

Below are the steps to run an application as an ADMIN always (on Windows 7 OS). You need to be an Admin on the System to do this changes!

  1. Locate the application EXE. Right Click on the EXE and open the Application Properties window.
  2. Select Compatibility tab and Click on the button, "Change Settings for all Users".
    Running application as ADMIN showing compatibility tab
  3. It opens up another window with options to manage "Compatibility for all users". Under Privilege section, check the option "Run this program as an Administrator" option and save the changes.

After these changes, next time when you start an application, it will start by default an administrator, which definitely will save some time for you!

11 May 2014

why one shouldn't ignore code compile warnings

Recently I was working on a code re factoring; A method in class had duplicated code and wasn't following the DRY principle.

The method under question was building 3 similar objects but of different types. The code looked like below in simple:

public class Car 
{
public string Type{get;set;}
}

public List<Car> BuildCars()
{
  List<Car> cars = new List<Car>();

  // Build car of type 1
   Car type1Car = new Car(){Type=1};
   cars.Add(type1Car);

  // Build car of type 2
   Car type2Car = new Car(){Type=2};
   cars.Add(type2Car );

  // Build car of type 3
   Car type3Car = new Car(){Type=3};
   cars.Add(type3Car);

   return cars;
}

As one can see a lot of code is duplicated in the method BuildCars(). Duplicate code will suffer from below syndromes:

  • Duplicated code will not have good readability as the methods looks lengthy.
  • And also the code isn't maintainable as it is difficult to debug and apply defect fixes.

Hence I started with re factoring of the method. Created a new method BuildCar() like below:

public Car BuildCar(string type)
{
  Car car = new Car() {Type=1};
  return car;
}

Later as part of the refactoring added a new BuildCar() method and used it in the BuildCars(). Hence code become little more readable and more maintainable because of reduction in duplicate code.

public List<Car> BuildCars()
{
   List<Car> cars = new List<Car>();

   Car car= BuildCar(1);
   cars.Add(car);

   car= BuildCar(2);
   cars.Add(car);

   Car car= BuildCar(3);
   cars.Add(car);

   return cars;
}

Started debugging the code and found that cars returned from BuildCars() method are all of type 1!. Something went wrong with refactoring then. It didn't take much time to find the issue and correct it. Although it wasted some time!.

Corrected the BuildCar() method to use the input parameter type and everything started working as expected. The input variable 'type' was never used and 'type' was hardcoded to 1.

public Car BuildCar(string type)
{
  Car car = new Car() {Type=type};
  return car;
}

Then how one can prevent these kinds mistake from happening again?

Visual Studio will give a warning if a variable isn't used by default. So if I had used the Build Warnings window to see the "unused variable" warning then the mistake would have been prevented and there wouldn't have been a defect in my code.

So moral of the story is ALWAYS ENSURE ZERO WARNINGS! in your code.

11 January 2014

visual studio error/exception and resolution

Sometimes while working with Visual Studio IDE (Integrated Development Environment) we encounter exceptions. When such exceptions occur, Visual Studio pops up an Exception dialog displaying little useful information. So Visual Studio IDE users will end up googling and wasting time. Below is the list of Visual Studio exceptions occurred and how can we resolve such exceptions.

  1. Golden rule to get rid of Visual Studio errors:

    When exception encountered and the error dialog is not giving enough clue to resolve the issue, close the Visual Studio application and reopen the solution; Most of the time, restarting Visual Studio will do the trick and solves the unexpected error.

  2. Error / Exception statement-1:

    Recently faced error message, "The parameter is incorrect" while trying to run a new unit test added to a unit test project using the Test View of Visual Studio.

    ---------------------------
    Microsoft Visual Studio
    ---------------------------
    Exception was thrown: The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))

    Solution to fix the exception dialog:

    Were able to run the unit tests after closing Visual Studio and reopening the Test Project.

  3. Error / Exception statement-2:

    Encountered the exception, "data.coverage is invalid or corrupt" while trying to see the Code Coverage for a solution built using Visual Studio.

    ---------------------------
    Microsoft Visual Studio
    ---------------------------
    Code Coverage Analysis engine threw exception(s):
    Coverage file "Solution physical path\TestResults\data.coverage" is invalid or corrupt.

    Solution to fix the exception dialog:

    This issue was happening because of disk space issue. After deleting all the content in "Test Results" folder and freeing up the space, were able to view the code coverage.