methods

When to use Shared methods in .NET

﹥>﹥吖頭↗ 提交于 2019-12-05 11:09:53
I'm get kind of getting mixed messages about this so I'm hoping someone can clear this up for me. Should I be using Shared methods/functions in the following situation: I have a generic class named "Person". This class represents a person in the database. I have a manager class named "PersonManager". This class contains methods which adds, updates, deletes individual Person objects. A method also exists to lookup Persons from the database. Should these methods in the manager class be declared as shared methods? Or is it more appropriate to create a new instance of the PersonManager class each

Check if class has method in PHP

泪湿孤枕 提交于 2019-12-05 10:55:57
问题 Currently my code looks like that: switch ($_POST['operation']) { case 'create': $db_manager->create(); break; case 'retrieve': $db_manager->retrieve(); break; ... } What I want to do is, to check if method called $_POST['operation'] exists: if yes then call it, else echo "error" Is it possible? How can I do this? 回答1: You can use method_exists: if (method_exists($db_manager, $_POST['operation'])){ $db_manager->{$_POST['operation']}(); } else { echo 'error'; } Though I strongly advise you don

tagging methods and calling them from a client object by tag

我怕爱的太早我们不能终老 提交于 2019-12-05 10:48:10
I have been trying to figure out a way to tag several methods from my base class, so that a client class can call them by tag. The example code is: public class Base { public void method1(){ ..change state of base class } public void method2(){ ..change state of base class } public void method3(){ ..change state of base class } } A client class from a main() method will call each method of Base through a random instruction sequence: public static void main(String[] args) { String sequence = "ABCAABBBABACCACC" Base aBase = new Base(); for (int i = 0; i < sequence.length(); i++){ char temp =

Using a Laravel model method in an Eloquent query

僤鯓⒐⒋嵵緔 提交于 2019-12-05 10:44:48
This is for Laravel 5.2. I have a method defined as follows in my Users model: public function name() { return "$this->name_first $this->name_last"; } I'm trying to figure out how to use that as part of a query, but it seems like it isn't possible for a somewhat obvious reason: the database doesn't know anything about the method and that makes perfect sense. However, the concept of what I'm trying to achieve makes sense in certain contexts, so I'm trying to see if there's a way to accomplish it naturally in Eloquent. This doesn't work, but it represents what I'm trying to accomplish: public

Node readline module doesn't have 'on' function?

左心房为你撑大大i 提交于 2019-12-05 10:07:38
I'm trying to create a node app that reads a textfile line by line using the 'readline' module, and prints it to the console. var lineReader = require('readline'); lineReader.createInterface({ input: fs.createReadStream('./testfile') }); lineReader.on('line', function(line){ console.log(line); }); According to the module's documentation, there should be an 'on' method . However when I log the instance of the readline object I created, I don't see an 'on' method anywhere: { createInterface: [Function], Interface: { [Function: Interface] super_: { [Function: EventEmitter] EventEmitter: [Circular

Golang methods with same name and arity, but different type

穿精又带淫゛_ 提交于 2019-12-05 09:53:02
问题 The following code works fine. Two methods operating on two different structs and printing a field of the struct: type A struct { Name string } type B struct { Name string } func (a *A) Print() { fmt.Println(a.Name) } func (b *B) Print() { fmt.Println(b.Name) } func main() { a := &A{"A"} b := &B{"B"} a.Print() b.Print() } Shows the desired output in the console: A B Now , if I change the method signature in the following way I get an compile error. I just move the receiver of the method to

How do I dispatch to a method based on a parameter's runtime type in C# < 4?

最后都变了- 提交于 2019-12-05 09:26:37
I have an object o which guaranteed at runtime to be one of three types A , B , or C , all of which implement a common interface I . I can control I , but not A , B , or C . (Thus I could use an empty marker interface, or somehow take advantage of the similarities in the types by using the interface, but I can't add new methods or change existing ones in the types.) I also have a series of methods MethodA , MethodB , and MethodC . The runtime type of o is looked up and is then used as a parameter to these methods. public void MethodA(A a) { ... } public void MethodB(B b) { ... } public void

How to retrieve the current method name so to output it to the logger file?

纵然是瞬间 提交于 2019-12-05 09:25:59
I am using Ruby on Rails 3.2.2 and, in order to display warning messages for development purposes, I am using logger.warn in my code. I would like to retrieve the method name in which that logger.warn runs so to output that method name to the log file. class ClassName < ActiveRecord::Base def method_name # Note: This code doesn't work. It is just a sample. logger.warn "I would like to retrieve and display the #{self.class.to_s}##{method_name}" end end In the log file I would like to see: I would like to retrieve and display the ClassName#method_name Is it possible? If so, how can I make that?

C# WPF How to set Property setter method dynamically?

与世无争的帅哥 提交于 2019-12-05 09:01:15
I've been searching around but I just can't seem to find what I'm looking for, so I'll give it a go here. Situation: I have the class MainWindow and MainWindowData. In MainWindowData are only public properties defined with the attribute UpdateGUI. public class UpdateGUI : Attribute { } public class MainWindowData { [UpdateGUI] public string TESTVAR { get; set; } } Now I want to add a method to each property's setter method in MainWindowData. More specific: void OnPropertyChanged(String PropertyName); I figured I'd fetch all UpdateGUI properties in the MainWindow constructor, and then somehow

Calling static function from instance

血红的双手。 提交于 2019-12-05 08:56:51
I'm trying to call a static magic function ( __callStatic ) from a member of its child class. Problem being, it goes to the non-static __call instead. <?php ini_set("display_errors", true); class a { function __call($method, $params) { echo "instance"; } static function __callStatic($method, $params) { echo "static"; } } class b extends a { function foo() { echo static::bar(); // === echo self::bar(); // === echo a::bar(); // === echo b::bar(); } } $b = new b(); echo phpversion()."<br />"; $b->foo(); ?> Output: 5.3.6 instance How can I make it display "static"? If you remove the magic method '