static-methods

asp.net access a control from static function

六眼飞鱼酱① 提交于 2019-11-28 02:09:27
问题 I have a webform and in that webform, I am trying to access a Panel object from a static method, but couldn't access it How can I access a Panel object from static method. Why am I trying to access the object from static? Because I am using JQUERY which only accepts static methods. I tried to store Panel in session in Page_Load() and retrieve it from static method but it didn't work. 回答1: You are probably using web method in the aspx page to call it from jQuery ajax() . You could not access

Why is using static helper methods in Java bad?

回眸只為那壹抹淺笑 提交于 2019-11-27 21:15:24
问题 I'm asking because I'm trying to use a mocking framework (Mockito) which does not allow you to mock static methods. Looking into it I've found quite a few blog posts saying that you should have as few static methods as possible, but I'm having difficulty wrapping my head around why. Specifically why methods that don't modify the global state and are basically helper methods. For instance I have a class called ApiCaller that has several static methods. One of the static method's purpose is to

Static extension methods in Kotlin

蹲街弑〆低调 提交于 2019-11-27 19:56:44
How do you define a static extension method in Kotlin? Is this even possible? I currently have an extension method as shown below. public fun Uber.doMagic(context: Context) { // ... } The above extension can be invoked on an instance. uberInstance.doMagic(context) // Instance method but how do I make it static method like shown below. Uber.doMagic(context) // Static or class method To achieve Uber.doMagic(context) , you can write an extension to the companion object of Uber (the companion object declaration is required): class Uber { companion object {} } fun Uber.Companion.doMagic(context:

Calling a Activity method from BroadcastReceiver in Android

匆匆过客 提交于 2019-11-27 19:41:29
Here I am creating an online application that depends only on Internet. So whenever there is a network error it must notify user. For that, I have created a BroadcastReciver that receives call when network connection gets lost(Internet). All this works perfectly. Now what I need is that I have to call a method of Activity from this Broadcast Receiver, where I have created an Alert Dialogue. I have read many answers on stack-overflow.com that I can declare that method static and call by using only Activity name, e.g MyActivityName.myMethod() But I can't declare my method static, because I am

Why Static method sometimes returns same result for separate call?

三世轮回 提交于 2019-11-27 19:27:30
问题 In my c# code I have a static method. Here is the code sample: public class RandomKey { public static string GetKey() { Random rng = new Random(); char[] chars = new char[8]; for (int i = 0; i < chars.Length; i++) { int v = rng.Next(10 + 26 + 26); char c; if (v < 10) { c = (char)('0' + v); } else if (v < 36) { c = (char)('a' - 10 + v); } else { c = (char)('A' - 36 + v); } chars[i] = c; } string key = new string(chars); key += DateTime.Now.Ticks.ToString(); return key; } } I am calling this

How to mock with static methods?

巧了我就是萌 提交于 2019-11-27 19:00:16
I'm new to mock objects, but I understand that I need to have my classes implement interfaces in order to mock them. The problem I'm having is that in my data access layer, I want to have static methods, but I can't put a static method in an interface. What's the best way around this? Should I just use instance methods (which seems wrong) or is there another solution? Grundlefleck I would use a method object pattern. Have a static instance of this, and call it in the static method. It should be possible to subclass for testing, depending on your mocking framework. i.e. in your class with the

Java - calling static methods vs manual inlining - performance overhead

百般思念 提交于 2019-11-27 17:49:13
问题 I am interested whether should I manually inline small methods which are called 100k - 1 million times in some performance-sensitive algorithm. First, I thought that, by not inlining, I am incurring some overhead since JVM will have to find determine whether or not to inline this method (or even fail to do so). However, the other day, I replaced this manually inlined code with invocation of static methods and seen a performance boost. How is that possible? Does this suggest that there is

cannot make a static reference to a non static method

眉间皱痕 提交于 2019-11-27 15:53:16
So far I have the following code: import java.util.Scanner; public class HallLanceMemoryCalculator { private double currentValue; public static int displayMenu(){ Scanner input=new Scanner(System.in); int choice=0; while(choice<1||choice>5){ System.out.println("1.Add"); System.out.println("2.Subtract"); System.out.println("3.Multiply"); System.out.println("4.Divide"); System.out.println("5.Clear"); System.out.println("What would you like to do?"); choice=input.nextInt(); } return choice; } public static double getOperand(String prompt){ Scanner input=new Scanner(System.in); System.out.println(

Static methods in PHP

自闭症网瘾萝莉.ら 提交于 2019-11-27 15:43:32
问题 Why in PHP you can access static method via instance of some class but not only via type name? UPDATE: I'm .net developer but i work with php developers too. Recently i've found this moment about static methods called from instance and can't understand why it can be usefull. EXAMPLE: class Foo { public static Bar() { } } We can accept method like this: var $foo = new Foo(); $foo.Bar(); // ?????? 回答1: In PHP the class is instantiated using the new keyword for example; $MyClass = new MyClass();

Why can't you call a non-static method from a static method?

牧云@^-^@ 提交于 2019-11-27 15:18:14
I have a static method [Method1] in my class, which calls another method [Method2] in the same class and is not a static method. But this is a no-no. I get this error: An object reference is required for the non-static field, method, or property "ClassName.MethodName()" Can someone please briefly describe why? including other things that might be related to this. A non-static method requires an instance of the class. Unless you have passed in an instance, or created an instance in your method, you cannot call a non-static method, as you have no idea what instance of the class that method