initialization

Where does JSP initialize?

I have a project which works fine. bookstore example I didn't understand where it initialize JSP's variable? There is no beans. All we have is Java class. JSP calls java class's variable but how? I searched into all the configuration files but nothing found. public class Bookstore { private final Map<String, Item> items = new Tr...

Initializing a string array in a method call as a parameter in C#

If I have a method like this: public void DoSomething(int Count, string[] Lines) { //Do stuff here... } Why can't I call it like this? DoSomething(10, {"One", "Two", "Three"}); What would be the correct (but hopefully not the long way)? ...

Python __init__ setattr on arguments?

It seems that often __init__ methods are similar to this: def __init__(self, ivar1, ivar2, ivar3): self.ivar1 = ivar1 self.ivar2 = ivar2 self.ivar3 = ivar3 Is there someway to turn the arguments into a list (without resorting to *args or **kwargs) and then using setattr to set the instance variables, with the name of the p...

When are static C++ class members initialized?

There appears to be no easy answer to this, but are there any assumptions that can be safely made about when a static class field can be accessed? EDIT: The only safe assumption seems to be that all statics are initialized before the program commences (call to main). So, as long as I don't reference statics from other static initializa...

C# assign values of array to separate variables in one line

Can I assign each value in an array to separate variables in one line in C#? Here's an example in Ruby code of what I want: irb(main):001:0> str1, str2 = ["hey", "now"] => ["hey", "now"] irb(main):002:0> str1 => "hey" irb(main):003:0> str2 => "now" I'm not sure if what I'm wanting is possible in C#. Edit: for those suggesting I jus...

Why do I have to assign a value to an int in C# when defaults to 0?

This works: class MyClass { int a; public MyClass() { int b = a; } } But this gives a compiler error ("Use of unassigned local variable 'a'"): class MyClass { public MyClass() { int a; int b = a; } } As far as I can tell this happens because in the first example, technically, the...

Are there any advantages in using block initialization?

Is there any reason to do use block initialization, like this: x = Observer.new do add_event(foo) some_other_instance_method_on_observer self.some_attribute = something end instead of initializing attributes using the dot operator on an instance variable like this: x = Observer.new x.add_event(foo) x.some_other_instance_method_...

Objective-C: Is an autoreleased initialisation followed by a retain wrong in a constructor?

In my @interface theres a NSArray *Monate followed by: @property (nonatomic, retain) NSArray* Monate; If i do: filePath = [[NSBundle mainBundle] pathForResource:@"SomeFile" ofType:@"plist"]; self.Monate = [NSArray arrayWithContentsOfFile:filePath]; in the constructor, it gets set to an autoreleased object (is that correct?). So s...

structure initialization

static struct Args { char* arg1; unsigned arg2; unsigned arg3; char* arg4; } arg; My program saves command line args to a structure. Sometime all of the members are set... sometimes only a couple of them. In the case where only arg1 is set, what would the best practice be to do with the rest of the members? Thanks. ...

initwithint warning, no method found?

I have a warning and just can't work out how to make it go away. In my .h I have this... -(void)restartTimer; Then the in my .m I have... -(void)restartTimer{ TimerViewController *TimerView = [[TimerViewController alloc] initWithInt:hStart number:mStart]; I get this error: Warning: no '-initWithInt:number.' method found. I a...

Static const string won't get initialized

I have some static const strings as private members of my C++ class. I am aware of the declaration in .h and definition (and initialization) in .cpp practice. In the class constructor I invoke a function that uses these static strings. Surprisingly when in constructor, the strings remain uninitialized (empty strings) which is creating a ...

initialize all hashes with a default_proc

I wanted to extend the Hash class so that all hashes get same default_proc when they're created. So I put this in my file: class Hash def initialize self.default_proc = proc { |hash, key| raise NameError, "#{key} is not allowed" } end end This works fine if I use this syntax h = Hash.new but not if I use h = {} Playing wit...

Ruby initialize method: setting instance variable with a hash key

Hello, I am facing a undefined local variable or method error when initializing the following in ruby: class Model attr_accessor :var1, :var2, :state def initialize (x, y, key) @var1 = x @var2 = y @state = every_state[:key] #this line produces the error @every_state = { :A => SateA.new, :B => StateB.new, :C =>...

Ruby Style: should initialize take a file with data or just the raw data as parameters

Hello, I was curious if anyone had insight on what is the best way for an object to load data from a file in Ruby. Is there a convention? There are two ways I can think of accomplishing this: Have the initialize method accept a path or file and parse the data within the initialize method, setting the object variables as well. Have th...

Instance Initializer vs private members

I have the following code :- public class Test5 { private int value ; public static void main(String[] args) { Test5 a, b; a = new Test5(); b = new Test5(){{ value = 1 ;}}; } } The following line shows an error :- b = new Test5(){{ value = 1 ;}}; non-static variable cannot be referenced from a s...

Initialization order with constructors in C++

By instantiating an object in C++ with the following class I get a segmentation fault or aborts, depending on the order declaring member variables. E. g. putting mMemberVar and mAnotherMemberVar after mAnotherCountVar results in a segfault. From this listing I removed a std::ofstream from the member variables, which caused the segmentati...

How to initialize a static member

Hi, I want to initialize two static data members. See the two files //Logger.h class Logger{ public: static LoggerConcrete error; static LoggerConcrete write; }; and //Logger.cpp Logger::error = LoggerConcrete(LOG_DEBUG); Logger::write = LoggerConcrete(LOG_DEBUG); The initilization of the two static ...

How to reuse a UILabel? (Or any object)

This: UILable *myLabel = [[UILabel alloc] init]; UILable *myLabel = [[UILabel alloc] init]; gives me a redefinition error. But this: for(i=0;i<5;i++) { UILable *myLabel = [[UILabel alloc] init]; // some label code here [self.view addSubview:myLabel]; [myLabel release]; } doesn't. So...

Objective C - Are SystemSoundID's (typedef'd UInt32) automatically assigned 0?

- (void)playAlarmSound:(NSTimer *)theTimer { static SystemSoundID soundID/* = 0 */; // ? if (!soundID) { soundID = [Utilities createSystemSoundIDFromFile:@"beep" ofType:@"caf"]; } ... } Is SystemSoundID (which is a UInt32) automatically assigned 0? or should I explicitly assign it? I have to do it this way and t...

How to initialize char array and empty it again and again in a loop before strcpy() in C?

I want to reinitialize d everytime in a loop char d[90]; while(ptr != NULL) { printf("Word: %s\n",ptr); //int k = 0; strcpy(d, ptr); d[sizeof(d)-1] = '\0'; //something more .... .... } ...