Archive for the ‘Development’ Category.
September 22, 2009, 4:41 am
WhyMyVariableAndObjectNamesInMyCodeAreSoLong
Reason 1 – One of the main reasons that variable names have been shortened in the past are because you have to type them and the longer they are the longer they take to type. However, with today’s Integrated Development Environments (IDEs) you only have to type a few characters and then hit tab or enter and it finishes the variables for you. So this reason for short variable names has become a non-factor.
Reason 2 – The longer the variable name, the better documented your code is. Lets face it, there are those that demand documentation and those that argue that code should be so well written it doesn’t need documentation. So if I have a variable that represents the time it takes to drive to work, I will go ahead and make the variable name, theTimeItTakesToDriveToWork
.
September 22, 2009, 4:21 am
I am not going to explain the Tower of Hanoi here. I assume if you are reading this you know what you are looking for. Otherwise, read this:
http://en.wikipedia.org/wiki/Tower_of_Hanoi
Main.cpp
#include <iostream>
#include "TowerOfHanoi.h"
int main()
{
TowerOfHanoi *toh = new TowerOfHanoi(true);
std::cout << toh->toString();
toh->solve();
system("pause");
}
TowerOfHanoi.h
#include <iostream>
#include <string>
#include <sstream>
#include <string>
#define leftPeg 1
#define middlePeg 2
#define rightPeg 3
class TowerOfHanoi
{
public:
// Constructors
TowerOfHanoi(bool inPromptUser);
TowerOfHanoi(int inNumberOfRings);
// Destructor
~TowerOfHanoi();
// Other functions
void solve();
// Standard functions
std::string toString();
bool equals(TowerOfHanoi inTowerOfHanoi);
private:
void initializeClass(int inNumberOfRings);
void runRecursiveAlgorithm(int inNumberOfRings, int mPegOriginallyHoldingRing, int inTemporaryPeg, int inDestinationPeg);
std::string intToString(int i);
int power(int inInteger, int inExponent);
std::string getTabs();
int mNumberOfRings;
int mPegOriginallyHoldingRing;
int mDestinationPeg;
int mTemporyPeg;
int mNumberOfStepsToSolve;
int mStepCounter;
};
TowerOfHanoi.cpp
#include "TowerOfHanoi.h"
#include <iostream>
#include <string>
// Constructors
TowerOfHanoi::TowerOfHanoi(bool inPromptUser)
{
int tmpNumberOfRings;
std::cout << std::endl << "How many rings? [1 - 10] " << std::endl;
std::cin >> tmpNumberOfRings;
initializeClass(tmpNumberOfRings);
}
TowerOfHanoi::TowerOfHanoi(int inNumberOfRings)
{
initializeClass(inNumberOfRings);
}
// Destructor
TowerOfHanoi::~TowerOfHanoi()
{
}
// Other functions
void TowerOfHanoi::solve()
{
mStepCounter = 0;
runRecursiveAlgorithm(mNumberOfRings, mPegOriginallyHoldingRing, mTemporyPeg, mDestinationPeg);
}
// Private functions
void TowerOfHanoi::initializeClass(int inNumberOfRings)
{
mNumberOfRings = inNumberOfRings;
mNumberOfStepsToSolve = power(2, inNumberOfRings) - 1;
mPegOriginallyHoldingRing = leftPeg;
mDestinationPeg = rightPeg;
mTemporyPeg = middlePeg;
mStepCounter = 0;
}
void TowerOfHanoi::runRecursiveAlgorithm(int inNumberOfRings, int mPegOriginallyHoldingRing, int inTemporaryPeg, int inDestinationPeg)
{
std::string tabs = "\t\t";
if (inNumberOfRings == 1)
{
std::cout << "Step " << ++mStepCounter << getTabs() << mPegOriginallyHoldingRing << " > " << inDestinationPeg << std::endl;
}
else
{
runRecursiveAlgorithm(inNumberOfRings - 1, mPegOriginallyHoldingRing, inDestinationPeg, inTemporaryPeg);
std::cout << "Step " << ++mStepCounter << getTabs() << mPegOriginallyHoldingRing << " > " << inDestinationPeg << std::endl;
runRecursiveAlgorithm(inNumberOfRings - 1, inTemporaryPeg, mPegOriginallyHoldingRing, inDestinationPeg);
}
}
std::string TowerOfHanoi::getTabs()
{
if (mStepCounter > 99)
{
return "\t";
}
return "\t\t";
}
// Standard functions
std::string TowerOfHanoi::intToString(int i)
{
std::stringstream ss;
std::string s;
ss << i;
s = ss.str();
return s;
}
int TowerOfHanoi::power(int inInteger, int inExponent)
{
int retVal = 1;
for (int i = 0; i < inExponent; i++)
{
retVal *= inInteger;
}
return retVal;
}
// Standard Functions
std::string TowerOfHanoi::toString()
{
return "Tower of Hanoi with " + intToString(mNumberOfRings) + " rings.\n";
}
bool TowerOfHanoi::equals(TowerOfHanoi inTowerOfHanoi)
{
if (this->mNumberOfRings == inTowerOfHanoi.mNumberOfRings)
{
return true;
}
return false;
}
September 22, 2009, 4:01 am
The Unary Scope Resolution Operator is used to access a global variable when a local variable of the same name exists.
For example, look at the following code
#include
using namespace std;
int number = 100;
int main()
{
double number = 27.9;
int a = number;
// The global variable ::number is intended.
// This line should be: int a = ::number;
cout << a;
cout << number;
cout << ::number;
}
[/sourcecode]
Question
What is the value of A going to be? 27.9 or 100?
Answer
Neither, it will be 27.
Explanation
One can assume that because variable a
is an int
and the global variable number
is an int
, that the global variable ::number
was intended over the local variable number
.
However, the local variable number
that is a double
was used.
Since variable a
is an int
, the value of the local variable number
is casted to an int
when assigned, so 27.9 becomes 27.
This can lead to difficult to solve bugs. The value should be 100 but is 27.
Looking at the code, this doesn’t look obviously wrong.
If a developer makes it a practice to always preface global variables with the Unary Scope Resolution Operator, such bugs can be avoided.
September 21, 2009, 7:24 pm
How do I get the number of rows returned from a SQL Query in C#?
Having used other languages where this is much simpler, I was surprised at how “not simple” this was in C#. I expected it to be a little more complex than in some scripting language such as PHP, but it was way more complex.
Here is how I do it:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace CountRows
{
class Program
{
static void Main(string[] args)
{
// Create a string to hold the database connection string
string sdwConnectionString = @"Data Source = ServerName; user id=UserName; password=P@sswd!; Initial Catalog = DatabaseName;";
// Create a string to hold the database connection string
string query = "SELECT * FROM MyTable";
// Pass both strings to a new SqlCommand object.
SqlCommand queryCommand = new SqlCommand(query, sdwDBConnection);
// Create a SqlDataReader
SqlDataReader queryCommandReader = queryCommand.ExecuteReader();
// Create a DataTable object to hold all the data returned by the query.
DataTable dataTable = new DataTable();
dataTable.Load(queryCommandReader);
// The DataTable object has a nice DataTable.Rows.Count property that returns the row count.
int rowCount = rowCount = dataTable.Rows.Count;
}
}
}
Now doing it this way, you also have the data available in the DataTable dataTable
object so you don’t have to go to the database and get it again.
Return to ADO.NET and Database with C#
September 21, 2009, 6:26 pm
How I get the number of rows in a table using C#?
Here is a step by step:
Step 1 – Create a new class: (attached click here: SQLTableRowCounter.cs)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace SQLTableRowCounter
{
class SQLTableRowCounter
{
private string mCountQuery;
private SqlConnection mConnection;
private int mNumberOfRows;
public SQLTableRowCounter(String inTableName, SqlConnection inConnection)
{
mCountQuery = "SELECT COUNT(*) FROM " + inTableName;
mConnection = inConnection;
mConnection.Open();
SqlCommand mCountQueryCommand = new SqlCommand(mCountQuery, mConnection);
mNumberOfRows = (int)mCountQueryCommand.ExecuteScalar();
}
public int NumberOfRows
{
get { return mNumberOfRows; }
set { mNumberOfRows = value; }
}
}
}
Step 2 – Now create the object and get the value:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace SQLTableRowCounter
{
class Program
{
static void Main(string[] args)
{
string connectionString = @"Data Source = ServerName; user id=UserName; password=P@sswd!; Initial Catalog = DatabaseName;";
SqlConnection connection = new SqlConnection(connectionString);
SQLTableRowCounter qrc = new SQLTableRowCounter("TableName", connection);
int numRows = qrc.NumberOfRows;
}
}
}
September 21, 2009, 6:08 pm
Hey all,
I have had a computer since 1984 when I was 7. I have been a computer geek most of my life. I have ten years of intense technical support experience with computer operating systems, networking equipment, and desktop management.
I am currently a Systems Analyst / Developer for LANDesk.
I know FreeBSD, Linux, Windows. I have worked with the following languages: C++, C#, Java, PHP and dabbled with a couple others.
I have worked with the following database: MySQL, MS SQL, Postgresql.
I need a place to store my knowledge. I need a location to store my knowledge that I am sure will not disappear. If I find a good knowledge source, the page may change or the site may be discontinued. Or I may never find a good source and I have to figure something out through trial an error, and a text file on my laptop is probably going to get lost and it also doesn’t help anybody else out.
So I hope this site eventually helps you as much as it helps me.
Thanks,
Rhyous (Jared Barneck)