[C++/CLI] How to throw a .NET exception

Recently cooperate with a Taiwan university lab. They use C++ to code a core library for our .NET developers to use. Here is how to throw a .NET exception in C++/CLI

public:
	int divide(int a, int b)
	{
		if(b==0)
		   throw gcnew System::Exception("It is a C++\CLI exception, can not divided by zero");
	}

[javascript]goto link later

here is the javascript code for preventing redirect when user clicks on a html anchor

javascript:

var url = {};

function addEvent(el, type, evt){
	if(el){
	if ( document.addEventListener ) {
	el.addEventListener( type, evt, false );
	} else if ( document.attachEvent ) {
	type="on"+type;
	el.attachEvent( type, evt );}}
}

function log4link0(el){
	addEvent(el, "click", function(evt){
		var lv = el.getAttribute("href");
		if(lv){
			/* record the clicked href */
			console.log("going to:"+lv);
			url.value = lv;}
		/* stop click event */
		if(evt){
			if (evt.stopPropagation ){
				evt.stopPropagation();
				evt.preventDefault();  
			}else{
				window.event.cancelBubble = true;
				window.event.returnValue = false;}
		}	
	});
}

function log4link(){
	var lks = document.getElementsByTagName("a");
	if(lks){
		var i=lks.length; while(i--){
			log4link0(lks[i]);}}
	/* register click event to element*/
	addEvent(document.getElementById("goto"), "click", function(){
		if(url && url.value)
			window.location = url.value;
	});
}

/* registr event on document load */
if ( document.addEventListener ) {
document.addEventListener( "DOMContentLoaded", log4link, false );
} else if ( document ) {
document.attachEvent("onreadystatechange",function(){ 
		if ( document.readyState === "complete" ) {log4link();}
});}

html:

<body>
<a href="http://www.google.com">Google</a>
<a href="http://www.yahoo.com">YAHOO!</a>
<a href="http://www.baidu.com">Baidu</a>
<a id="goto">continue</a>
</body>

i also “componentlized” this into a light-weight and independent javascript js library, called Go2Later

[C#] about Clone

 fooObj obj1 = new fooObj() { Name = "Aval" };
fooObj obj2 = (fooObj)obj1.Clone();
fooObj obj3 = (fooObj)obj1.CloneWithSerialization();
fooObj obj4 = obj1;
fooObj obj5 = (fooObj)obj1.ShallowCopy();
System.Diagnostics.Debug.WriteLine(obj1 == obj2);
System.Diagnostics.Debug.WriteLine(obj1 == obj3);
System.Diagnostics.Debug.WriteLine(obj1 == obj4);
System.Diagnostics.Debug.WriteLine(obj1 == obj5);
System.Diagnostics.Debug.WriteLine(obj1.Equals(obj2));
System.Diagnostics.Debug.WriteLine(obj1.Equals(obj3));
System.Diagnostics.Debug.WriteLine(obj1.Equals(obj4));
System.Diagnostics.Debug.WriteLine(obj1.Equals(obj5));

[Serializable]
    class fooObj: ICloneable, ISerializable
    {
        public String Name { get; set; }

        public override bool Equals(object obj)
        {
            if (obj == this)
                return true;
            else if (obj == null)
                return false;
            else 
                return (obj.GetType() == typeof(fooObj) && this.Name.Equals(((fooObj)obj).Name));
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public object Clone()
        {
            return new fooObj() { Name = this.Name };
        }

        public object MemberwiseClone()
        {
            return this.MemberwiseClone();
        }

        public object CloneWithAs()
        {
            return (new fooObj(){Name=this.Name}) as object;
        }

        public object ShallowCopy()
        {
            return this;
        }

        public object CloneWithSerialization()
        {
            using (Stream objects = new MemoryStream())
            {
                IFormatter formatter = new BinaryFormatter();
                formatter.Serialize(objects, this);
                objects.Seek(0, SeekOrigin.Begin);
                return formatter.Deserialize(objects);
            }
        }

        public fooObj() { }

        #region ISerializable Members

        [SecurityPermissionAttribute(SecurityAction.Demand,
          SerializationFormatter = true)]
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("Name", this.Name);
        }

        protected fooObj(SerializationInfo info, StreamingContext context)
        {
            this.Name = info.GetString("Name");
        }

        #endregion
    }

[ie] interesting letter spacing

letter-spacing attribute in css is pretty handy when you run into the ie font spacing problem. Html text in certain font(in my case, Verdana) sometimes looks different in Firefox and IE, ever with same font-size & font.
e.g.

font-family: Verdana;

will look different in IE and Firefox with different letter spacing. it could be solved by judging the letter-spacing

a.ls0{letter-spacing:0px;}
a.ls1{letter-spacing:1px;}
a.li-1{letter-spacing:-1px;}
a{font-family:Verdana;}

[javascript] about object referring

object referring is confusing in Javascript. somehow, i came out with a piece of code

var u = {};
var x = y = z = {"cvalue":"cell", "call":function(){alert(this.cvalue);}};

(function(){u=x;/*change all cvalue in x,y, z, u*/ u.cvalue = "notcell";})();

if(u == x && x == y && y == z && z == u){
    u.call();
}

//only u goes to null
u = null;
//x,y,z stay same
alert(x.cvalue);

By u = null, only breaking the reference to the certain memory location(noticing not reference to “x”). Therefore, if x= null, u still referring to the memory and u.call() still works.

I am a little blue fellow

little blue fellow
likes to share

  • open source story
  • startup story
  • coding
JavaScript String match

[C#] using and dispose

using is a very helpful statement in C#.  Recently, i work on a visual recognizing project, involving a lot of image processing. When processing a video file frame by frame, a serious memory leak came out due to the indisposed image object. To solve this, we could call dispose() method.


image.Dispose();

Or better approach

using(NImage image = NImage.FromBitmap(videoBMP.BMPBinary))
{
	//do stuff
}

With using statement, won’t worry when to call dispose and try/catch block

e.g.

private void execute()
{
	using (Waster w = new Waster())
	{
		w.Throws();
	}
}

class Waster: IDisposable
{
	public void Throws()
	{
		throw new Exception();
	}

	public void Dispose()
	{
		System.Diagnostics.Debug.WriteLine(&quot;i am wasted...&quot;);
	}
}

“i am wasted” is still printed out