Monthly Archives: November 2020

VB.NET || How To Copy All Properties & Fields From One Object To Another Using VB.NET

The following is a module with functions which demonstrates how to copy all properties and fields from one object to another using VB.NET.

The function demonstrated on this page is a generic extension method which uses reflection to copy all the matching properties and fields from one object to another.

This function works on both structure and class object fields and properties.

The two objects do not have to be the same type. Only the matching properties and fields are copied.


1. Copy Properties & Fields

The example below demonstrates the use of ‘Utils.Objects.CopyPropsTo‘ to copy all the matching properties and fields from the source object to the destination object.


2. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


3. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

VB.NET || Universal Object Serializer and Deserializer Using VB.NET

The following is a module with functions which demonstrates how to create a simple universal object serializer and deserializer using VB.NET.

The following functions take in a instance of the abstract classesObjectSerializer‘, and ‘ObjectDeserializer‘. Those classes defines the functionality that is common to all the classes derived from it. They are what carries out the actual serialization and deserialization.

Breaking things up like so makes it easy to have different serializations with only calling one function.

Note: The functions in this module uses code from previous articles which explains how to serialize and deserialize objects.

For those examples of how to serialize and deserialize objects using Json and XML, see below:


1. JsonSerializer

The example below demonstrates the use of ‘Utils.Objects.JsonSerializer‘ to serialize an object to Json.

By changing the second parameter, calling the same function will change its behavior.


2. JsonDeserializer

The example below demonstrates the use of ‘Utils.Objects.JsonDeserializer‘ to deserialize an object from Json.

By changing the second parameter, calling the same function will change its behavior.


3. XmlSerializer

The example below demonstrates the use of ‘Utils.Objects.XmlSerializer‘ to serialize an object to Xml.

By changing the second parameter, calling the same function will change its behavior.


4. XmlDeserializer

The example below demonstrates the use of ‘Utils.Objects.XmlDeserializer‘ to deserialize an object from xml.

By changing the second parameter, calling the same function will change its behavior.


5. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


6. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

VB.NET || How To Send, Post & Process A REST API Web Request Using VB.NET

The following is a module with functions which demonstrates how to send and receive a RESTful web request using VB.NET.

Contents

1. Overview
2. WebRequest - GET
3. WebRequest - POST
4. WebRequest - PUT
5. WebRequest - PATCH
6. WebRequest - DELETE
7. Utils Namespace
8. More Examples


1. Overview

The following functions use System.Net.HttpWebRequest and System.Net.HttpWebResponse to send and process requests. They can be called synchronously or asynchronously. This page will demonstrate using the asynchronous function calls.

The examples on this page will call a test API, and the resulting calls will return Json results.

The Json objects we are sending to the API are hard coded in the examples below. In a real world application, the objects would be serialized first before they are sent over the network, and then deserialized once a response is received. For simplicity, those operations are hard coded.

For examples of how to serialize and deserialize objects using Json and XML, see below:

Note: Don’t forget to include the ‘Utils Namespace‘ before running the examples!


2. WebRequest – GET

The example below demonstrates the use of ‘Utils.WebRequest.Get‘ to execute a GET request on the given url.

The optional function parameter allows you to specify System.Net.HttpWebRequest options, like the UserAgent, Headers etc.


3. WebRequest – POST

The example below demonstrates the use of ‘Utils.WebRequest.Post‘ to execute a POST request on the given url.

The optional function parameter allows you to specify System.Net.HttpWebRequest options, like the UserAgent, Headers etc.


4. WebRequest – PUT

The example below demonstrates the use of ‘Utils.WebRequest.Put‘ to execute a PUT request on the given url.

The optional function parameter allows you to specify System.Net.HttpWebRequest options, like the UserAgent, Headers etc.


5. WebRequest – PATCH

The example below demonstrates the use of ‘Utils.WebRequest.Patch‘ to execute a PATCH request on the given url.

The optional function parameter allows you to specify System.Net.HttpWebRequest options, like the UserAgent, Headers etc.


6. WebRequest – DELETE

The example below demonstrates the use of ‘Utils.WebRequest.Delete‘ to execute a DELETE request on the given url.

The optional function parameter allows you to specify System.Net.HttpWebRequest options, like the UserAgent, Headers etc.


7. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


8. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

VB.NET || How To Check If A String Is A Valid HTTP URL Using VB.NET

The following is a module with functions which demonstrates how to check whether a string is a valid HTTP URL using VB.NET.

Note: The following function only checks for valid url formatting. It does not determine if the address behind the url is valid for navigation.


1. Is Valid URL

The example below demonstrates the use of ‘Utils.Http.IsValidURL‘ to check whether a string is a valid HTTP URL.


2. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


3. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

VB.NET || How To Get, Add, Update & Remove Values From A URL Query String Using VB.NET

The following is a module with functions which demonstrates how to get, add, update and remove parameters from a query string using C#.

The following functions use System.Web to modify the url.

To use the functions in this module, make sure you have a reference to ‘System.Web‘ in your project.

One way to do this is, in your Solution Explorer (where all the files are shown with your project), right click the ‘References‘ folder, click ‘Add Reference‘, then type ‘System.Web‘ in the search box, and add the reference titled System.Web in the results Tab.

Note: Don’t forget to include the ‘Utils Namespace‘ before running the examples!


1. Add – Single Parameter

The example below demonstrates the use of ‘Utils.HttpParams.Add‘ to add a query string parameter to a url.


2. Add – Multiple Parameters

The example below demonstrates the use of ‘Utils.HttpParams.Add‘ to add a query string parameter to a url.


3. Update – Single Parameter

The example below demonstrates the use of ‘Utils.HttpParams.Update‘ to update a query string parameter to a url.


4. Update – Multiple Parameters

The example below demonstrates the use of ‘Utils.HttpParams.Update‘ to update a query string parameter to a url.


5. Remove – Single Parameter

The example below demonstrates the use of ‘Utils.HttpParams.Remove‘ to remove a query string parameter from a url.


6. Remove – Multiple Parameters

The example below demonstrates the use of ‘Utils.HttpParams.Remove‘ to remove multiple query string parameters from a url.


7. Clear – Remove All Parameters

The example below demonstrates the use of ‘Utils.HttpParams.Clear‘ to remove all query string parameters from a url.


8. Get – Get All Parameters

The example below demonstrates the use of ‘Utils.HttpParams.Get‘ to get all query string parameters from a url.


9. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


10. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

VB.NET || How To Serialize & Deserialize JSON Using VB.NET

The following is a module with functions which demonstrates how to serialize and deserialize Json using VB.NET.

The following generic functions use Newtonsoft.Json to serialize and deserialize an object.

Note: To use the functions in this module, make sure you have the ‘Newtonsoft.Json‘ package installed in your project.

One way to do this is, in your Solution Explorer (where all the files are shown with your project), right click the ‘References‘ folder, click ‘Manage NuGet Packages….‘, then type ‘Newtonsoft.Json‘ in the search box, and install the package titled Newtonsoft.Json in the results Tab.

Note: Don’t forget to include the ‘Utils Namespace‘ before running the examples!


1. Serialize – Integer Array

The example below demonstrates the use of ‘Utils.Json.Serialize‘ to serialize an integer array to Json.

The optional function parameter allows you to specify the Newtonsoft.Json.JsonSerializerSettings.


2. Serialize – String List

The example below demonstrates the use of ‘Utils.Json.Serialize‘ to serialize a list of strings to Json.

The optional function parameter allows you to specify the Newtonsoft.Json.JsonSerializerSettings.


3. Serialize – Custom Object List

The example below demonstrates the use of ‘Utils.Json.Serialize‘ to serialize a list of custom objects to Json.

The optional function parameter allows you to specify the Newtonsoft.Json.JsonSerializerSettings.


4. Deserialize – Integer Array

The example below demonstrates the use of ‘Utils.Json.Deserialize‘ to deserialize Json to an integer array.

The optional function parameter allows you to specify the Newtonsoft.Json.JsonSerializerSettings.


5. Deserialize – String List

The example below demonstrates the use of ‘Utils.Json.Deserialize‘ to deserialize Json to a list of strings.

The optional function parameter allows you to specify the Newtonsoft.Json.JsonSerializerSettings.


6. Deserialize – Custom Object List

The example below demonstrates the use of ‘Utils.Json.Deserialize‘ to deserialize Json to a list of objects.

The optional function parameter allows you to specify the Newtonsoft.Json.JsonSerializerSettings.


7. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


8. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

VB.NET || How To Serialize & Deserialize XML Using VB.NET

The following is a module with functions which demonstrates how to serialize and deserialize XML using VB.NET.

The following generic functions use System.Xml.Serialization to serialize and deserialize an object.

Note: Don’t forget to include the ‘Utils Namespace‘ before running the examples!


1. Serialize – Integer Array

The example below demonstrates the use of ‘Utils.Xml.Serialize‘ to serialize an integer array to xml.

The optional function parameter allows you to specify the System.Xml.Serialization.XmlRootAttribute.


2. Serialize – String List

The example below demonstrates the use of ‘Utils.Xml.Serialize‘ to serialize a list of strings to xml.

The optional function parameter allows you to specify the System.Xml.Serialization.XmlRootAttribute.


3. Serialize – Custom Object List

The example below demonstrates the use of ‘Utils.Xml.Serialize‘ to serialize a list of custom objects to xml.

The optional function parameter allows you to specify the System.Xml.Serialization.XmlRootAttribute.


4. Deserialize – Integer Array

The example below demonstrates the use of ‘Utils.Xml.Deserialize‘ to deserialize xml to an integer array.

The optional function parameter allows you to specify the System.Xml.Serialization.XmlRootAttribute.


5. Deserialize – String List

The example below demonstrates the use of ‘Utils.Xml.Deserialize‘ to deserialize xml to a list of strings.

The optional function parameter allows you to specify the System.Xml.Serialization.XmlRootAttribute.


6. Deserialize – Custom Object List

The example below demonstrates the use of ‘Utils.Xml.Deserialize‘ to deserialize xml to a list of objects.

The optional function parameter allows you to specify the System.Xml.Serialization.XmlRootAttribute.


7. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


8. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

VB.NET || How To Split & Batch An Array/List/IEnumerable Into Smaller Sub-Lists Of N Size Using VB.NET

The following is a module with functions which demonstrates how to split/batch an Array/List/IEnumerable into smaller sublists of n size using VB.NET.

This generic extension function uses a simple for loop to group items into batches.


1. Partition – Integer Array

The example below demonstrates the use of ‘Utils.Partition‘ to group an integer array.


2. Partition – String List

The example below demonstrates the use of ‘Utils.Partition‘ to group a list of strings.


3. Partition – Custom Object List

The example below demonstrates the use of ‘Utils.Partition‘ to group a list of custom objects.


4. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


5. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

VB.NET || How To Convert An Array/List/IEnumerable To A DataTable & Convert A DataTable To A List Using VB.NET

The following is a module with functions which demonstrates how to convert an Array/List/IEnumerable to a DataTable, and how to convert a DataTable to a List using VB.NET.

Contents

1. Overview
2. Convert To DataTable - Integer Array
3. Convert To DataTable - List Of Strings
4. Convert To DataTable - List Of Objects
5. Convert To List - Integer DataTable
6. Convert To List - String DataTable
7. Convert To List - Multi-Column DataTable
8. Utils Namespace
9. More Examples


1. Overview

The functions demonstrated on this page are generic extension methods which uses reflection to convert a DataTable to a List(Of T) and an Array/List/IEnumerable to a DataTable.

These functions work on primitive data types, custom structure and class object fields and properties, as well as DBNull.Value, and nullable arrays and lists.

Note: Don’t forget to include the ‘Utils Namespace‘ before running the examples!


2. Convert To DataTable – Integer Array

The example below demonstrates the use of ‘Utils.Collections.ToDataTable‘ to convert a nullable integer array to a datatable.

When converting nullable types, their value is represented as DBNull in the returned table.


3. Convert To DataTable – List Of Strings

The example below demonstrates the use of ‘Utils.Collections.ToDataTable‘ to convert a list of strings to a datatable.

When converting nullable types, their value is represented as DBNull in the returned table.


4. Convert To DataTable – List Of Objects

The example below demonstrates the use of ‘Utils.Collections.ToDataTable‘ to convert a list of custom objects to a datatable.

A class is used in this example, but a custom structure also works here as well.

When converting nullable types, their value is represented as DBNull in the returned table.


5. Convert To List – Integer DataTable

The example below demonstrates the use of ‘Utils.Collections.ToList‘ to convert a datatable to a List(Of Integer?).

When converting DBNull types, their value is represented as ‘Nothing‘ in the returned list.


6. Convert To List – String DataTable

The example below demonstrates the use of ‘Utils.Collections.ToList‘ to convert a datatable to a List(Of String).

When converting DBNull types, their value is represented as ‘Nothing‘ in the returned list.


7. Convert To List – Multi-Column DataTable

The example below demonstrates the use of ‘Utils.Collections.ToList‘ to convert a datatable to a List(Of Object).

A class is used in this example, but a custom structure also works here as well.

When converting DBNull types, their value is represented as ‘Nothing‘ in the returned list.


8. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


9. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

VB.NET || How To Set & Get The Description Of An Enum Using VB.NET

The following is a module with functions which demonstrates how to set and get the description of an enum using VB.NET.

This function uses the DescriptionAttribute to get the description of an Enum element. If a description attribute is not specified, the string value of the Enum element is returned.


1. Get Description

The example below demonstrates the use of ‘Utils.GetDescription‘ to get the description of enum elements.


2. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


3. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

VB.NET || How To Iterate & Get The Values Of An Enum Using VB.NET

The following is a module with functions which demonstrates how to iterate and get the values of an enum using VB.NET.

This function is a generic wrapper for the Enum.GetValues function.


1. Get Values – Basic Enum

The example below demonstrates the use of ‘Utils.GetEnumValues‘ to get the values of a simple Enum.


2. Get Values – Numbered Enum

The example below demonstrates the use of ‘Utils.GetEnumValues‘ to get the values of a numbered Enum.


3. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


4. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

VB.NET || How To Resize & Rotate Image, Convert Image To Byte Array, Change Image Format, & Fix Image Orientation Using VB.NET

The following is a module with functions which demonstrates how to resize an image, rotate an image to a specific angle, convert an image to a byte array, change an image format, and fix an image orientation Using VB.NET.

Contents

1. Overview
2. Resize & Rotate - Image
3. Resize & Rotate - Byte Array
4. Resize & Rotate - Memory Stream
5. Convert Image To Byte Array
6. Change Image Format
7. Fix Image Orientation
8. Utils Namespace
9. More Examples


1. Overview

To use the functions in this module, make sure you have a reference to ‘System.Drawing‘ in your project.

One way to do this is, in your Solution Explorer (where all the files are shown with your project), right click the ‘References‘ folder, click ‘Add References‘, then type ‘System.Drawing‘ in the search box, and add a reference to System.Drawing in the results Tab.

Note: Don’t forget to include the ‘Utils Namespace‘ before running the examples!


2. Resize & Rotate – Image

The example below demonstrates the use of ‘Utils.Image.Resize‘ and ‘Utils.Image.Rotate‘ to resize and rotate an image object.

Resizing and rotating an image returns a newly created image object.


3. Resize & Rotate – Byte Array

The example below demonstrates the use of ‘Utils.Image.Resize‘ and ‘Utils.Image.Rotate‘ to resize and rotate a byte array.

Resizing and rotating an image returns a newly created image object.


4. Resize & Rotate – Memory Stream

The example below demonstrates the use of ‘Utils.Image.Resize‘ and ‘Utils.Image.Rotate‘ to resize and rotate a memory stream.

Resizing and rotating an image returns a newly created image object.


5. Convert Image To Byte Array

The example below demonstrates the use of ‘Utils.Image.ToArray‘ to convert an image object to a byte array.

The optional function parameter allows you to specify the image format.


6. Change Image Format

The example below demonstrates the use of ‘Utils.Image.ConvertFormat‘ to convert the image object raw format to another format.

Converting an images format returns a newly created image object.


7. Fix Image Orientation

The example below demonstrates the use of ‘Utils.Image.FixOrientation‘ to update the image object to reflect its Exif orientation.

When you view an image in a Photo Viewer, it automatically corrects image orientation if it has Exif orientation data.

Utils.Image.FixOrientation‘ makes it so the loaded image object reflects the correct orientation when rendered according to its Exif orientation data.


8. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


9. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

VB.NET || How To Convert A String To Byte Array & Byte Array To String Using VB.NET

The following is a module with functions which demonstrates how to convert a string to a byte array and a byte array to a string using VB.NET.


1. String To Byte Array

The example below demonstrates the use of ‘Utils.GetBytes‘ to convert a string to a byte array. The optional parameter allows to specify the encoding.


2. Byte Array To String

The example below demonstrates the use of ‘Utils.GetString‘ to convert a byte array to a string. The optional parameter allows to specify the encoding.


3. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


4. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

VB.NET || How To Save, Open & Read File As A Byte Array & Memory Stream Using VB.NET

The following is a module with functions which demonstrates how to save, open and read a file as a byte array and memory stream using VB.NET.


1. Read File – Byte Array

The example below demonstrates the use of ‘Utils.ReadFile‘ to read a file as a byte array.


2. Read File – Memory Stream

The example below demonstrates the use of ‘Utils.ReadFile‘ to read a file as a memory stream.


3. Save File – Byte Array

The example below demonstrates the use of ‘Utils.SaveFile‘ to save the contents of a byte array to a file.


4. Save File – Memory Stream

The example below demonstrates the use of ‘Utils.SaveFile‘ to save the contents of a memory stream to a file.


5. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


6. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.