Showing posts with label code. Show all posts
Showing posts with label code. Show all posts

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...

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)    

Friday, March 18, 2016

Codility's got it

New website I came across. Good learning material and fun challenges.