static-methods

findViewById inside a Static Method

走远了吗. 提交于 2019-12-23 08:05:10
问题 I have this static method: public static void displayLevelUp(int level, Context context) { LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View layout = inflater.inflate(R.layout.custom_level_coast, (ViewGroup) findViewById(R.id.toast_layout_root)); // this row TextView text = (TextView) layout.findViewById(R.id.toastText); text.setText("This is a custom toast"); Toast toast = new Toast(context); toast.setGravity(Gravity.CENTER_VERTICAL,

what is the work-around to “non-static variable cannot be referenced from a static context”? [closed]

纵然是瞬间 提交于 2019-12-23 07:06:30
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 6 years ago . There's a particular idiom to putting a method, or perhaps anonymous inner class, somehow, into the main method of a driver class: package net.bounceme.dur.misc; import net.bounceme.dur.misc.Foo; public class

Issue with static member function and derived class

回眸只為那壹抹淺笑 提交于 2019-12-23 06:08:35
问题 I have a class with static member function (which is necessary). To be able to use non-static members of the class. I defined a Static_This which is a pointer to the class. template<class T> class Energy_Minimizer { protected: static Energy_Minimizer* Static_This; Petsc_Vector<T>* Gradient; Petsc_Vector<T>* Solution; static int Form_Function_and_Gradient(Petsc_Vector<T> *Solution_,Petsc_Vector<T> *Gradient_, PetscReal *Function_Value_); public: Energy_Minimizer(MPI_Comm Communicator_); void

Base Class to handle behavior, customize features, etc in Console Application?

此生再无相见时 提交于 2019-12-23 04:29:49
问题 I have a Console Application like that: public enum TypeMessage : int { Log, Success, Warning, Alert } class Program { static void Main(string[] args) { //do stuff WriteMessage("Sucess .... etc", TypeMessage.Success); } static void WriteMessage(string message, TypeMessage typeMessage = TypeMessage.Log) { switch (typeMessage) { case TypeMessage.Success: Console.ForegroundColor = ConsoleColor.Green; break; case TypeMessage.Warning: Console.ForegroundColor = ConsoleColor.Yellow; break; case

C++/CLI : How to declare template array as method parameters

。_饼干妹妹 提交于 2019-12-23 03:12:48
问题 I am a newbie to C++/CLI. What is the equivalent of the following C# code in managed C++/CLI for both the header and source file? public static bool ArrayEquals<T>(T[] a, T[] b) { return true; } 回答1: Here is the content for the source file: public: generic <typename T> static bool ArrayEquals(array<T>^ a, array<T>^ b) { return true; } 回答2: I tried the following and kept getting linker errors. Now I know, the keyword generic should be used instead. Thanks Laurent! :) template<typename T> bool

Appropriate use of Static Method

心已入冬 提交于 2019-12-22 04:57:26
问题 Conceptually, is it appropriate to use a static method (C#) when the method will only take inputs and reformat the input as the output? For example: public static string FormatString(string inputString){ return "some formatting" + inputString + "Some other formatting"; } If I were to have a few of these types of methods, would a static "utility" class be a good idea? 回答1: I'd agree with the other answers so far that it certainly makes sense a lot of the time. Sometimes , you may want to

Why do static methods need to be wrapped into a class?

≯℡__Kan透↙ 提交于 2019-12-22 04:28:17
问题 Sorry for the unlearned nature of this question. If there's a simple answer, just a link to an explanation will make me more than happy. After 6 months programming I find static classes to be somewhat useful for storing routines that apply to many different classes. Here's a simplified example of how I use static classes, it's a class for parsing text into various things public static class TextProcessor { public static string[] GetWords(string sentence) { return sentence.Split(' '); } public

Java Pattern class doesn't have a public constructor, why?

别说谁变了你拦得住时间么 提交于 2019-12-22 03:44:23
问题 I've been reviewing Java Regex Library, surprised by the fact the Pattern class does not have a public constructor which I've taken for granted for years. One reason I suspect the static compile method is being used in favor of constructor could be that constructor would always return a new object while a static method might return a previously created (and cached) object provided that the pattern string is the same. However, it is not the case as demonstrated by the following. public class

Are static methods always loaded into memory?

做~自己de王妃 提交于 2019-12-21 20:28:15
问题 Say that I have a Java project with some "Utils" classes, and that those classes have only static methods and members. Once I run my application, are those methods and members automatically loaded into memory? Or that only happens once I call the class along the code? EDIT: Some sample code to illustrate my question. RandomUtils.java public class RandomUtils { private static Random rand = new Random(); public static int randInt(int min, int max) { // nextInt is normally exclusive of the top

Calling a Static method in C#

五迷三道 提交于 2019-12-21 07:36:54
问题 How do I call a static method? I want to call this from a class I have created, I want to get the location from IP. I've declared it but what I need to do is call the method... as static ... To be honest with you, I'm quite confused here, do I need to instantiate address , city , etc.? I have done this so far; LocationTools.cs public static class LocationTools { public static void GetLocationFromIP(string address, out string city, out string region, out string country, out double? latitude,