Questions tagged [variable-initialization]

In computer programming, initialization is the assignment of an initial value for a data object or variable.

In computer programming, initialization is the assignment of an initial value for a data object or variable. The manner in which initialization is performed depends on programming language, as well as type, storage class, etc., of an object to be initialized. Programming constructs which perform initialization are typically called initializers and initializer lists. Initialization is distinct from (and preceded by) declaration, although the two can sometimes be conflated in practice. The complement of initialization is finalization, which is primarily used for objects, but not variables.

119 questions
0
votes
0 answers

initialize a PHP variable if not set

In my code (with php notice errorhandling) I have lots of these blocks inside loops: $someVar=array(); ... foreach (...){ if(!isset($someVar["something"])){ $someVar["something"]=0; } $someVar["something"]++; } another solution would be…
rubo77
  • 19,527
  • 31
  • 134
  • 226
0
votes
2 answers

C Variable has incomplete initializer

I am trying to make a struct with a default value, as described here: Default values in a C Struct. However, I have this C code, inside a header file: /* tokens.h */ typedef struct { char *ID; char *KEY; char *TYPE; } tokens; const…
Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
0
votes
1 answer

C# destroying variables to free up memory

Given I loop the execution of function Foo multiple times: int Foo(int a) { int b = 5; return a * b; } I believe the variable "b" is gets initialized many times (as many times as I initialize function Foo). Assuming I don't want to move "b" outside…
Tooa1
  • 23
  • 4
0
votes
2 answers

java: ActionListener variable contains action to modify itself - 'Variable might not have been initialized'

I have a bit of code that does this: Creates an ActionListener a. Removes itself from the button that it will be attached to (see 2.) b. Does some other stuff Adds that ActionListener to a button (in code:) ActionListener playButtonActionListener…
Eric Reed
  • 377
  • 1
  • 6
  • 21
0
votes
0 answers

Why is integer variable showing a warning sign in Eclipse?

In the below picture, why is totalNotes variable shown with a warning sign in Eclipse ? If it is because I have initialised totalNotes only inside for loop, then by that logic shouldn't costOfSweet variable also be marked with a warning sign ? Code…
Kaushik
  • 553
  • 3
  • 11
  • 27
0
votes
2 answers

Module variable can't be initiated with DOM elements

I'm trying to keep my Javascript code inside of modules, but I am not being able to initialize the module's variables with DOM elements. Everything I have tried have only returned empty variables. index.html
FTM
  • 1,887
  • 17
  • 34
0
votes
1 answer

C# an organized way to hide (clean up) many declared variables from my main?

So if I have a lot of long global string variables in my main that end up taking up a lot of lines in my main (around 500 to give you an idea) is there a way I can hide them somewhere else but still access them in my main? To better clarify, if this…
0
votes
2 answers

Using loop to print out triangle

The task is to print the following shape using while loop only. * ** *** **** ***** ****** ******* ******** ********* The following code is what I have tried already, but it doesn't work unfortunately: #include "stdafx.h"//Visual Studio…
Dave Nguyen
  • 129
  • 1
  • 1
  • 8
0
votes
4 answers

In Java, how can I use a variable initialized inside a try/catch block elsewhere in the program?

I have a basic quadratic formula program, but I've modified the beginning to end the program if any value other than a double is entered. However, because I've done this, I can't seem to be able to use the value inputted anywhere else in the…
0
votes
1 answer

Changing model during training[tensorflow]

I am creating a model in tensorflow with all layers having relu as the activation layer. However, when the batch size increases to 500, I want to change the model such that the second last layer to the output layer has sigmoid activation layer.…
0
votes
2 answers

What does variable-initialization line do in VB.Net?

Can someone please tell what the following line of VB.Net is initializing: Dim x As SomeType() = New SomeType(0) {} What holds x variable? Is it an array? How can it be translated to C# for example? I guess SomeType is probably an anonymous type,…
mikeg
  • 63
  • 3
  • 10
0
votes
2 answers

How do compilers detect usage of unassigned local variables?

Compilers for languages like Java or C# will complain if you attempt to use a local variable which has not (definitely) been assigned/initialized yet. I wonder how that functionality is implemented in the compiler. Obviously, the information about…
0
votes
2 answers

variable initialization of a class

I have a doubt regarding the initialization of variables of a class. When usually declare a class with the variables and I initialize (even if they are final) I do it via the constructor. For example: class Example {     private int a, b;        …
mikelplhts
  • 1,181
  • 3
  • 11
  • 32
0
votes
1 answer

Swift: Prime Number app: Variable initialisation

Im quite new to programming, so hopefully someone can explain this to me in plain english.. Im trying to build an app in Swift that tells you wether the number inputted is a prime number or not. If it isn't prime, it should also tell you the…
sar91
  • 125
  • 1
  • 4
0
votes
2 answers

Loop performance for local variable

Is there any performance penalties has 1st sample vs. 2nd one and why? // 1. variable is declared inside the loop body while(isSomethingTrue == true) { std::string str; findStr(str); // str initialization processStr(str); // processStr does…
Loom
  • 9,768
  • 22
  • 60
  • 112