Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Sunday, November 4, 2012

Null Check in different language


Python - Null Check

15 or "default"       # returns 15
0 or "default"         # returns "default"
None or "default"    # returns "default"
False or "default"    # returns "default"

Django Template - None Check


{{obj.item_value|default_if_none:"smile"}}

C# - Null Check

var data = val ?? "default value";

Java - Null check

Foo f = new Foo();
DummyObj obj = new Obj().getSelection();
String str = obj != null ? f.format(obj) : "";

Javscript - Null/Undefined check

if (! param) param = "abc";
//other way to check this
if (param == null) is same as if(!param)


jQuery - Null/existence check

if ( $('#myDivObj').length ) {}

Feel free to add comments and other languages Null check mechanism (inline or explicit) 

Saturday, February 6, 2010

Object Oriented JavaScript and C#

Just started to work on object oriented java script language, and realized its whole new world of world of programming. You can define the properties (member variables) and methods for objects, and objects of the JavaScript are Dictionary. As its dictionary, it will be collection of name/value pairs. Below is the example –

JSObject = new Object();

You don’t need to define Property beforehand, you can directly initialize it directly.

JSObject.property1 = new Date();

alert('this is property property1 :' + JSObject.property1);

Same with the method –

JSObject.func1 = function() { alert ('this is method func'); }

JSObject.func1();

This is just a basic example, you can create the full workflow of the script page, and handle all the operations in backend.

JSObject2 = { Property1 : "Test",

Method1 : function() {alert("Test method1 function");}

You can register the script on the back endd and execute on the page load –

if(!Page.ClientScript.IsStartupScriptRegistered("myTest"))

Page.ClientScript.RegisterStartupScript(this.GetType(), "myTest", "Test()",true);

This is just simple OOP JS example, you can do many more thing in client side and take advantage of JS. ExtJS library is another extended world of JavaScript. I will explore more things, and post few more examples and features of capabilities of JavaScript.