December 2007 - Posts

DotnetKicks on CommunityServer.org [in 24 seconds]
Monday, December 31, 2007 1:10 AM

i didn't do this the right way i didn't go through documentation or consult with forums, i just wanted to do this the quick and easy way, what i do is i keep a sepparate theme for my blog sepparate from other bloggers so i can change whatever i like without hurting anyone's blogs

so what i really did is i edited to theme pages to show the dotnetkicks button

first the post.aspx page

<csblog:weblogpostdata Property="FormattedBody" IncrementViewCount="true" 
    runat="server" />       
    <p>&nbsp;</p>
<%
   1: Response.Write(String.Format
   2:  ("<a href=\"http://www.dotnetkicks.com/kick/?url=http://{0}\">" +
   3:     "<img src=\"http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://{0}\" border=\"0\" /></a>"
   4: , "aspxwizard.net" + CurrentWeblogPost.ViewPostURL));
%>

i went scanning for the tag that would render the body of the post and then added the raw <%%> block with a response.write that would display the URL, and it just worked :D

 

then i wanted to display the same button on the postlist.aspx ,and i did the same thing i looked for the tag that would render the body and added it like this

<CSBlog:WeblogPostData Property="FormattedBody" runat="server" />
 
<script language="javascript">
   1:  
   2:     var link = 'http://aspxwizard.net<CSBlog:WeblogPostData Property="ViewPostURL" runat="server" />';
   3:     document.write ('<p>&nbsp;</p><a href="http://www.dotnetkicks.com/kick/?url=' + link + '"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=' + link + '" border="0" /></a>')
</script>

 

this time i didn't know how to obtain the currentweblogpost instance so i introduced this post still rugged, but works fine and it took me 24 seconds :D

if u manage to do a better solution refer to this post URL and i'll create a track back for u and implement ur method :D

 

Update : this would also work for any other community button on community server blogs just user a different HTML like DIGG's code instead of dotnetkicks code

by amir.magdy | 1 comment(s)
Filed under: ,
Have u ever needed to extend a string? [Extension Methods]
Thursday, December 27, 2007 6:34 AM

well it's not inheritable by default so you can not extend it. that used to be the case before c# 3.0

now you can specify all the functionality that you need in an Extension Method. problem lies in that the extension method needs to access the instance of the object you are extending

well, it goes like this

1- first you declare a static class with ur static extension method an extension method is defined by the first parameter refering to the instance of the object it's going to extend using the Keyword this (note to implement your complicated algorithm to validate an Email :) )

   1: public static class StringExtension
   2: {
   3:     // note the use of this in the parameter
   4:     public static bool IsEmail(this string s){
   5:         if (s.IndexOf("@") > 0)
   6:         {
   7:             return true;
   8:         }else {
   9:             return false;
  10:         }
  11:     }
  12: }

2- In ur using list on the top of ur file includer the name of the CLASS yes the name of the class not its name space it would look like this
 

   1: using System.Web;
   2: using System.Text;
   3: using Sytem.IO;
   4: // and
   5: using StringExtension;

3- Now whenever u have a string instance that you are using you'll find an extension method added

extension

 

nice isn't it

 

update: 2 jan 2008

this is a practical example

by amir.magdy | 8 comment(s)
Filed under: ,
Terminal server has exceeded maximum number of allowed connection [override maximum]
Thursday, December 27, 2007 2:55 AM

well there's an easy solution if need something urgently and u need to kick someone (close his session)

 

mstsc -v:[UrServerNameOrIP] /console

by amir.magdy | 2 comment(s)
Filed under:
Object and Collection Initializers
Wednesday, December 26, 2007 5:03 AM

well also one of the nicest features in c# 3.0 is the object initializers

well we used to do this

   1: Employee emp = new Employee();
   2: emp.FirstName="Amir";
   3: emp.LastName="Magdy";
   4: emp.Title = "Mr.";

i actually hated writing this previous snippet

now in c# 3.0 we do this

   1: var emp = new Employee {FirstName="Amir",LastName="Magdy",Title="Mr."};

u c that's just one line

it's really nicer now that u don't have to write the type name twice as in line 1 first snippet u just type var

also it's way nicer when u do the same for a collection you can initialize a whole collection in one line :D

   1: var Employees = new List<Employee> {
   2: {new Employee{FirstName="Amir",LastName="Magdy",Title="Mr."}
   3: ,{new Employee{FirstName="John",LastName="Samir",Title="Mr."}
   4: ,{new Employee{FirstName="Mona",LastName="Maurice",Title="Mrs."}
   5: };
i like the syntax it's way shorter and way readable than how it used to
by amir.magdy | 3 comment(s)
Filed under: ,
Auto Implemented Properties
Monday, December 24, 2007 2:23 AM

C# 3 comes with a very nice feature (not undermining the power of lambda expressions and Linq), just this one shows how nice the language has evolved into

 

   1: class Employee
   2: {
   3:     public string firstName { get; set; }
   4: }

you see the previous code will act as a full blown property that maintains its instance value

internally the compiler creates a member and maintains the value for the property

and u can use it in the usual way

   1: var emp = new Employee();
   2: emp.firstName = "Amir";
   3: // next line will show a message box
   4: // with the value "Amir" shown :)
   5: // isn't that nice
   6: MessageBox.Show(emp.firstName);
by amir.magdy | 1 comment(s)
Filed under: ,
Happy Holidays
Sunday, December 23, 2007 8:29 AM

VB9 song

 

   1: Module VB
   2:     Dim myvar As Integer?() = {3 * 3}
   3:     Sub Main()
   4:         For Each i In myvar
   5:             Console.Write("Hello VB")
   6:             With i
   7:                 Console.Write(.Value)
   8:             End With REM a language so true
   9:             If i IsNot Nothing Then
  10:                Console.WriteLine()
  11:             End If REM 
  12:             Console.Write(<some>xml</some>)
  13:         Next
  14:     End Sub
  15: End Module
by amir.magdy | with no comments
Filed under: ,
Free E-books
Sunday, December 23, 2007 3:09 AM

Microsoft has opened 3 MS Press books completely FREE for public.

The books are:

· Introducing Microsoft LINQ by Paolo Pialorsi and Marco Russo (ISBN: 9780735623910) This practical guide covers Language Integrated Query (LINQ) syntax fundamentals, LINQ to ADO.NET, and LINQ to XML. The e-book includes the entire contents of this printed book!

· Introducing Microsoft ASP.NET AJAX by Dino Esposito (ISBN: 9780735624139) Learn about the February 2007 release of ASP.NET AJAX Extensions 1.0, including an overview and the control toolkit.

· Introducing Microsoft Silverlight 1.0 by Laurence Moroney (ISBN: 9780735625396) Learn how to use Silverlight to simplify the way you implement compelling user experiences for the Web. Discover how to support an object-oriented program model with JavaScript.

Object Orientation In JavaScript III [Overloading and Overriding]
Thursday, December 06, 2007 3:17 PM

let's start with Overriding it's easier to explain

of course you must have the idea of inheritence in your mind well if you have missed it check back here first

   1: function superClass (){
   2:     this.superClassMember = function(){
   3:         alert ('this is executed from the Super Class');
   4:     }
   5: }
   6:  
   7: // and then 
   8:  
   9: function subClass(){
  10:     // the next two lines are the inheritence implementation
  11:     this.inheritence = superClass;
  12:     this.inheritence();
  13:     
  14:     // then the rest of the sub class implemenation
  15:  
  16:     this.subClassMember = function (){
  17:         alert ('this is executed from the Sub Class');
  18:     }
  19: }

 

now just add this to the definition of the subCalss

 

   1: function subClass (){
   2:     ...
   3:     ..
   4:     //the next 3 lines are the lines to be added
   5:  
   6:     this.superClassMember = fucntion(){
   7:         alert ("this is the overriden member of the subClass")
   8:     }
   9:  
  10: }

now when u create an instance of the subClass and execute the "superClassMemeber" the result would be the alert containing"this is the overriden member of the subClass"

like this

   1: var subClassObj= new subClass();
   2: //this runs the functionality in the subclass as it has
   3: // been overriden
   4: subClassObj.superClassMember();

 

and now for overloading, i can't really say that the same concept applies in JavaScript, but you can call the very same function with a different no of parameters and it wouldn't break, and also you can add extra parameters and use them inside that function

let's try optional parameters first

   1: function multiply (firstNo,secondNo,thirdNo){
   2:     result  = firstNo * secondNo;
   3:     // then u check for the existence of the thirdNo
   4:     // like this
   5:     if (thirdNo){
   6:         result = result * thirdNo
   7:     }
   8:     return result
   9: }

now the above example can be called like this

   1: mutiply(1,2,3);
   2: //or 
   3: multiply(1,2);

 

or Accessing extra parameters: in the following snippet you'll find out that every function in JavaScript has a private attribute that is called "arguments" which is an array that contains all the values in the parameters call even if the function signature does not contain any parameters

   1: function multiply(){
   2:     var result=1;
   3:     for(i=0;i<arguments.length;i++){
   4:         result = result * arguments [ i ] ;
   5:     }
   6: }

let me give another example

   1: function doSomething (operand,value){
   2:    var expressionToEvaluate = value  ;
   3:    for (i=2;i<arguments.length; i++){
   4:        // note that the first parameter "operand"
   5:        // is accessed with its name and the rest are
   6:        // being accessed through the arguments Array
   7:        expressionToEvaluate+= operand + arguments [ i ];
   8:    }
   9:    return eval (expressionToEvaluate);
  10: }   
  11:  
  12:  
  13: this can do another type of polymorphism :)
  14: alert (doSomething('*',2,3,4,5));
  15: alert (doSomething('+',2,3,4,5));

 

this article is part of a series please check

part 1,

part 2

by amir.magdy | 2 comment(s)
Filed under:
Object Orientation In JavaScript II [Inheritance]
Wednesday, December 05, 2007 2:38 PM

well there are two ways inheritance can be implemented in JavaScript this code explains how the first way

 

   1: function superClass (){
   2:     this.superClassMember = function(){
   3:         alert ('this is executed from the Super Class');
   4:     }
   5: }
   6:  
   7: // and then 
   8:  
   9: function subClass(){
  10:     // the next two lines are the inheritence implementation
  11:     this.inheritence = superClass;
  12:     this.inheritence();
  13:     
  14:     // then the rest of the sub class implemenation
  15:  
  16:     this.subClassMember = function (){
  17:         alert ('this is executed from the Sub Class');
  18:     }
  19: }

 

now in the previous code snippet check out line 11 actually all that was done is that a new attribute was created for the subclass and its value now references the super class then

in line 12 as the attribute actually references a method we can simply invoke it and as long as we're actually in the scope of the second function the this code referencing "this"would simply be running in the second method context which automatically gives all the internals of the super class to the subclass

and then simply you can do this

   1: var subObj = new subClass();
   2: subObj.subClassMember();
   3: // and that would make the alert from the subClass Appear which is normal 
   4: // the cool line to execute is this
   5: subObj.superClassMember();
   6: // and it would display the message from the superClass Method

 

 

and there's also this, you can use prototyping to have all the functionality of a class inside another

it would look like this

   1: function superClass (){
   2:     this.superClassMember = function(){
   3:         alert ('this is executed from the Super Class');
   4:     }
   5: }
   6: // and then the sub class
   7: function subClass(){
   8:     this.subClassMember = function (){
   9:         alert ('this is executed from the Sub Class');
  10:     }
  11: }
  12:  
  13: // inheritence is implemented in the next line
  14: subClass.prototype = new superClass;

and of course you can use the members in the subclass when instantiating the second you can use the same code in the second snippet in this post

this post is part of a series starting here

by amir.magdy | 5 comment(s)
Filed under:
Object Orientation in JavaScript
Tuesday, December 04, 2007 7:14 AM

JavaScript and object orientation has been a long debated issue, and because of the untyped nature of JavaScript many have thought of it as a temporary solution till something that is OO emrges and replaces it, well the current AJAX sites has proved them wrong

in the coming javascript posts i'll try to tap into the OO concepts and how it can be mimicked using JavaScript

this firsrt post is about Classing

i'll first create a simple class

   1: function User (){
   2:     this.firstName ="Amir";
   3:     this.lastName = "Magdy";