// Var.cpp : main project file.
// Example of the Decimal(int, int, int, bool, unsigned char) 
// constructor.
#include "stdafx.h"

using namespace System;

// Get the exception type name; remove the namespace prefix.
String^ GetExceptionType(Exception^ ex)
{
	String^ exceptionType = ex->GetType()->ToString();
	return exceptionType->Substring(exceptionType->LastIndexOf('.') + 1);
}

// Create a Decimal object and display its value.
void CreateDecimal(int low, int mid, int high, bool isNeg, unsigned char scale)
{
	// Format the constructor for display.
	array<Object^>^boxedParams = gcnew array<Object^>(5);
	boxedParams[0] = low;
	boxedParams[1] = mid;
	boxedParams[2] = high;
	boxedParams[3] = isNeg;
	boxedParams[4] = scale;
	String^ ctor = String::Format("Decimal({0}, {1}, {2}, {3}, {4})", boxedParams);
	String^ valOrExc;

	// try-catch exception handling block
	try
	{
		// Construct the Decimal value.
		Decimal decimalNum = Decimal(low,mid,high,isNeg,scale);

		// Format and save the Decimal value.
		valOrExc = decimalNum.ToString();
	}

	catch (Exception^ ex) 
	{
		// Save the exception type if an exception was thrown.
		valOrExc = GetExceptionType(ex);
	}

	// Display the constructor and Decimal value or exception.
	int ctorLen = 76 - valOrExc->Length;
	// Display the data on one line if it will fit.
	if (ctorLen > ctor->Length)
		Console::WriteLine("{0}{1}", ctor->PadRight(ctorLen), valOrExc);
	// Otherwise, display the data on two lines.
	else
	{
		Console::WriteLine("{0}", ctor);
		Console::WriteLine("{0,76}", valOrExc);
	}
}

int main(array<System::String ^> ^args)
{
	Console::WriteLine("This example demonstrates the Decimal(int, int, "
		"int, bool, unsigned char) \nconstructor "
		"generates the following output.\n");
	Console::WriteLine("{0,-38}{1,38}", "Constructor", "Value or Exception");
	Console::WriteLine("{0,-38}{1,38}", "-----------", "------------------");

	// Construct Decimal objects from double values.
	CreateDecimal(0, 0, 0, false, 0);
	CreateDecimal(0, 0, 0, false, 27);
	CreateDecimal(0, 0, 0, true, 0);
	CreateDecimal(1000000000, 0, 0, false, 0);
	CreateDecimal(0, 1000000000, 0, false, 0);
	CreateDecimal(0, 0, 1000000000, false, 0);
	CreateDecimal(1000000000, 1000000000, 1000000000, false, 0);
	CreateDecimal(-1, -1, -1, false, 0);
	CreateDecimal(-1, -1, -1, true, 0);
	CreateDecimal(-1, -1, -1, false, 15);
	CreateDecimal(-1, -1, -1, false, 28);
	CreateDecimal(-1, -1, -1, false, 29);
	CreateDecimal(Int32::MaxValue, 0, 0, false, 18);
	CreateDecimal(Int32::MaxValue, 0, 0, false, 28);
	CreateDecimal(Int32::MaxValue, 0, 0, true, 28);

	return 0;
}

