Posts

Showing posts from 2022

Simple Walkthrough on Visual Studio Code Snippets

Image
  I guess most of the Software Developers might have used or aware of  the Code Snippet feature in Visual Studio which helps us to write  code much faster. Did any of us try to explore how Code  Snippets work or in other words how the shortcut gets converted to extensive code? Let’s explore in this post how Code Snippet work and try to see where  the code is defined for a particular Code Snippet. Exploring Existing Code Snippets First lets try to see all the existing snippets which comes by default in  Visual Studio. This can be seen by opening the Code Snippets  Manager by invoking the shortcut key(CTRL + K,CTRL +   B)  or go to Tools -> Code Snippets Manager as shown in the figure below: Once the Code Snippets Manager comes up, it will list down the  available Code Snippets which are preloaded with the manager as  shown below: Locating Code Snippet File To Understand the structure of the Code Snippet let us try...

Visual Studio Extension Development | Code Analyzer - A Complete Guide

Image
  Isn't it really cool if we could customize the Intellisense/Analyzer rule so that we can add our own analyzer rules and code fixes for the same? Yes, its possible with the help of Visual Studio Extension development toolkit. Lets walk through the steps involved for creating your own code analyzer with code fixes as visual studio extension project and then let us see the analyzer in action. Installing Necessary Toolset for Visual Studio Extension Development First step is to install the necessary tool set from the Visual Studio Installer. For this demo I am using Visual Studio 2022. Launch the Visual Studio Installer from the programs and Select the Visual Studio Extension Development from other Toolsets if not already checked as shown in the figure below and click on Modify   Creating New Analyzer Project with Code Fix in Visual Studio Once its successfully installed and updated, Project templates would be available from the Extension. Create a new project with the " Analyze...

More About Null Pointer Exception in C++

Image
  We all might have encountered with the "infamous" Null Pointer Exception or Access Violation Exception during our development in C++.These seems to be pretty annoying especially when the code becomes complex and involves different modules. Here we are going to have a closer look at Null Pointer case and try to understand or decode what causes this exception with an example. Must Read : Memory Leak in C++ Understanding Null Pointer Exception Scenario Below code presents an example: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 class Classic { public: string getName() const { return m_name; } void print() { cout << "Print called" ; } private: string m_name; }; int main () { Classic * c = nullptr; c -> print(); // did not throw exception string name = c -> getName(); //Threw null pointer exception cin.get(); return 0 ; } As shown in above code a Class Classic is defined with ...

Easy way to do Dynamic Type Casting using Reflection in C#. | Converting Type variable to Type

Image
  Some times we might have come across situations where in we are provided with the type information of the object to be instantiated at runtime. In those cases we use the type information to create an object using Reflection, but unfortunately we will not be able to type cast to the desired type since the type information is available to us as a variable. In those cases we can make use of a generic Cast method and invoke that method during runtime with the help of Reflection. This is also called as Dynamic Type Casting using Reflection. We can see this with the help of an C# code example as shown below: Code Example for Dynamic Type Casting 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 using System ; using System .Collections; using System .Collections.Generic; using System .IO; using System .Li...

Some Interesting and Useful SQL Commands

Image
  SQL or in other words Structured Query Language is an essential skill which every developer would like to have in their arsenal. Its a powerful language to interact with SQL Database. Despite its popularity lot of developers struggle to use some basic commands. Here we are going to see some interesting and Useful SQL commands and their use with the help of an example. Let me state the problem statement first and then we can try to solve the problem using SQL Query. Problem Statement We are given a table which describes the Train Departure Timings from different stations as shown in the figure below: We are asked to return the results of second most recently departed train from each station. So from the example table above the result would contain the following records: Lets see how we can get the result using SQL. Solution First of all we need to group the records based on the station name. Along with that we need to sort each group based on the departure timings. In this case we...

How to Validate JSON in C# Effortlessly using JSON Schema file?

Image
  There are situations where we may need to validate JSON data which we are receiving from external sources. This is done to ensure data integrity. This can be done easily with the help of a JSON Schema file which lists down the constraints and the template of JSON data. Validating JSON using JSON Schema in C# In this post we are going to see with an example, How we can validate the JSON data against the JSON Schema we are using. For this I have created a JSON Schema which lays down the template for the JSON data along with the constraints for the properties of JSON data. Next is to create the JSON data, for this demo, I have hardcoded the JSON data as a string. Below code demonstrates these in action: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 using Newtonsoft.Json.Linq ; using Newtonsoft.Json.Schema ; using Syst...

How to Generate JSON from JSON Schema File in .Net C#?

Image
  Many a times we may have the JSON Schema file describing the JSON content type and its format. Our requirement may be to generate a JSON file from the Schema with the default values for the properties populated from the Schema file. Here we are going to see how this can be done using a simple static utility class in .Net. Generating Default JSON from JSON Schema file For this example I have created a Console Application in C# .Net Framework, and in the Program.cs file I have hardcoded the JSON Schema which acts as our input to generate the JSON. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 ...

How to Check Memory Leaks From Running the Application using UMDH?

While running some applications we might have noticed the memory resource consumption going high, This is all fine if the memory gets released when the application is stopped or killed. But what if the memory consumption does not come down even after the application is killed or stopped? This is when we say that Memory Leaks are present in the application. Tricky part is how can we identify them? For a simple application finding the root cause can be tricky no need to mention how challenging it would be to find the root cause in the case of large commercial application which is composed of several modules. In these situations UMDH can come to the rescue by pointing out the memory allocation and deallocation by each modules present in the process and giving hints where it might be going wrong. UMDH is typically used in situations where we have to find memory leak in the execution path of some action being performed. Following are the steps that needs to be done for narrowing down to...

How to Auto Generate CMakeList file and Build C++ project in VS Code?

Image
Introduction   VS Code is a lightweight fast and easy to use editor for developing software in any language. Traditionally most of us use Visual Studio for our development purposes especially if we are developing in C++. One of the Key advantage while using VS Code to develop applications in C++ is that developer can solely focus on creating source file and these can be bundled in a project or solution file using CMake with any compiler and build configuration. Here we are going to see step by step how to Auto generate CMakelist file in VS Code which specifies the source files that needs to be bundled together and the build configuration etc. Installing CMake Tool Extension Before we get started we need to install extension for CMake Tools in VS Code. I Have installed extension from Microsoft for this purpose as shown in the figure below:   Create a sample C++ file with some code as shown in the figure below in the folder which is loaded in VS Code Next step is to use Com...

Simple In-Memory Caching in .Net with Apache Ignite

Image
  In the Last Post we have seen how to setup Apache Ignite Server as an independent process  . Now we will see how to use the Apache Ignite as an In-Memory Cache in .NET. We will see through an example how to write Clients which knows how to talk with the Apache Ignite Server. Installing Necessary Nuget Packages Before starting off we have to install Apache.Ignite  NuGet package to the Console Application that we have created which is required to enable Apache Ignite functionality. This can be installed through Visual studio "Manage NuGet Packages" tool. Once the NuGet package is installed for the project we have to write clients which can access the Apache Ignite cache store to "Put" and "Get" data. Basically Apache Ignite will store the data as Key Value Pair. Code to access the cache from the client is shown below: Code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 using Apache.Ignite.C...