static-members

Setting Label Text in XAML to string constant

一世执手 提交于 2019-11-29 09:31:41
I have a single string constant that I have to re-use in several different XAML layouts, so instead of duplicating it, I'd like to just bind it to a constant. I have a class which defines the string in C#: public static class StringConstants { public static string MyString { get { return "SomeConstant"; } } } I'd like to be able to set the value through XAML via something like the following: <Label Content="{Binding local:StringConstants.MyString}"/> Is this achievable? I've searched for examples, but I've only found samples that involve some tinkering in the code-behind and I'm wondering if

Java: Overriding static variable of parent class?

橙三吉。 提交于 2019-11-29 09:12:49
I have the following class which I'm using as the base of all the models in my project: public abstract class BaseModel { static String table; static String idField = "id"; public static boolean exists(long id) throws Exception { Db db = Util.getDb(); Query q = db.query(); q.select( idField ).whereLong(idField, id).limit(1).get(table); return q.hasResults(); } //snip.. } I'm then trying to extend from it, in the following way: public class User extends BaseModel { static String table = "user"; //snip } However, if I try to do the following: if ( User.exists( 4 ) ) //do something Then, rather

What is better: Static variable V.S. Asp.NET Application Session?

烂漫一生 提交于 2019-11-29 06:56:56
Say you want to share some resource, like a class or a variable across all threads/sessions within a ASP.NET web application. What is better? 1) A static variable having thread-safe accessors to that static variable? 2) Or a ASP.NET application session variable? If you only have one of those, there is little difference. If you have several, you should use static variables rather than Application variables. The Application.Lock method will lock all Application variables, while you can use separate syncronisition identifiers for your static variables, so that each lock only affects the code that

C++ definition of dllimport static data member

偶尔善良 提交于 2019-11-29 02:17:38
问题 I do have a class which looks like below: //.h file class __declspec(dllimport) MyClass { public: //stuff private: static int myInt; }; // .cpp file int MyClass::myInt = 0; I get the following compile error: error C2491: 'MyClass::myInt' : definition of dllimport static data member not allowed what should I do? 回答1: __declspec(dllimport) means that the current code is using the DLL that implements your class. The member functions and static data members are thus defined in the DLL, and

Static Method Memory Allocation

寵の児 提交于 2019-11-29 02:01:19
We have two classifications heap and stack . When a object is created, memory for object is stored in heap. What if the class has static methods ,which can be called using class name. If object is not created then how will it allocate memory and if it does where will it allocate memory? It depends on the JVM, but static fields are usually stored in a special object on the heap. (You can see it in a heap dump) When the ClassLoader is unloaded, its classes and their static "objects"/fields are also cleaned up. The only thing different about the static "object" is you can't get a reference to it.

Member variables in ES6 classes

巧了我就是萌 提交于 2019-11-29 01:02:47
Is there any way to use the ECMAScript6 class notation to declare either a static class variable or a default value for an instance variable? Without class what I have in mind would be written as function MyClass(arg) { if(arg) this.arg = arg; } MyClass.classVariable = 42; MyClass.prototype.arg = "no arg specified"; The most obvious ES6-like notation in my opinion would have been class MyClass { constructor(arg) { if(arg) this.arg = arg; } static let classVariable = 42; let arg = "no arg specified"; } But this doesn't work, since according to the current spec draft the only productions of

How to initialize static members in the header

柔情痞子 提交于 2019-11-29 00:25:04
问题 Given is a class with a static member. class BaseClass { public: static std::string bstring; }; String has obviously to be default-initialized outside of the class. std::string BaseClass::bstring {"."}; If I include the above line in the header along with the class, I get a symbol multiply defined error. It has to be defined in a separate cpp file, even with include guards or pragma once . Isn't there a way to define it in the header? 回答1: You can't define a static member variable more than

Why would code explicitly call a static method via a null pointer?

与世无争的帅哥 提交于 2019-11-28 23:33:26
问题 I've seen code like this in a couple of old projects: class Class { static void Method() {} }; ((Class*)0)->Method(); This code contains undefined behavior because it includes dereferencing a null pointer (no matter what happens afterwards). It really makes no sense - the cast is there to feed the type name to the compiler and whoever wrote the code above could have written this instead: Class::Method(); and the latter would be okay. Why would anyone write the former code? Is it a known idiom

Private class functions vs Functions in unnamed namespace

拟墨画扇 提交于 2019-11-28 22:01:57
问题 I've found myself that I tend not to have private class functions. If possible, all candidates to private class function rather I put in to unnamed namespace and pass all necessary information as function parameters. I don't have a sound explanation why I'm doing that but at least it looks more naturally to me. As a consequence I need to expose less internal details in the header file. What is your opinion - is it correct practice? 回答1: In the semi large projects where I usually work (more

What are static variables?

六眼飞鱼酱① 提交于 2019-11-28 21:37:39
What are static variables designed for? What's the difference between static int and int? The static keyword has four separate uses, only two of which are closely related: static at global and namespace scope (applied to both variables and functions) means internal linkage this is replaced by unnamed namespaces and is unrelated to the rest in particular, others tend to imply some sort of uniqueness, but internal linkage means the opposite : you can have many objects with the same name, as long as each has internal linkage and you only have one per translation unit static data members are