A while ago i was asked to switch a group of words in the reverse order. This was a simple task until they added the caveat that i could not create a new string object while switching the order of the words. I searched the web and i really could not find a straight and complete way to accomplish this. So i came out with my own way. Not to say that it is the best way, but it is a way to do it. I kept things simple to make it easier to understand.
The first thing i do is create the string of words using the StringBuilder. I defined a few variables to track the characters in the string. My approach here was to first flip the whole string of characters. In order to accomplish this i use a while loop and i start switching characters from the back to the front and from the front to the back until i get to the middle of the string. It is important to stop in the middle or you will end up with the original string.
The next step is where the actual work happens. Line 35 and 36 include a logic to look for the instance of blank spaces. The blank spaces are the delimiters for each work in the full string.
Lines 38-41 determines where the next word will end. This is necessary since the last word must be determine.
The while loop in lines 43-49 flip the letters in each word. After the flipping is completed for the word, the outer loop moves forward to the next group of characters to continue the flipping process.
The final result illustrates flipping a group of words in the opposite way without creating different objects.
You can imagine that there are many other ways to accomplish this task, but if someone ever ask you to perform this task with the minimum amount f string creation, you can use this.
Monday, February 6, 2012
Sunday, May 9, 2010
Super Fast Record Search in C#
Searching for a piece of data in your tables is a common task in almost every single application whether it is a desktop or web application. If you want to look for an item in a relatively small set of records most likely you use some type of record iteration like a foreach loop. We all have done some type of search similar to the one i mentioned.
The problem comes when the amount of information stored in your database begins to increase, and then it grows exponetially! At this point if you have not plan properly you will have to come up with ingenous ideas on how to speed up the data processing and retrieval. Most of this ideas include throwing more hardware power, indexing and the kitchen sink.
Since I am certain that at some point in your life you are going to run into this issue, well here it is. A search that can tackle a large number of record in a very short amount of time, or at least i think so :-\
In this exercise we will create a set of 1 million records, the records are in a circular array that have been shifted "n" positions to the right. The task at hand is to find the amount of spaces in the array that the first element was shifted to the right. No need to worry about the amount of iterations in the array, just the current position.
The keyword here is "BinarySearch" The BinarySearch allows us to search a one dimensional sorted array for an specific value. It uses a binary type of search algorithm.
The problem comes when the amount of information stored in your database begins to increase, and then it grows exponetially! At this point if you have not plan properly you will have to come up with ingenous ideas on how to speed up the data processing and retrieval. Most of this ideas include throwing more hardware power, indexing and the kitchen sink.
Since I am certain that at some point in your life you are going to run into this issue, well here it is. A search that can tackle a large number of record in a very short amount of time, or at least i think so :-\
In this exercise we will create a set of 1 million records, the records are in a circular array that have been shifted "n" positions to the right. The task at hand is to find the amount of spaces in the array that the first element was shifted to the right. No need to worry about the amount of iterations in the array, just the current position.
The keyword here is "BinarySearch" The BinarySearch allows us to search a one dimensional sorted array for an specific value. It uses a binary type of search algorithm.
Friday, May 15, 2009
IE Open new window with focus using Update Panels
Every once in a while we need to open a new window from our main page and we want to make sure it has the focus so the user does not keep on clicking the same "open" button creating multiple pages in the background.
It sounds simple but if you are using "Update Panels" so avoid the annoying refresh look in your page and then you want to open a new window, this can be challenging. Instead here is a way to do it. I used a mix of javascript and c# code.
In my client javascript I defined a function that calls the window.open and pass the proper parameters to declare the page i would like to open.
//script language="javascript"//
function ReportDetailPopup(xCaseNumber) {
var w = window.open("ReportUIDetail.aspx?ExternalCaseNumber=" + xCaseNumber);
}
//script//
Now in my codebehind i call the "RegisterStartupScript" function to call my javascript function and then i set the focus on the Page recently called.
protected void grdResults_SelectedIndexChanged(object sender, EventArgs e)
{
string externalCaseNumber = "";
externalCaseNumber = grdResults.SelectedDataKey.Value.ToString();
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "OpenNewWindow", "ReportDetailPopup('" + externalCaseNumber + "');", true);
Page.Focus();
}
I am certain that there are other ways to do it, but if you are entering the world of "update panels" you will find that many things don't work the way they should for obvious reasons. I will post more tricks to get the most out of update panels.
It sounds simple but if you are using "Update Panels" so avoid the annoying refresh look in your page and then you want to open a new window, this can be challenging. Instead here is a way to do it. I used a mix of javascript and c# code.
In my client javascript I defined a function that calls the window.open and pass the proper parameters to declare the page i would like to open.
//script language="javascript"//
function ReportDetailPopup(xCaseNumber) {
var w = window.open("ReportUIDetail.aspx?ExternalCaseNumber=" + xCaseNumber);
}
//script//
Now in my codebehind i call the "RegisterStartupScript" function to call my javascript function and then i set the focus on the Page recently called.
protected void grdResults_SelectedIndexChanged(object sender, EventArgs e)
{
string externalCaseNumber = "";
externalCaseNumber = grdResults.SelectedDataKey.Value.ToString();
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "OpenNewWindow", "ReportDetailPopup('" + externalCaseNumber + "');", true);
Page.Focus();
}
I am certain that there are other ways to do it, but if you are entering the world of "update panels" you will find that many things don't work the way they should for obvious reasons. I will post more tricks to get the most out of update panels.
Custom Config file in a custom location?
Have you ever had the need to share some settings among different parts of your n-tier environment? Obviously yes! Well recently i was in need of using some type of file that will contain data that can be use in different components of my application without having to replicate the file/data inside and turn it into a maintenance nightmare. Many ideas were thrown around. At the end we agree on using a config file. Similar to the Web.config or the App.Config, but fully customize for our application.
There is a fairly simple way of doing this. First you will create your config file with the values you want.

I called this file “DBSettings.config” and I place it in the same directory as all of my assemblies (dlls) the actual “bin” folder.
Secondly, I created a project called “Controller” with a class called “ConfigurationSettings.cs” Here is the code for this file.
using System;
using System.Reflection;
using System.Collections;
using System.Collections.Specialized;
using System.Globalization;
using System.Configuration;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
namespace MyNamespace.Controller
{
public class ConfigurationSettings
{
private const string APPSETTINGS_SECTION_NAME = "appSettings";
private const string CONFIG_SECTIONS_PATH = "/configuration/configSections";
private const string CONFIG_SECTIONS_GROUP_PATH = "sectionGroup";
private const string CONFIG_SECTION_PATH = "section";
private static Assembly _callingAssembly = null;
private ConfigurationSettings()
{
}
public static object GetConfig(string sectionName, string configFileName)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(configFileName);
IConfigurationSectionHandler handler = GetHandler(sectionName, xmlDoc);
object config = null;
if (sectionName == APPSETTINGS_SECTION_NAME)
{
config = GetAppSettingsFileHandler(sectionName, handler, xmlDoc);
}
else
{
XmlNode node = xmlDoc.SelectSingleNode("//" + sectionName);
config = handler.Create(null, null, node);
}
return config;
}
public static object GetConfig(string sectionName)
{
Assembly asm = Assembly.GetCallingAssembly();
if (_callingAssembly != null && _callingAssembly != asm)
{
asm = _callingAssembly;
}
string filePath = Path.GetDirectoryName(asm.Location);
string asmName = Path.GetFileName(asm.Location);
string configFileName = Path.Combine(filePath, asmName + ".config");
return GetConfig(sectionName, configFileName);
}
public static NameValueCollection AppSettingsLocationDefined(string configFileName)
{
NameValueCollection appSettings = (NameValueCollection)ConfigurationSettings.GetConfig(APPSETTINGS_SECTION_NAME, configFileName);
if (appSettings == null)
{
appSettings = new NameValueCollection();
}
return appSettings;
}
public static NameValueCollection AppSettings
{
get
{
//Store the calling assembly because we are about to call ourselves
_callingAssembly = Assembly.GetCallingAssembly();
NameValueCollection appSettings = (NameValueCollection)ConfigurationSettings.GetConfig(APPSETTINGS_SECTION_NAME);
if (appSettings == null)
{
appSettings = new NameValueCollection();
}
return appSettings;
}
}
protected static IConfigurationSectionHandler GetHandler(string sectionName, XmlDocument xmlDoc)
{
IConfigurationSectionHandler handler = null;
if (sectionName == APPSETTINGS_SECTION_NAME)
{
handler = new NameValueSectionHandler();
return handler;
}
string[] sections = sectionName.Split('/');
string sectionGroup = string.Empty;
string section = string.Empty;
string xPath = string.Empty;
//see if we have a section group that we have to go through
if (sections.Length > 1)
{
sectionGroup = sections[0];
section = sections[1];
xPath = string.Format(CONFIG_SECTIONS_PATH + "/" +
CONFIG_SECTIONS_GROUP_PATH + "[@name='" + sectionGroup + "']/" +
CONFIG_SECTION_PATH + "[@name='" + section + "']");
}
else
{
section = sections[0];
xPath = string.Format(CONFIG_SECTIONS_PATH + "/" +
CONFIG_SECTION_PATH + "[@name='" + section + "']");
}
XmlNode node = xmlDoc.SelectSingleNode(xPath);
string typeName = node.Attributes["type", ""].Value;
if (typeName == null || typeName.Length == 0)
return handler;
Type handlerType = Type.GetType(typeName);
handler = (IConfigurationSectionHandler)Activator.CreateInstance(handlerType);
return handler;
}
protected static object GetAppSettingsFileHandler(string sectionName, IConfigurationSectionHandler parentHandler, XmlDocument xmlDoc)
{
object handler = null;
XmlNode node = xmlDoc.SelectSingleNode("//" + sectionName);
XmlAttribute att = (XmlAttribute)node.Attributes.RemoveNamedItem("file");
if (att == null || att.Value == null || att.Value.Length == 0)
{
return parentHandler.Create(null, null, node);
}
else
{
string fileName = att.Value;
string dir = Path.GetDirectoryName(fileName);
string fullName = Path.Combine(dir, fileName);
XmlDocument xmlDoc2 = new XmlDocument();
xmlDoc2.Load(fullName);
object parent = parentHandler.Create(null, null, node);
IConfigurationSectionHandler h = new NameValueSectionHandler();
handler = h.Create(parent, null, xmlDoc2.DocumentElement);
}
return handler;
}
}
}
Finally I created another project within the solution called “Controller.Database” with a class called
“LoadData.cs” This class contains all of the DB calls with sprocs and query calls, but the important part
is the actual connection string. I am including the part that bring the connection string from the customize config file. It is important to remember that you need to reference the IO and the Reflection assemblies in order to get it to read the proper values.
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections.ObjectModel;
using System.Threading;
using System.ComponentModel;
using MyNamespace.Controller;
using System.IO;
using System.Reflection;
namespace MyNamespace.Controller.Database
{
public class DataAccessManager
{
private const int DEFAULT_MAPPING_SERVICETYPE = 0;
#region Connection String
public static string ConnectionString
{
get
{
//Retrieving the location of the assembly file.
string appPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
//Appending the custom config file name.
appPath = Path.Combine(appPath, "DBSettings.config");
return ConfigurationSettings.AppSettingsLocationDefined(appPath)["ConnectionString"].ToString();
}
}
#endregion
...more code that makes specific calls to get data to different components accros the application.
From this point on you can call the ConnectionString static string from the LoadData.cs and the rest will
be taken care of.
The way we implemented this code allowed us to not include any db connection objects in the UI by
referencing only the components that are in charge of gathering the data.
There is a fairly simple way of doing this. First you will create your config file with the values you want.
I called this file “DBSettings.config” and I place it in the same directory as all of my assemblies (dlls) the actual “bin” folder.
Secondly, I created a project called “Controller” with a class called “ConfigurationSettings.cs” Here is the code for this file.
using System;
using System.Reflection;
using System.Collections;
using System.Collections.Specialized;
using System.Globalization;
using System.Configuration;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
namespace MyNamespace.Controller
{
public class ConfigurationSettings
{
private const string APPSETTINGS_SECTION_NAME = "appSettings";
private const string CONFIG_SECTIONS_PATH = "/configuration/configSections";
private const string CONFIG_SECTIONS_GROUP_PATH = "sectionGroup";
private const string CONFIG_SECTION_PATH = "section";
private static Assembly _callingAssembly = null;
private ConfigurationSettings()
{
}
public static object GetConfig(string sectionName, string configFileName)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(configFileName);
IConfigurationSectionHandler handler = GetHandler(sectionName, xmlDoc);
object config = null;
if (sectionName == APPSETTINGS_SECTION_NAME)
{
config = GetAppSettingsFileHandler(sectionName, handler, xmlDoc);
}
else
{
XmlNode node = xmlDoc.SelectSingleNode("//" + sectionName);
config = handler.Create(null, null, node);
}
return config;
}
public static object GetConfig(string sectionName)
{
Assembly asm = Assembly.GetCallingAssembly();
if (_callingAssembly != null && _callingAssembly != asm)
{
asm = _callingAssembly;
}
string filePath = Path.GetDirectoryName(asm.Location);
string asmName = Path.GetFileName(asm.Location);
string configFileName = Path.Combine(filePath, asmName + ".config");
return GetConfig(sectionName, configFileName);
}
public static NameValueCollection AppSettingsLocationDefined(string configFileName)
{
NameValueCollection appSettings = (NameValueCollection)ConfigurationSettings.GetConfig(APPSETTINGS_SECTION_NAME, configFileName);
if (appSettings == null)
{
appSettings = new NameValueCollection();
}
return appSettings;
}
public static NameValueCollection AppSettings
{
get
{
//Store the calling assembly because we are about to call ourselves
_callingAssembly = Assembly.GetCallingAssembly();
NameValueCollection appSettings = (NameValueCollection)ConfigurationSettings.GetConfig(APPSETTINGS_SECTION_NAME);
if (appSettings == null)
{
appSettings = new NameValueCollection();
}
return appSettings;
}
}
protected static IConfigurationSectionHandler GetHandler(string sectionName, XmlDocument xmlDoc)
{
IConfigurationSectionHandler handler = null;
if (sectionName == APPSETTINGS_SECTION_NAME)
{
handler = new NameValueSectionHandler();
return handler;
}
string[] sections = sectionName.Split('/');
string sectionGroup = string.Empty;
string section = string.Empty;
string xPath = string.Empty;
//see if we have a section group that we have to go through
if (sections.Length > 1)
{
sectionGroup = sections[0];
section = sections[1];
xPath = string.Format(CONFIG_SECTIONS_PATH + "/" +
CONFIG_SECTIONS_GROUP_PATH + "[@name='" + sectionGroup + "']/" +
CONFIG_SECTION_PATH + "[@name='" + section + "']");
}
else
{
section = sections[0];
xPath = string.Format(CONFIG_SECTIONS_PATH + "/" +
CONFIG_SECTION_PATH + "[@name='" + section + "']");
}
XmlNode node = xmlDoc.SelectSingleNode(xPath);
string typeName = node.Attributes["type", ""].Value;
if (typeName == null || typeName.Length == 0)
return handler;
Type handlerType = Type.GetType(typeName);
handler = (IConfigurationSectionHandler)Activator.CreateInstance(handlerType);
return handler;
}
protected static object GetAppSettingsFileHandler(string sectionName, IConfigurationSectionHandler parentHandler, XmlDocument xmlDoc)
{
object handler = null;
XmlNode node = xmlDoc.SelectSingleNode("//" + sectionName);
XmlAttribute att = (XmlAttribute)node.Attributes.RemoveNamedItem("file");
if (att == null || att.Value == null || att.Value.Length == 0)
{
return parentHandler.Create(null, null, node);
}
else
{
string fileName = att.Value;
string dir = Path.GetDirectoryName(fileName);
string fullName = Path.Combine(dir, fileName);
XmlDocument xmlDoc2 = new XmlDocument();
xmlDoc2.Load(fullName);
object parent = parentHandler.Create(null, null, node);
IConfigurationSectionHandler h = new NameValueSectionHandler();
handler = h.Create(parent, null, xmlDoc2.DocumentElement);
}
return handler;
}
}
}
Finally I created another project within the solution called “Controller.Database” with a class called
“LoadData.cs” This class contains all of the DB calls with sprocs and query calls, but the important part
is the actual connection string. I am including the part that bring the connection string from the customize config file. It is important to remember that you need to reference the IO and the Reflection assemblies in order to get it to read the proper values.
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections.ObjectModel;
using System.Threading;
using System.ComponentModel;
using MyNamespace.Controller;
using System.IO;
using System.Reflection;
namespace MyNamespace.Controller.Database
{
public class DataAccessManager
{
private const int DEFAULT_MAPPING_SERVICETYPE = 0;
#region Connection String
public static string ConnectionString
{
get
{
//Retrieving the location of the assembly file.
string appPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
//Appending the custom config file name.
appPath = Path.Combine(appPath, "DBSettings.config");
return ConfigurationSettings.AppSettingsLocationDefined(appPath)["ConnectionString"].ToString();
}
}
#endregion
...more code that makes specific calls to get data to different components accros the application.
From this point on you can call the ConnectionString static string from the LoadData.cs and the rest will
be taken care of.
The way we implemented this code allowed us to not include any db connection objects in the UI by
referencing only the components that are in charge of gathering the data.
Tuesday, March 31, 2009
Finding a job
If you are like most people in the USA right now, you either know someone that lost their job or you are someone that lost your job! I was both. In my previous company they laid off 4,000 employees in one day. I survived three laid offs, but the last one got me.
I do want to share some tips on finding a job. One of the most important items in your list is to use this time to increase your expertise in anything that relates to your industry of work. You don't have to spend tons of money in courses or training DVD's. You can get most if not all the resources that you can imagine in your local library. It is almost free (unless you are late in returning items) since you already pay for it in your taxes!
For real just grab a book and read it, you will see how your knowledge will increase and in time will make you more appealing for companies. Another tip and one that i believe to be "essential" in finding a job is to post your resume online...once it is nice and ready make sure to update it at least once a week. Otherwise your resume will go to the bottom of the list and recruiters would not see it. It might sound crazy, but i swear it works. It does not matter how good your resume is, everyday these websites are bombarded with new and updated resumes that go to the "top" of the pile, meanwhile your gets to the bottom. It actually makes sense since the system assumes that since you have not updated your resume, you probably are no longer looking. I don't mean to update the whole information, but at least a letter or a number would do.
Last but not least, always think positively even if it looks gloomy and you feel like a loser you are not! We are all going through the same thing and it could happen to anyone.
Best of luck and God Speed!
I do want to share some tips on finding a job. One of the most important items in your list is to use this time to increase your expertise in anything that relates to your industry of work. You don't have to spend tons of money in courses or training DVD's. You can get most if not all the resources that you can imagine in your local library. It is almost free (unless you are late in returning items) since you already pay for it in your taxes!
For real just grab a book and read it, you will see how your knowledge will increase and in time will make you more appealing for companies. Another tip and one that i believe to be "essential" in finding a job is to post your resume online...once it is nice and ready make sure to update it at least once a week. Otherwise your resume will go to the bottom of the list and recruiters would not see it. It might sound crazy, but i swear it works. It does not matter how good your resume is, everyday these websites are bombarded with new and updated resumes that go to the "top" of the pile, meanwhile your gets to the bottom. It actually makes sense since the system assumes that since you have not updated your resume, you probably are no longer looking. I don't mean to update the whole information, but at least a letter or a number would do.
Last but not least, always think positively even if it looks gloomy and you feel like a loser you are not! We are all going through the same thing and it could happen to anyone.
Best of luck and God Speed!
Subscribe to:
Posts (Atom)


