static-variables

set default value in class constructor C#

戏子无情 提交于 2019-12-07 06:10:51
问题 I need a default value set and many different pages access and update..initially can I set the default value in the class constructor like this? What is the proper way to do this in C# .NET? public class ProfitVals { private static double _hiprofit; public static Double HiProfit { get { return _hiprofit; } set { _hiprofit = value; } } // assign default value HiProfit = 0.09; } 回答1: You can put it in the declaration: private static double _hiprofit = 0.09; Or if it's a more complicated

Python Instantiate Class Within Class Definition

喜夏-厌秋 提交于 2019-12-06 10:47:47
I am attempting to add a variable to a class that holds instances to the class. The following is a shortened version of my code. class Classy : def __init__(self) : self.hi = "HI!" # "CLASSIES" variable holds instances of class "Classy" CLASSIES = [] for i in xrange(0,4) : CLASSIES.append(Classy()) Upon running the code, I get the following error. Traceback (most recent call last): File "classy.py", line 6, in Classy CLASSIES.append(Classy()) NameError: name 'Classy' is not defined Is there another way to add instances of a class to a class/static variable within that class? The class body is

How a static variable is accessible before the declaration?

喜你入骨 提交于 2019-12-06 04:52:03
问题 public class Main { static int x = Main.y; // static int x = y; //Not allowed; y is not defined static int y = x; public static void main(String[] args) { System.out.println(x);//prints 0 } } How come I am allowed to use y trough the class, but not directly? When is y defined? 回答1: The precise rules governing forward reference to class variables are described in the section §8.3.2.3 of the JLS: 8.3.2.3 Restrictions on the use of Fields during Initialization The declaration of a member needs

What exactly does “static” mean when declaring “global” variables in C++?

梦想与她 提交于 2019-12-06 03:45:46
问题 This is an expansion of the scope of a previous question of mine. What exactly is "static", how is it used, and what is the purpose of using "static" when dealing with C++? Thanks. 回答1: The keyword static has different meanings in C++, depending on the context. When declaring a free function or a global variable it means that the function is not to be available outside of this single translation unit: // test.cpp static int a = 1; static void foo() {} If the result of compiling that

Access property from a class method?

痞子三分冷 提交于 2019-12-05 21:16:51
In order to make my code testable, I have created a lazy initializer; this way in my unit test, I can mock any object I want before the getter gets called. When it comes to class methods, though, my class method doesn't have access to the properties I have defined. Is there any way to make the properties accessible by my class method? If not, is there any way to create static variables that are also accessible outside of this class, i.e., accessible by my unit test class? @implementation @synthesize webService; + (void)doSomething { self.webService.url = @"some url"; [self.webService start]; /

ARC: How to release static variable?

ぐ巨炮叔叔 提交于 2019-12-05 17:28:35
问题 Will dealloc (below) release the NSString pointed to by the static variable exampleString ? // ExampleClass.h @interface ExampleClass : NSObject @end // ExampleClass.m static NSString *exampleString; @implementation ExampleClass - (void)dealloc { exampleString = nil; } - (id)init { self = [super init]; if (self) { exampleString = [NSString stringWithFormat:@"example %@", @"format"]; } return self; } @end 回答1: Yes, because since you did not specify an ownership qualifier, the LLVM compiler

PHP: Sharing a static variable between threads

岁酱吖の 提交于 2019-12-05 16:46:06
I have a problem in sharing static variable between different threads in PHP. In simple words I want to 1. Write a static variable in one thread 2. Read it in other thread and do the required process and clean it. For testing above requirement I have written below PHP script. <?php class ThreadDemo1 extends Thread { private $mode; //to run 2 threads in different modes private static $test; //Static variable shared between threads //Instance is created with different mode function __construct($mode) { $this->mode = $mode; } //Set the static variable using mode 'w' function w_mode() { echo

How to access a static variable from another file in C? [duplicate]

别等时光非礼了梦想. 提交于 2019-12-05 10:18:45
Possible Duplicate: Static variable How to access a static variable from another file in C? As a Static variable has a file scope, I think there is no way we can access it outside a file. But still I feel there might be some trick or way to do the same. I don't think there is a easy way. If you can change the file with the static variable you can do something like: static int hiddenVar; // The static var you want to get at // A new function you write int * getHiddenVar() { return &hiddenVar; } But of course if you can change the file, you would just drop the static keyword. Also, I doubt this

'… incorrectly extends base class static side' error when overriding static field in derived class

☆樱花仙子☆ 提交于 2019-12-05 10:08:13
Overriding static field in derived class causes error TS2417: Build:Class static side 'typeof TDerived' incorrectly extends base class static side 'typeof TBase'. Is this a legit error case? class TBase { private static s_field = 'something'; public constructor() {} } class TDerived extends TBase { private static s_field = 'something else'; // commenting this line fixes error public constructor() { super(); } } How should i deal with static fields then? The only workaround right now would be to prepend class name to every static field name which is an exceptionally ugly solution. private

Does android save static variables?

折月煮酒 提交于 2019-12-05 09:13:11
I'm writing a simple android application which is basically a modification of the fragments demo available in the android documentation. In the app, there is a file called Ipsum.java which has a static ArrayList of Strings called Headlines. In the onCreate() method of the main activity, I have the following code which adds some elements into the array list. if (savedInstanceState == null){ Ipsum.Headlines.add("String 1 "); Ipsum.Headlines.add("String 2"); } savedInstanceState is a Bundle that the system passes to the method, if the app is being resumed from some inactive state. The logic is