Featured post

What’s Popular

What's Popular


How to Get the Underlying DataRow of a DataGridViewRow
23-07-2012 How to get the underlying data row of the bound data table linked to a DataGridViewRow in a DataGridView? How to get the underlying DataRow of a DataGridView.Row? How to get the underlying DataRow matching to a Selected Row of a DataGridView? How to map the DataGridView row to the datasource? How to get back to the matching DataRow of the DataSource of a DataGridView Row? Answer: Use the DataBoundItem property of t...

Comparing Objects in .Net - Reference Equality
30-05-2012 Comparing two objects returns true if they point to the same entry of the heap. Public Class Person { Private String Name; Private int Age; Public Person(String Name) { this.Name = Name; } } Class Program { public static void Main(String[] args) { Person P1 = new Person("John"); Person P2 = new Person("Ron"); ...

Using Short-Circuit Condition Evaluation in VB using AndAlso and OrElse
02-07-2012 Normally, a programming language stops evaluating a composite condition when it finds one of following scenarios. (Condition 1) AND (Condition 2) Since the above composite condition to evaluate to true, both condition 1 and condition 2 have to evaluate to TRUE. If the condition 1 evaluates to FALSE then it is obvious that the whole condition is going to evaluate to FALSE Regardless of the condition 2. Therefore, usually ...

How to auto save the data table in memory into database?
2010-04-01 We frequently get to fetch data from the database, update them and then write them back to the database. Most of the time we only have to write them back as individual records. How about updating a whole database table in the memory and having to synchronize all changes to the actual data table? My initial thought was this would be full of complex coding. However, thanks to Microsoft, there's nothing much to be done at ...

The Structure-Conduct-Performance Model
2010-04-01 In 1930s, a group of economists developed an approach to understand relationship among a firm’s environment, behaviour and performance. This theoretical framework, since then, is known as the Structure-Conduct-Performance (S-C-P) Model.                       Structure, in this model refers to the structure of the industry in which the...

Internet Explorer Major Security Flaw!
2008-12-16 Internet Explorer, one of the most widely used Microsoft products, Considered as the world's most popular web browser has received a Red Alert! The situation was raised when Andreas Sandblad from Sweden reported a vulnerability to Microsoft. The vulnerability: Point of Origin: The Cross-Domain Security Model Fix: Not Available Yet, Microsoft is still investigating on this Description: This Cross-Domain Security model is supposed...

What is Go in SQL Server and the Importance of Using Go
25-05-2012 Go is not a SQL or Transact SQL command. Go is a command recognized by sqlcmd and osql facilities provided by SQL server studio. Go specifies an end to a particular SQL batch. Once the parser interprets a Go, that block is considered completed and the following SQL are considered as new batch till the parser finds a new Go. For example, even though following code isn't syntactically wrong, it won't work because a create pr...

How To Check For Full SubStrings In A Comma Separated String

22/07/2013

How to avoid detecting substrings, how to check for perfect substrings, avoid detecting substrings, avoid detecting substrings when checked for substrings.

Today I needed to find if a string is contained within a string.

So I used the most straight forward method (I used SQL but this works for any language)

declare @msgsToSkip varchar(max) = ‘geoLocation,statusUpdateCheck,forceClose';
declare @msg varchar(max)= ‘statusUpdate’
if(charindex(@msg, @msgsToSkip) > 0)
begin
print ‘found’
end
else
print ‘not found’

Problem – as you can see, this code is written to messages with caption as either “geoLocation”, “statusUpdateCheck”, or “forcedClose”

But I also receive a message with caption “statusUpdate” this is different to “statusUpdateCheck” and must be allowed but obviously the code returns true for statusUpdate as it is a substring of statusUpdateCheck.

The Solution,

Simply make each word dirty by adding a character to it. It could be any character as long as you keep the code consistent. In more simple words, simply add a character to the string you are

Looking For and the string you are Searching In.

This is the fixed code

declare @msgsToSkip varchar(max) = ‘geoLocation-,statusUpdateCheck-,forceClose-‘; — <– Notice that a ‘-‘ is added to make each word dirty
declare @msg varchar(max)= ‘statusUpdate’
if(charindex(@msg + ‘-‘, @msgsToSkip) > 0) — <- Notice the + ‘-‘
begin
print ‘found’
end
else
print ‘not found’

This will not detect statusUpdate as statusUpdateCheck because at the checking time, the code sees statusUpdate as statusUpdate- and it is not a substring of statusUpdateCheck (note that  statusUpdateCheck is now statusUpdateCheck- but it’s the ‘-‘ in the statusUpdate that does the trick.)

Menol
ILT

ILT – Simple technique to toggle between true and false or one and zero

20/06/2013

If you want to toggle a value from 1 to 0 or 0 to 1 depending on the current value then you are at the right place.
(i.e. change the value to 0 only if it’s 1 now and change it to 1 only if it is 0 at the moment)

You can use this to toggle between true and false as well.

Problem: Convert the value to zero if it’s one or convert the value to one if it’s zero.

Solution:
Use this formula : New Value = (Old value -1) * -1
e.g. zero = (one – 1) * -1;
e.g. one = (zero – 1) * -1;

Example:

private void myCode()
{
int x;

/ * — case 1: value of x is one. The expected result after applying the formula is 0 — */
x = 1;
printX(x); // prints Value of X is : 1

// apply
x = toggleOneZero(x);
//Check the output
printX(x); // prints Value of X is : 0

/ * — case 2: value of x is zero. The expected result after applying the formula is 1 — */
x = 0;
printX(x); // prints Value of X is : 0

// apply
x = toggleOneZero(x);
//Check the output
printX(x); // prints Value of X is : 1
}

private int toggleOneZero(int originalValue)
{
int newValue = (originalValue -1) * -1
return newValue;
}

private void printX(int x)
{
Console.WriteLine(“Value of X is :” + x.ToString());
}

[/sourcecode]
Menol

ILT