Google adsense

Search

Thursday, March 24, 2011

JavaScript: check for undefined or null object/variable

if(typeof(foo) !== 'undefined' && foo != null) {
//you can use foo!
}

Thanks,

InfoSource: http://snipplr.com/view/14618/javascript-check-for-undefined-or-null-objectvariable/

Wednesday, March 23, 2011

JavaScript: Enum in JavaScript

Example 1:

var Days = {"sunday" : 0, "monday" : 1, "tuesday" : 3, "wednesday" : 4, "thursday" : 5, "friday" : 6, "saturday" : 7};

document.write(Days.friday);


Example 2:

function Enum() {}
Enum.ColorType = {red:0, blue:1, green:2}
document.write(Enum.ColorType.blue);


Example 3:

var enumObj = new Object();
enumObj.fontSize = {small:10, medium:12, large:14}
document.write(enumObj.fontSize.small);


Thanks,

InfoSource: http://programming.top54u.com/post/Creating-Javascript-Enum-values.aspx

Friday, March 18, 2011

JavaScript: jQuery loading remote/external javascript files using getScript()

FMM: For my memory

$.ajaxSetup({async: false});

$.getScript(MVCRoot+fileName+'.js');

$.ajaxSetup({async: true});


FYI

Thanks,

Info Source: http://colourgray.wordpress.com/2008/09/22/jquery-loading-external-javascript-files-using-getscript/

Tuesday, March 15, 2011

JavaScript: Functions

FMM: For my memory

substring() --- string.substring(from, to);
toUpperCase() --- string.toUpperCase();
toLowerCase() --- string.toLowerCase();

FYI

Wednesday, March 2, 2011

C#: Evaluate C# Code (Eval Function)

It is function to give you a lot of chance such as convert string to object.

// Eval > Evaluates C# sourcelanguage

public static object Eval(string sCSCode) {

CSharpCodeProvider c = new CSharpCodeProvider();
ICodeCompiler icc = c.CreateCompiler();
CompilerParameters cp = new CompilerParameters();

cp.ReferencedAssemblies.Add("system.dll");
cp.ReferencedAssemblies.Add("system.xml.dll");
cp.ReferencedAssemblies.Add("system.data.dll");
cp.ReferencedAssemblies.Add("system.windows.forms.dll");
cp.ReferencedAssemblies.Add("system.drawing.dll");

cp.CompilerOptions = "/t:library";
cp.GenerateInMemory = true;

StringBuilder sb = new StringBuilder("");
sb.Append("using System;\n" );
sb.Append("using System.Xml;\n");
sb.Append("using System.Data;\n");
sb.Append("using System.Data.SqlClient;\n");
sb.Append("using System.Windows.Forms;\n");
sb.Append("using System.Drawing;\n");

sb.Append("namespace CSCodeEvaler{ \n");
sb.Append("public class CSCodeEvaler{ \n");
sb.Append("public object EvalCode(){\n");
sb.Append("return "+sCSCode+"; \n");
sb.Append("} \n");
sb.Append("} \n");
sb.Append("}\n");

CompilerResults cr = icc.CompileAssemblyFromSource(cp, sb.ToString());
if( cr.Errors.Count > 0 ){
MessageBox.Show("ERROR: " + cr.Errors[0].ErrorText,
"Error evaluating cs code", MessageBoxButtons.OK,
MessageBoxIcon.Error );
return null;
}

System.Reflection.Assembly a = cr.CompiledAssembly;
object o = a.CreateInstance("CSCodeEvaler.CSCodeEvaler");

Type t = o.GetType();
MethodInfo mi = t.GetMethod("EvalCode");

object s = mi.Invoke(o, null);
return s;

}

Thanks,
Info source: http://www.codeproject.com/KB/cs/evalcscode.aspx