Questions tagged [instance-variables]

In object-oriented programming with classes, an instance variable is a variable defined in a class (i.e. a member variable), for which each object of the class has a separate copy.

In object-oriented programming with classes, an instance variable is a variable defined in a class (i.e. a member variable), for which each object of the class has a separate copy.

1506 questions
-3
votes
1 answer

Why does it not need to use the "self" parameter like in Python to define Class in C++?

I am a beginner of C++ with Python background. I have some ambiguity about the process of obtaining instance attributes in Python classes and C++ classes. As follows, I list two classes that have the same function in Python and C++ respectively. My…
Ili a
  • 1
  • 1
-3
votes
1 answer

I guess some Ruby internals

class MyClass def instance_variable=(var) puts "inside getter" instance_variable = var end def function_1 self.instance_variable = "whatever" end def function_2 @instance_variable = "whatever" end end myclass =…
krzysiek
  • 465
  • 5
  • 16
-3
votes
1 answer

How to store data taken from user in an array

I am developing a hospital management system on command line. I want to take a value from a user, assign it to an instance variable, and further store it in an array. The code is as follows: def doctor_details @doctors = Array.new puts 'Enter…
Moriarty
  • 159
  • 2
  • 16
-3
votes
1 answer

Objects' variables changed by method in another class

I created a simple method in my CardGames class that replicates a card game to play around with conditional statements. I call the method from a separate Player class because the player earns/loses points based on the card. I want the method to be…
gunsnfloyd
  • 49
  • 4
-3
votes
1 answer

How is it possible for variables to be initialized outside of the constructor?

Variables only get memory allocation during object creation, so why does assigning a value to a variable outside a constructor produce no error? Moreover what is the location of this assigned value as no particular object is created?
-3
votes
2 answers

When to use instance variables instead of properties in objective-C?

With the Objective-C's @property directive I can declare properties for which getter and setter methods will be created automatically. I can't think of any particular reason to use instance variables where I would have to write my own setter and…
ilgaar
  • 804
  • 1
  • 12
  • 31
-3
votes
2 answers

Java Object Creating issue

I am trying to pass a String of "A" to a constructor. The problem I am running into is the instance variable that has to be set isn't filled with anything. Now I am trying something different than I ussually do. As soon as I try anything with the…
Marciano
  • 142
  • 1
  • 11
-3
votes
2 answers

What is the best way to add instance variables acquired from the user inputs in different methods out of class

I want to add the values of variables declared from user inputs in different methods so how to sum all the variable values which are results from different methods like sum=a+c+d and declare some statements depending on the sum. class Mets …
-3
votes
1 answer

how do I access an instance variable in ruby on rails

@revenue_category is a ActiveRecord object that contains one row from my revenues table which was retrieved with Revenue.all(:order => "revenue_made") and then sorted out. So each of its attributes is a column name, and the value of the attribute is…
Ryan
  • 5,644
  • 3
  • 38
  • 66
-4
votes
2 answers

How do I pass variables between methods in C#?

I am trying to build a program, but I realized I can't access a certain variable because it's created in another method. How do I transfer a variable to another method? This is an example of what I'm trying to do: namespace Example { public…
Arandomguy
  • 15
  • 4
-4
votes
1 answer

Accessing member attributes of a class when we the member attribute's name is stored in another variable

We are facing a problem in accessing member attributes of a class when we the member attribute name is stored in another variable. For example : We have a class A, which has member attributes is var1, var2, var2, and upto var 100. From another…
Rahul Yadav
  • 31
  • 1
  • 7
-4
votes
3 answers

How to use 1 setter for multiple instance variables

So, I am trying to create one setter method for multiple instance variables of one constructor. I have already made a getter that works this way: public int getQuiz(int num) { int quiz = -1; int[] tempArray = {this.qz1, this.qz2, this.qz3,…
simon.b
  • 1
  • 1
  • 5
-4
votes
2 answers

Modify an instance variable within the Main class or main method

I would like to be able to modify the value of a local variable defined in a constructor within the class via the main driver class at some point while running the program. How would I be able to achieve this? Here is a sample of a constructor that…
JKawa
  • 179
  • 2
  • 6
  • 20
-4
votes
1 answer

How to access struct's instance fields from a function?

Assuming, that I have a Graph struct, like so: type Graph struct { nodes []int adjList map[int][]int } // some methods on the struct // constructor func New() *Graph { g := new(Graph) g.adjList = make(map[int][]int) return…
CuriOne
  • 103
  • 3
  • 8
-4
votes
1 answer

Declare an Instance Variable to Reference an Object

So we are making an Avatar for a game and we have already created the Avatar as an Avatar class file. Our problem is that in our class file Game we are having difficulty declaring Avatar as an instance variable. The code is as follows: public…
1 2 3
99
100