Skip to main content

Posts

Sudoku Puzzle Generator

Sudoku puzzles can be generated by a switching the rows and columns of a valid puzzle. The switch has to be done between the 123, 456 and 789 rows/columns. ie; 1 cannot be switched with 7 for example. A random pair can be generated and if a loop is run say 50 times, we get a new puzzle. Then the cells can be hidden again randomly. The following code generates the puzzle from a base character string which is converted to a 81 length Char array. The output is the solution as well as the puzzle with blank (or 0s)               Dim stdArray As String = "317849265245736891869512473456398712732164958981257634174925386693481527528673149"         Dim charArray() As Char = stdArray.ToCharArray         Dim rng As New Random         Dim m As Integer         Dim row1 As Integer         Dim row2 As Integer         Dim col1 As Integer         Dim col2 As Integer         Dim i As Integer         Dim cell1 As Integer         Dim cell2 As Integer         Dim tempC A
Recent posts

BitArray 0 and 1s to Hex and back

This code will let you convert an arbitrary string with 0s and 1s only into a Hex value, and back. Why? Maybe you have a Javascript component that renders based on a long array of bits. Lets say var b = "101010101100101001000101011101010010011100000010100110000010001010010011000000000001000010101001110000000001100010100000" The string can be stored in the DB as nvarchar(max) field, but then if you want to reduce the length by 1/8th, you can convert it into a Hexadecimal representation The above would be equivalent to 27029822 930010A9C018A0 Ok, this is the 'bit packing' concept implemented in a very crude way. Anyhow, the output is human readable and probably easier to pass in a JSON The functions are as below     Private Function bitArrayStrtoHex(b As String) As String         Dim sb As New System.Text.StringBuilder         For i As Integer = 0 To b.Length - 1 Step 8             Dim cut8 As String             If b.Length - i <= 8 Then      

HTML Sidebar menu without jQuery

Most examples I found on the net for a responsive sidebar had a reference to jQuery. For two lines of code, I find it an unnecessary overhead. So this is a basic page with no dependencies that has a toggle-able sidebar as well as a media query javascript that hides the sidebar below a breakpoint screen width. Modify as necessary. Maybe tricks could be added such as the one to change the hamburger to X when the sidebar is visible. https://jsbin.com/romiqov/2/edit?html,output <!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8">     <title>SideBar test</title>     <style>         * {             margin: 0;             padding: 0;             font-family:'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;         }         #sidebar {             position: fixed;             width: 200px;             height: 100%;             top: 50px;             left: 0px;             transition

SQL Server - Get columns and data types from all tables

Useful script USE SmartHire GO  SELECT TABLE_NAME, COLUMN_NAME, COLUMNPROPERTY(OBJECT_ID(TABLE_SCHEMA + '.' + TABLE_NAME), COLUMN_NAME, 'ColumnID') AS COLUMN_ID, DATA_TYPE  FROM SmartHire.INFORMATION_SCHEMA.COLUMNS  GO   Output TABLE_NAME COLUMN_NAME COLUMN_ID DATA_TYPE TblBankDetails BankID 1 int TblBankDetails BankName 2 varchar TblContactPreferences ContactPreferenceID 1 int

Modal dialog window using Javascript and CSS

Add this function to your scripts. Call it from a click or other events like button.onlick= function() { showModal("some content to be shown as modal dialog"); function showModal(txt) {     var d = document.getElementById("xf_modal_window"); d && n.parentNode.removeChild(d); var n = document.createElement("div");     n.id = "xf_modal_window", n.className = "modal"; var o = document.createElement("div");     o.id = "er_div_modal_content", o.innerHTML = txt, n.appendChild(o); var t = document.createElement("input");     t.type = "BUTTON", t.value = "close", t.onclick = function () { t.parentNode.parentNode.removeChild(n) }, n.appendChild(t), document.body.appendChild(n) } This to your CSS  .modal {     position: absolute;     left: 0px;     top: 0px;     width: 100%;     height: 100%;     text-align: center;     z-index: 990;     background-color: #808080;    

Getting ENUM values by Reflection

Recently I had this problem, where I had  to get the Items and Values of an Enum within a class. The class name and enum name (as strings) are known and the instance itself has to be created at runtime. i.e; via Reflection Namespace N1abcd .... Public Class TestClass     Public Enum TestEnum         val1 = 20         val2 = 30     End Enum End Class The syntax in VB.net is as below '   System.Type.GetType("Namespace Name" + "." + "Class Name" + "+" + "Enum Name") Dim fieldInfos() As System.Reflection.FieldInfo = System.Type.GetType(" N1abcd .TestClass+TestEnum").GetFields             For Each f As System.Reflection.FieldInfo In fieldInfos                 If f.IsLiteral Then                    MsgBox(f.Name & " : " & CType(f.GetValue(Nothing), Integer) & vbCrLf)                End If     Next 

.Net Serialize and Deserialize Objects to XML and database

Serialization Create an empty instance of your object Dim Obj as New MYOBJECT Dim cn As New SqlConnection(ConnectionString.Text_) Dim cmd As New SqlCommand Dim Trans As SqlTransaction   With cmd                 .Connection = cn                 .Transaction = Trans                 .CommandType = CommandType.StoredProcedure                 .CommandText = "somestoredprocedure"             Dim xMLSerialiser_ As New System.Xml.Serialization.XmlSerializer ( Obj.GetType )                 Dim sWriter As New System.IO.StringWriter                 xMLSerialiser_.Serialize(sWriter, Obj )                 'add xml to database                 .Parameters.AddWithValue("@persistxml", sWriter.ToString) End With cmd.ExecuteNonQuery() Trans.Commit()   DeSerialization Dim Obj as New MYOBJECT cn.Open()             With cmd                 .Connection = cn                 .CommandType = CommandType.Text