Sunday, March 4, 2018

Shared Programming

Programming doesn't need to be a lonely activity. I'd love for it to be taught, shared, discussed and debated on.



Saturday, March 3, 2018

Get the Gridview row, row index which fired the RowCommand event

Here is the code snippet:

Public Sub gvTestGrid_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) Handles gvTestGrid.RowCommand
Dim gvr As GridViewRow = CType(CType(e.CommandSource, LinkButton).NamingContainer,           GridViewRow)
Dim rowIndex As Integer = gvr.RowIndex
//Make a label visible in this row
CType(gvr.FindControl("UserSelected"), Label).Visible = True
End Sub


This assumes you have ItemTemplate column in your GridView (gvTestGrid) containing a label with Id="UserSelected" which is hidden by default.

Type of parameter is not CLS-compliant

So it's just another normal day, I am coding and coding and coding and suddenly this squiggly line shows up!
Oh no! The squiggly line of death you ask?? Not quite it's like a caterpillar and not much more than that really. It's not dreaded, deceitful or deadly. So I am pretty sure I won't die and neither would my laptop.


This squiggly line appeared below a function parameter. On closer inspection, as I hover my mouse over the caterpillar,  a message pops up saying that "Type of parameter 'someVariableName' is not CLS compliant. "
First time I have seen that error. Now if you'll want more details of the error you can find it here
In my specific case, this was caused because 'Unsigned types should not be part of the public interface of the class'. This particular function was being made public and the parameter in question was a enumerator type.
Which means ..... (drum roll)...........
Enums are stored as unsigned integers by default!
No, no they are not. Actually, here is the real reason why I got the compiler warning:
"The method was being publicly exposing from an assembly marked as CLS-Compliant a type that is from an assembly that is not CLS-Compliant.
This post on SO helped


Extreme short hand coding (C#, ASP.NET)

I was adding items to a drop down list on code behind and in my quest to reduce lines of code, I did this:

dropdownListOne.Items.Add(New ListItem("text", "value" ) With {.Selected = True})

This would add a new listitem and select it. You can also achieve this by writing the following lines:

Dim li As New ListItem()
li.Text = "text"
li.Value = "value"
li.Selected = True
dropDownListOne.Items.Add(li)

Woohoo! So I just saved 4 lines of code (could be more if we had more properties of ListItem to set). Not much difference in performance, I suppose. You can achieve the same thing in C# using:

dropDownListOne.Items.Add(New ListItem("text", "value") { Selected = true});

Linq is fun

Recently I used some LINQ in Visual Basic .NET to search through data structures such as Array and Lists.

//check if list contains a particular object with two property values equal to x and y respectively.
objectArray.ToList.Find(Function(objectItem) objectItem.PropertyOne = x AndAlso objectItem.PropertyTwo = y)
//remove specific notification objects from list of notification objects list
objectArray.RemoveAll(Function(objectItem) objectItem.PropertyOne = x)
//creates a new array without a particular item in the original array
objectArray = objectArray.Where(Function(objectItem) objectItem.PropertyOne <> x).ToArray()


Definitely helps with brevity of the code and has a cool syntax. But I am not so sure about performance. I think it still uses loops internally. I will keep updating this as I use more and more LINQ. More to follow...

Self join SQL Interview Question

This seems like a good interview question. Let's suppose in our database, there is a Section table with a SectionId and a ParentSectionId. ParentSectionId contains the SectionId of the parent.
Now, find sections and which are parents of sections other than themselves.

SELECT S.SECTIONID FROM Section S  INNER JOIN
Section S2 ON S.SectionId= S2.ParentSectionId
WHERE S2.ParentSectionId <> S2.SectionId)


Alternatively,

SELECT * FROM Section S  AND
(SELECT COUNT(*) FROM Section S2
WHERE S2.ParentSectionId = S.SectionId
AND S2.ParentSectionId <> S2.SectionId)

Coding digest (SQL): Order by CreatedDateTime And ID Descending

I am creating a timeline of events right now in descending order. We have the events inside a table called timeline table. If the two events have the same CreatedDateTime, they should be ordered by their IDs (primary key which is a identity).

 So let's write a query for that.
(Assuming X is the current datetime)
SELECT * FROM TIMELINETABLE WHEN CREATEDDATETIME < X
ORDER BY DATETIME DESC, ID DESC

Is this right?
It is right but it won't work for me. Because, I am populating timeline items 10 at a time. And once the user scrolls to the bottom of the page, I will display 10 more events. For that I need to pass the last datetime and ID displayed on screen.

(Assuming X is the datetime and Y is the ID)
SELECT TOP 10  * FROM TIMELINETABLE WHEN CREATEDDATETIME < X OR (DATETIME = X AND ID < Y)
ORDER BY DATETIME DESC

Date inputs




How to create a bootstrap modal dialog?



HTML code    

Precedence and associativity of operators

Recently at a meetup, one of my colleagues asked the following question:  
int a =1; int b=2; c=a+++b;
What is the value of c?
SOLUTION: So for the answer, you need to look at precedence and associativity of operators here ++ has higher priority than + and will be evaluated first. Therefore, the equation is equivalent to
c= (a++)+b;   //(1++)+2=1+2=3
And the value of c is 3.  

Lets take another example. Suppose we add the line
c=a=b;
to the above code. Now what is the value of c?
SOLUTION: Both the operators (=) are of the same precedence. Therefore associativity comes into the picture and the associativity of = is from right to left. Therefoer, the equation is equivalent to,
 c=(a=b);
And the value of c is 2  

TIP: You can memorize the basic structure of the precedence table using the following trick: PUMASRELCA  - each letter is the first letter of each category in the table arranged in order. Let me take you to the animal kingdom: PUMA S(low) R(red) E(lephants) L(loath) C(at) A(udio)    

Where does a programmer sit?

So you work in a office in a software team, and hence you know the general setting of cubicles and conference rooms.

Programmer 1: Hey! Do you know where Programmer 3 sits?

Programmer 2: Yes.

Programmer 1: Where?

Programmer 2: On a chair!

LOLs!

This joke is applicable in any office not just a software development office. Have fun with it.

Braces style

Use whatever brace style you prefer. But don't use this:


 Image Source

O(n) for matrices

Recently, I was discussing a coding problem with a couple of developers.
"Transpose a nxn matrix along it's diagonal"
I won't tell you the answer to the problem because you are all masters and of course it is very easy for you to figure it out (or search it!). But this problem ignited a discussion among us about O(n) for matrices.

So if you traverse a two dimensional matrix array[n][n] using two loops as follows:
for(i=1 to n)
for(j=1 to n)
print array[i][j]
What is the efficiency of the above algorithm?
You might be tempted to say it is O(n2) since we have two for loops. But look closer. The number of elements in a nXn matrix is n2. And therefore the efficiency of above algorithm is O(N) where N (=n2) is the number of elements in the matrix.