methods

What is the difference between new Some::Class and Some::Class->new() in Perl?

Many years ago I remember a fellow programmer counselling this: new Some::Class; # bad! (but why?) Some::Class->new(); # good! Sadly now I cannot remember the/his reason why. :( Both forms will work correctly even if the constructor does not actually exist in the Some::Class module but instead is inherited from a parent somewhere....

How to design this method if it returns multi-dimensional arrays?

I am now designing an SNMP library. The problem is caused by a special function like this, *** GetTable(string id) This function may return Variable[,] which is a two dimensional array sometimes, but also Variable[,,] and arrays with more dimensions. So I believe it is not reasonable to return fixed array such as Variable[,], Variable[...

Java synchronized methods: lock on object or class

The Java Tutorials say: "it is not possible for two invocations of synchronized methods on the same object to interleave." What does this mean for a static method? Since a static method has no associated object, will the synchronized keyword lock on the class, instead of the object? ...

do you put your calculations on your sets or your gets . .

which is better ??? public class Order { private double _price; private double _quantity; public double TotalCash { get { return _price * _quantity; } } or public class Order { private double _totalCash; private double _price; private double _quantity; private void CalcCashTotal() { _tot...

Java Static

Duplicate: http://stackoverflow.com/questions/413898/what-does-the-static-keyword-do-in-java I've read this post already. What does the "static" keyword in a method do? I remember being told that static != clingy...but that is pretty much all I know about this keyword. ...

C# method call delegation

Say I have a DLL assembly, containing an auto-generated class with two methods: public class AutoGeneratedClass { public int DoSomething(bool forReal) { ??? } public void DoSomethingElse(string whatever) { ??? } } The methods could be anything, really. The above is just an illustration. What kind of code would I need to gener...

Why do Rhino.Mocks and Moq say that Bar is a non-overridable member?

Could someone explain why both tests using the latest versions of Moq and Rhino.Mocks frameworks fail complaining that Bar is not a virtual/overridable method: public interface IFoo { string Bar(); } public class Foo : IFoo { public string Bar() { return "Bar"; } } [TestMethod] public void MoqTest() { var f...

Custom nested properties/methods in asp.net

I'm looking for a way to write a custom .net class that would allow for nested methods. For example... say I have a class X with a function Y that returns a list. Then I have another function that returns a sorted list... I would like to be able to do something like x.y().z() where z would accept the output of y() as its input. Basic...

Declarative ruby programming replacement for if/then/else/RETURN/end?

I have this showing up all over in my controllers: if not session[:admin] flash[:notice] = "You don't have the rights to do #{:action}." redirect_to :action=>:index return end And its sibling: if not session[:user] and not session[:admin] flash[:notice] = "You don't have the rights to do #{:action}." redirect_to :action=>:i...

C# - Calling methods from inside class

How do you call a client codes methods from inside a class defined in the client code? For example, I have a memory reading class, that can read values from the memory of a process at a certain address. I also have classes for managing the type of data that is read from memory (I am reading about an in-game 'object'. In the 'client code...

(Ruby) How can I get a reference to a method?

Is it possible in Ruby to get a reference to methods of an object ( I would like to know if this can be done without procs/lambdas ) , for example , consider the following code : class X def initialize @map = {} setup_map end private def setup_map # @map["a"] = get reference to a method # @map["b"] = get refere...

C/C++ Performance Globals vs Get/Set Methods

I saw this question asking about whether globals are bad. As I thought about the ramifications of it, the only argument I could come up with that they're necessary in some cases might be for performance reasons. But, I'm not really sure about that. So my question is, would using a global be faster than using a get/set method call? G-...

Changing the values of variables in methods, Java

Hello, I have a questions about changing the values of variables in methods in Java. I have the following example class: public class Test { public static void funk(int a, int[] b) { b[0] = b[0] * 2; a = b[0] + 5; } public static void main(String[] args) { int bird = 10; int[] tiger = {7}; T...

What is the most pythonic way to make a bound method act like a function?

I'm using a Python API that expects me to pass it a function. However, for various reasons, I want to pass it a method, because I want the function to behave different depending on the instance it belongs to. If I pass it a method, the API will not call it with the correct 'self' argument, so I'm wondering how to turn a method into a fu...

Returning a reference from a constant function

#include "iostream" #include "vector" class ABC { }; class VecTest { std::vector<ABC> vec; public: std::vector<ABC> & getVec() const { //Here it errors out return vec; } }; Removing the const fixes it , is it not the case that getVec is a constant method. So why is this not allowed? ...

How do you create python methods(signature and content) in code?

I've created a method that generates a new class and adds some methods into the class, but there is a strange bug, and I'm not sure what's happening: def make_image_form(image_fields): ''' Takes a list of image_fields to generate images ''' images = SortedDict() for image_name in image_fields: images[image_name] = fo...

Why can't this be optimized?

I have a function that I use to add vectors, like this: public static Vector AddVector(Vector v1, Vector v2) { return new Vector( v1.X + v2.X, v1.Y + v2.Y, v1.Z + v2.Z); } Not very interesting. However, I overload the '+' operator for vectors and in the overload I call the AddVector function to avoid code duplica...

How do you create a method for a custom object in JavaScript?

Is it like... var obj = new Object(); obj.function1 = function(){ //code } or something like that? ...

init() method in servlet

can we change the name of the init() method of servlet? i mean to say the life should be- xyz()-service()-destroy() instead of init()-service()-destroy() ...

Using "this" with methods (in Java)

Exact duplicate by same poster: http://stackoverflow.com/questions/516291/the-use-of-this-in-java Hello, what about using "this" with methods in Java? Is it optional or there are situations when one needs to use it obligatory? The only situation I have encountered is when in the class you invoke a method within a method. But it is op...