More on C# in Front Office
By Xing Zhou
()
About this ebook
This is NOT the 2nd Edition of the C# in Front Office. Rather, it extends on the contents in the first book.
Read more from Xing Zhou
Trade Stock Options Rating: 0 out of 5 stars0 ratingsBrain Teaser (2nd Edition) Rating: 0 out of 5 stars0 ratings
Related to More on C# in Front Office
Related ebooks
Pivot Tables for everyone. From simple tables to Power-Pivot: Useful guide for creating Pivot Tables in Excel Rating: 0 out of 5 stars0 ratingsExcel Functions and Formula Combinations Rating: 0 out of 5 stars0 ratingsIan Talks Python A-Z Rating: 0 out of 5 stars0 ratingsExcel 2007 VBA Programmer's Reference Rating: 5 out of 5 stars5/5Software Design Simplified Rating: 0 out of 5 stars0 ratingsC# in Front Office Rating: 0 out of 5 stars0 ratingsLearn Programming Using C# Rating: 0 out of 5 stars0 ratingsLearn Excel in 24 Hours Rating: 4 out of 5 stars4/5Learning Excel Made Easier Rating: 0 out of 5 stars0 ratingsData Structures and Algorithm Analysis in C++, Third Edition Rating: 5 out of 5 stars5/5C++ Programming: Effective Practices and Techniques Rating: 0 out of 5 stars0 ratingsExcel 101: A Beginner's Guide for Mastering the Quintessence of Excel 2010-2019 in no time! Rating: 0 out of 5 stars0 ratingsExcel VBA: A Beginners' Guide Rating: 4 out of 5 stars4/5Upgrading your skills with Access Rating: 0 out of 5 stars0 ratingsMastering Data Structures and Algorithms in Python & Java Rating: 0 out of 5 stars0 ratingsGETTING STARTED WITH SQL: Exercises with PhpMyAdmin and MySQL Rating: 0 out of 5 stars0 ratingsC Programming Language Essentials Rating: 4 out of 5 stars4/5Computer Algebra: Fundamentals and Applications Rating: 0 out of 5 stars0 ratingsAdvanced C++ Interview Questions You'll Most Likely Be Asked Rating: 0 out of 5 stars0 ratingsMicrosoft Excel Functions Vol 1 Rating: 2 out of 5 stars2/5Microsoft Excel Functions Vol 1: 1 Rating: 1 out of 5 stars1/5Learning Open Office: Calc & Base Rating: 0 out of 5 stars0 ratingsC Programming Concepts Rating: 0 out of 5 stars0 ratingsSpecial Techniques in Excel Rating: 0 out of 5 stars0 ratingsUpdate to Modern C++ Rating: 0 out of 5 stars0 ratingsADVANCED DATA STRUCTURES FOR ALGORITHMS: Mastering Complex Data Structures for Algorithmic Problem-Solving (2024) Rating: 0 out of 5 stars0 ratingsBasic Excel 2023: An Essential Guide to Foundational Excel Rating: 0 out of 5 stars0 ratingsMastering Excel: The Complete Guide to All Excel Formulas Rating: 0 out of 5 stars0 ratings
Programming For You
Python: Learn Python in 24 Hours Rating: 4 out of 5 stars4/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5Learn Algorithmic Trading: Build and deploy algorithmic trading systems and strategies using Python and advanced data analysis Rating: 0 out of 5 stars0 ratingsSQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5Learn Python in 10 Minutes Rating: 4 out of 5 stars4/5SQL All-in-One For Dummies Rating: 3 out of 5 stars3/5Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5Python: For Beginners A Crash Course Guide To Learn Python in 1 Week Rating: 4 out of 5 stars4/5Python Machine Learning By Example Rating: 4 out of 5 stars4/5PYTHON: Practical Python Programming For Beginners & Experts With Hands-on Project Rating: 5 out of 5 stars5/5Python Data Structures and Algorithms Rating: 5 out of 5 stars5/5TensorFlow in 1 Day: Make your own Neural Network Rating: 4 out of 5 stars4/5JavaScript All-in-One For Dummies Rating: 5 out of 5 stars5/5HTML & CSS: Learn the Fundaments in 7 Days Rating: 4 out of 5 stars4/5Principles of Data Science Rating: 4 out of 5 stars4/5C Programming for Beginners: Your Guide to Easily Learn C Programming In 7 Days Rating: 4 out of 5 stars4/5Python Games from Zero to Proficiency (Beginner): Python Games From Zero to Proficiency, #1 Rating: 0 out of 5 stars0 ratingsModern C++ Programming Cookbook Rating: 5 out of 5 stars5/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5Learn JavaScript in 24 Hours Rating: 3 out of 5 stars3/5Microservices Architecture Handbook: Non-Programmer's Guide for Building Microservices Rating: 4 out of 5 stars4/5
Reviews for More on C# in Front Office
0 ratings0 reviews
Book preview
More on C# in Front Office - Xing Zhou
More on C# in Front Office
- Advanced C# in Practice
Xing ZHOU
All rights reserved: Xing Zhou ([email protected])
This book is NOT the 2nd edition of the previous book C# in Front Office (ISBN: 144215358X). Instead, it covers many new topics and has lots of additional information. All contents in this book are chosen based on feedbacks, comments and questions from readers of the previous book.
Thanks for your interest in my book.
Table of Contents
1 More on Excel UDF
1.1 Process Vector / Matrix
1.1.1 Vector / Matrix as Input
1.1.2 Vector / Matrix as Output
1.1.3 Convert to a Collection of Key-Value Pairs
1.2 Process Date
1.2.1 Overview
1.2.2 A Practical Example - DateAdd()
1.2.3 Other Features (Business Calendar etc)
1.3 Hide Unwanted
Functions in Excel Function Wizard
1.3.1 A Simple Solution
1.3.2 A Better Solution
1.4 Develop C# UDF without Visual Studio
2 More on Scripting
2.1 Debugging Dynamic Script
2.1.1 Introduction
2.1.1. Script Debugging in C++
2.1.2. Script Debugging in C#
2.1.3. MyUtil.StartDebug()
2.1.4. Revised Script Engine Implementation
2.2 Multiple Script Source Files
3 More on C# / C++ Integration
3.1 Debug C#/C++ Hybrid Applications
4 More on Threading
4.1 Protect Critical Resource
4.1.1 Race Condition
4.1.2 Using lock statement
4.1.3 The Interlocked class
4.1.4 The Monitor class
4.1.5 Thread Re-entry
4.1.6 Reader Lock and Writer Lock
4.2 Avoid Deadlock
4.3 Lazy Caching and Double-Checking
4.4 Eliminate Unnecessary Locking - A Practical Example
4.5 Double-Buffer Pattern - A Practical Example
4.6 Thread Synchronization
4.6.1 Share Data
4.6.2 Synchronize Execution Flow
4.7 Monte Carlo
4.8 Inter-Process Synchronization
4.8.1 Synchronize Execution Flow
4.8.2 Share Data
4.9 Debug a Multithreaded Application
5 Develop Client Applications
5.1 Overview and Design Guideline
5.2 C# Solution Structure
5.3 C# Application as Client Application
5.4 VB Application as Client Application
5.5 Excel as Client Application
5.6 Web Client
6 Debugging Tips
6.1 Display Custom Debug Information
2.1.5. Method 1 - ToString()
2.1.6. Method 2 - DebuggerDisplay Attribute
2.1.7. Method 3 - autoexp.cs
Index of Examples
Index of Figures
1 More on Excel UDF
In the previous book, C# in Front Office, we have discussed how to develop Excel user defined functions (UDFs) using C#. It is based on COM automation technology which is different from the traditional C++ based approach. Compared with the traditional C++ based approach, the C# based approach is clearly much simpler, more flexible and more intuitive. What is also important is that the C# based approach requires little to none extra learning curve beyond regular
C# development skills. This means that any C# developer who can develop and debug regular C# programs can readily develop and debug C# based Excel UDFs. This feature can easily be translated to higher productivity in practice.
In this chapter, we will discuss some additional details and topics that are related to UDF development but have not been covered in the previous book. These contents are chosen based on readers’ feedback, comments and questions that have been received since publish of the previous book.
In order to keep this book concise and avoid unnecessary duplication, we will not discuss basic techniques of developing a UDF library using C# here. It is assumed that readers have already gained some practical working knowledge of developing Excel UDF libraries using C#. Otherwise, it is strongly suggested that readers should acquire such knowledge before continuing on this chapter. This can be done by either reading the 1st chapter of the previous book or taking some relevant training.
1.1 Process Vector / Matrix
1.1.1 Vector / Matrix as Input
Vectors and matrices are basic but very important mathematic data types. They are widely used in front office analytic libraries. As such, it is a natural requirement to process these two data types in UDFs. In this section, we will discuss some practical details and tricks about processing vectors and matrices in C# based UDFs.
On an Excel worksheet, a single-row or a single-column range is a natural representation of a vector; and a multi-row-multi-column range is a natural representation of a matrix. Thus it is intuitive to use Excel’s built-in Range[1] class as the parameter data type in a UDF to represent vector and/or matrix inputs. In fact, the Range class has all the usual properties that we will normally expect from a vector or a matrix class. These include Rows, Columns and so on. It also has a Cells iteration interface for looping through all its cells within this Range object. All these give us a clearly defined simple interface to deal with a Range object. Therefore, processing vectors/matrices as inputs is very simple and intuitive in C#.
However it is important to note how we should retrieve a numeric value from an individual cell using C#. For performance reason, we should use direct type cast instead of parsing the cell contents as text string first. This is because even though the Value2 property is declared as object type and has a default ToString() method, it is actually a double if the cell contains a number. Because it is a double, we can safely use direct type cast.
This means that we should NOT do something like the following:
double value = Double.Parse(cell.Value2.ToString());
Instead, we shall use:
double value = (double)(cell.Value2);
In reality, there will be a noticeable performance difference between these two approaches if we have to process large number of cells.
The following is a very simple example which shows how to handle vectors and/or matrices as inputs. And, in particular, it demonstrates how to retrieve numeric values using the direct type cast as we have just discussed. As the code uses the Cells iteration interface, it works for both single-row/column and multi-row/column inputs (i.e. both a vector and a matrix).
Please note that the ExcelUDFBase class has been fully discussed in the previous book C# in Front Office. It contains all the necessary infrastructural functions to make the class usable as a UDF class. A copy of its source code can be freely downloaded from that book’s website[2].
[ComVisible(true)
[ClassInterface(ClassInterfaceType.AutoDual)]
[Guid(F2719620-DD15-4e83-9E77-EFD27CF89EDE
)]
public class VectorAndMatrixSample : ExcelUDFBase
{
public object MySum(MsExcel.Range Input)
{
try
{
double sum = 0;
foreach (MsExcel.Range cell in Input)
{
sum += (double)(cell.Value2);
}
return sum;
}
catch (Exception err_)
{
return err_.ToString();
}
}
}
Example 1 Process Vectors and Matrices
1.1.2 Vector / Matrix as Output
To generate a vector or a matrix as a UDF’s output is the same thing as returning an object array. Developing an array function has been discussed in section 1.8 of the previous book C# in Front Office. Thus we will not repeat in details here. In a nutshell, an array function is a regular function that returns an object[,] in a C# implementation. If we enter such formula as an array function in an Excel cell (i.e. by pressing <ctrl> + <shift> + <enter>), the output will look like a vector or a matrix on an Excel spreadsheet depending on the dimensions of return data. The previous book has also discussed the technique to ensure the output range has the same dimension as that of the returning vector or matrix. It is very useful in practice not only to beautify the output (e.g. blanketing out additional cells instead of displaying N/A when the given Excel range is bigger than the result vector/matrix) but also to avoid potential misleading situation (e.g. missing output data when the given range is smaller than the result vector/matrix).
1.1.3 Convert to a Collection of Key-Value Pairs
Quite often, we need a collection of key-value pairs. A collection of key-value pairs is often used to represent a set of parameters where keys are parameter names and values are parameter values.
On an Excel spreadsheet, we usually use a 2-row or a 2-column range as such inputs. One row (or column) contains parameter names (i.e. the keys) and the other contains corresponding parameter values (i.e. the values). The follow figure shows a pseudo example.
As we can imagine, converting such a range