constants

Why can't I have “public static const string S = ”stuff"; in my Class?

怎甘沉沦 提交于 2019-12-20 08:00:47
问题 When trying to compile my class I get an error: The constant 'NamespaceName.ClassName.CONST_NAME' cannot be marked static. at the line: public static const string CONST_NAME = "blah"; I could do this all of the time in Java. What am I doing wrong? And why doesn't it let me do this? 回答1: A const object is always static . 回答2: From the C# language specification (PDF page 287 - or 300th page of the PDF): Even though constants are considered static members, a constant declaration neither requires

Where are literal strings placed and why can I return pointers to them?

孤人 提交于 2019-12-20 05:36:10
问题 I stumbled upon this function in an answer to this question: /* Note: I've formatted the code for readability. */ const char * getString() { const char *x = "abcstring"; return x; } I was surprised to discover that returning a pointer to the literal string worked, and didn't segfault like I thought it would. I always assumed that literals were pushed on to the stack or put in some other temporary memory, but where limited to the scope of the function. But here it seems that they are more

Accessing a constant

為{幸葍}努か 提交于 2019-12-19 18:53:49
问题 Why can't I access 'B' in the following from 'A' but can from the main environment? module A; end A.instance_eval{B=1} B #=> 1 A::B #=> uninitialized 回答1: The idiomatic way to do this would be A.const_set(:B, 1) A::B #=> 1 As to why it doesn't work, in Ruby 1.8 and 1.9.2+ (it was different in 1.9.1), constant lookup is lexically scoped. I found a good blog post with an explanation. To quote: Note that these rules apply to constant definition as well as lookup. In 1.8 and 1.9.2, a constant

Creating a constant dictionary object

若如初见. 提交于 2019-12-19 17:38:08
问题 I would like to accomplish something like what is being done in this post: Constants in Objective-C however, i would like to construct an NSDictionary. if i do something like: constants.h extern NSArray *const mFooKeys; extern NSArray *const mFooObjects; extern NSDictionary *const mFooDictionary; constants.m NSArray *const mFooKeys = [[NSArray alloc] initWithObjects: @"Foo", @"Bar", @"Baz", nil]; NSArray *const mFooObjects = [[NSArray alloc] initWithObjects: @"1", @"2", @"3", nil];

Can't add constant to vector in R

我们两清 提交于 2019-12-19 10:23:12
问题 I don't know what is happening, but I can't seem to add a constant to a vector. For example, typing in the console c(1,2,3,4)+5 returns 15 instead of (6,7,8,9). What am I doing wrong? Thank you for your help. 回答1: Someone.... probably you ... has redefined the "+" function. It's easy to do: > `+` <- function(x,y) sum(x,y) > c(1,2,3,4)+5 [1] 15 It's easy to fix, Just use rm() : > rm(`+`) > c(1,2,3,4)+5 [1] 6 7 8 9 EDIT: The comments (which raised the alternate possibility that c had instead

Why are we allowed to change values of “const” qualified variables?Why pointers are allowed for this,but not assignment?

大城市里の小女人 提交于 2019-12-19 09:01:22
问题 Consider the following 2 programs prog1 and prog2 .Here if I try to change the value of the const qualified variable i using a pointer ptr ,I get the warning( not error) "initialization discards qualifiers from pointer target type|" ,but the program runs nevertheless and displays the new value.But if I try to change the value of i in the second program using an assignment statement,I get the error (not warning) assignment of read-only variable 'i'| . Here are confusions arising from this

Should I store Enum ID/values in the database or a C# enumeration?

大城市里の小女人 提交于 2019-12-19 05:37:29
问题 Say my database tables have columns like UserType , SalesType , etc. Should I have database tables with UserTypeID , userTypeName or should I just create a C# enumeration? 回答1: What's wrong with both? If value's are user-defined or changing, definitely enum will not be suitable. If values are strictly non-changing (such as gender), you can have them as enums for ease of reference in the application and also in the DB as separate table to enforce foreign keys and as a reference. 回答2: It

dirname(__FILE__) on localhost

白昼怎懂夜的黑 提交于 2019-12-19 03:42:11
问题 I'm using WAMP and have a development site in the www directory. I want to use dirname(__FILE__) to define the path to the server root. Currently I'm using a config file which contains: define('PATH', dirname(__FILE__)); I'm including the config file in in my header.php file like this: <?php require_once("config.php") ?> Then, on my sub pages I use the constant PATH to define the path by including header.php . <?php require_once("../inc/header.php"); ?> However, my links are coming out like

Cannot declare Public static final String s = new String(“123”) inside an inner class

蹲街弑〆低调 提交于 2019-12-19 02:49:19
问题 I tried to declare a class as shown below class Outer{ private final class Inner{ public static final String s1 = new String("123"); public static final byte[] bytes = new byte[]{0x00, 0x01}; public static final String s2 = "123"; public static final byte byte1 = 0x02; } } In the above code s1 and bytes wont compile but s2 and byte1 compile. If I put the whole constant declaration in outer class it works fine. what am i missing. Any help? 回答1: Read Java Language Specification, 3rd ed, §8.1.3.

How do you store custom constants in Rails 4?

匆匆过客 提交于 2019-12-18 19:33:35
问题 I made some regular expressions for email, bitmessage etc. and put them as constants to #config/initializers/regexps.rb REGEXP_EMAIL = /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i REGEXP_BITMESSAGE = /\ABM-[a-zA-Z1-9&&[^OIl]]{32,34}\z/ and use it like if @user.contact =~ REGEXP_EMAIL elsif @user.contact =~ REGEXP_BITMESSAGE Is that's good practice? What's the best way to store them? 回答1: It makes sense, that's one of the possible approaches. The only downside of this approach, is that the