methods

How to determine if Native JavaScript Object has a Property/Method?

I thought this would be as easy as: if(typeof(Array.push) == 'undefined'){ //not defined, prototype a version of the push method // Firefox never gets here, but IE/Safari/Chrome/etc. do, even though // the Array object has a push method! } And it does work fine in Firefox, but not in IE, Chrome, Safari, Opera, they return all pr...

Static methods vs instance methods in C#

For an application I am writing, I want to have extreme extensibility and extension methods seem to give me what I want, plus the ability to call them without an instance, which I need too. I remember reading that static methods are faster than instance methods but don't get the advantages of GC. Is this correct? It's highly unlikely I...

Extension Methods for Indexers, would they be good ?

Extension Methods for Indexers, would they be good ? I was playing around with some code that re-hydrates POCO's. The code iterates around rows returned from a SqlDataReader and and uses reflection to assign properties from column values. Down my call stack I had a code a line like this :- poco.Set(“Surname”, “Smith”); // uses exten...

Properties vs Methods

Quick question: When do you decide to use properties (in C#) and when do you decide to use methods? We are busy having this debate and have found some areas where it is debatable whether we should use a property or a method. One example is this: public void SetLabel(string text) { Label.Text = text; } In the example, Label is a c...

Handling single and multiple values in C# methods effectively

I have a method like: AverageAndDoSomeMath (Point2) and I wonder how to handle: AverageAndDoSomeMath (Point2) // single AverageAndDoSomeMath (Point2 collection) // multiple using a single implementation preferably. For collection, I plan to use the IEnumerable type so I can pass any kind of collection, but for a single value, I do...

Attaching chained methods to collections of elements in JavaScript

This is—at least at the moment—purely experimentation, but I'm curious: is there a way to attach methods (via prototyping) to collections of elements? I've tested the following code: <div>a</div> <div>b</div> <div>c</div> <script> NodeList.prototype._ = function(s) { for (x = 0; x < this.length; x++) { eval('this[x]' + '....

How to use self class method on iPhone? (conceptual question)

I write an instance method in ClassName.m: -(void)methodName:(paraType)parameter {...} And call it using [self methodName:parameter]; A warning will pop up, but the code still runs successfully. Is this because I haven't created an instance of the class? Why the method still runs normally? And what is the correct way to call self meth...

Objective C scope problem

I have the following Obj C function that works properly: NSString* myfunc( int x ) { NSString *myString = @"MYDATA"; return myString; } However if I add code to update a UIImage the compile fails with image1 being unknown. image1 is valid: it's set up in the .h, synthesized and that exact line of code works in a m...

Calling a obj-c method with a parameter

I've change a c-style function to an objective-c method. As a method, how do i use it? NSString* myfunc( int x ) is now: - (NSString *)myFuncWithParam:(int)x c code: myString = myfunc(x); // works obj-c code: myString = myFuncWithParam(x); // fails to compile. From one of the answers: myString = [object myF...

Obj-C methods: calling with parameters

I've been using c-style functions, but I just learned they can't see instance variables. So I was advised to convert them to methods. NSString* myfunc ( int x ) becomes: - (NSString *)myfunc:(int)x and myString = myfunc(x); becomes myString = [myString myfunc: x]; ?? This compiles with ominou...

Why isn't calling a static method by way of an instance an error for the Java compiler?

I'm sure you all know the behaviour I mean - code such as: Thread thread = new Thread(); int activeCount = thread.activeCount(); provokes a compiler warning. Why isn't it an error? EDIT: To be clear: question has nothing to do with Threads. I realise Thread examples are often given when discussing this because of the potential to ...

Retrieving the calling method name from within a method (C#)

I have a method in an object that is called from a number of places within the object. Is there a quick and easy way to get the name of the method that called this popular method. Pseudo Code EXAMPLE: public Main() { PopularMethod(); } public ButtonClick(object sender, EventArgs e) { PopularMethod(); } public Button2Click(o...

Why can't java find my method?

Hey everyone, I am trying to wrap my mind around something in java. When I pass an object to another class' method, can I not just call any methods inherent to that object class? What is the reason code such as the example below does not compile? Thank you, class a { public static void myMethod(Object myObj) { myObj.testing();...

Extending Number.prototype in javascript and the Math object?

I've always wondered why Javascript has the global Math object instead of giving numbers their own methods. Is there a good reason for it? Also are there any drawbacks (other than efficiency) to doing something like this?: Number.prototype.round = function(){ return Math.round(this); }; Just to make clear, I understand that const...

Detecting Overridden Methods in Perl

Last week I was bitten twice by accidentally overriding methods in a subclass. While I am not a fan of inheritance, we (ab)use this in our application at work. What I would like to do is provide some declarative syntax for stating that a method is overriding a parent method. Something like this: use Attribute::Override; use parent '...

p method in Ruby hard to search for

I'm trying to find info on the p method in Ruby. It seems to produce internal info on the properties of a class but when I try to search for it I get every word that has the letter p in it. ...

aliasing a method results in different objects?

def foo "foo" end alias foo2 foo puts "foo2: " + foo2.object_id.to_s puts "foo: " + foo.object_id.to_s In the above example, I expected to see the same object_id output for each method call since they reference the same method. Why do I see different object_id's? When you alias a method in Ruby doesn't the alias refer to the origi...

C# Subclass with same method

I have a superclass with two subclasses. The two subclasses both have a method with checks whether a chapter has content. For subclass 1 this method is HasContent(int chapterID) and for subclass 2 this is HasContent(int chapterID, int institution). As you can see subclass 2 has an extra parameter. The purpose of both methods is the same....

Recursive functions within OCaml objects

I am trying to figure out recursion for OCaml in the context of an object's method. I have tried the following code but can't seem to get it to compile. class foo = object (self) method loopTest = let rec doIt x = Printf.printf "%d\n" x; if x>1 then doIt (x+1) end;; How do I create a recursive function of this sort within a m...

Flash/AS3 passing a method into a method

I am trying to make a transitionmanager and I wish to pass a method into a method, because I would like to make a bunch of different seperate transition methods which I fetch as a public static function from a Transitions class. Example in TransitionController class: public function doTransition(mc:MovieClip, transition:Function = Tra...