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
18
votes
4 answers

Instance Variables Inheritance

Can someone explain how a class can access the instance variables of its superclass and how that is not inheritance? I'm talking about 'The Ruby Programming Language' and the example class Point def initialize(x,y) # Initialize method @x,@y…
Zed
  • 5,683
  • 11
  • 49
  • 81
17
votes
3 answers

Rails -- self vs. @

I am following Michael Hartl's RoR tutorial, and it is covering the basics of password encryption. This is the User model as it currently stands: class User < ActiveRecord::Base attr_accessor :password attr_accessible :name, :email,:…
Kvass
  • 8,294
  • 12
  • 65
  • 108
17
votes
3 answers

C/C++ Possible to get a "list" of instance members by querying a class?

Suppose we have a struct in C++: struct foobar { int age; bool hot; String name }; Is there a way, programatically, to query the above struct to extract its instance members? For example: String[] members =…
user402642
17
votes
2 answers

"Access of shared member, constant member, enum member or nested type through an instance"

I wonder why Visual Studio is raising this warning: Access of shared member, constant member, enum member or nested type through an instance My code: Dim a As ApplicationDeployment =…
Petr Kováč
  • 365
  • 1
  • 2
  • 10
17
votes
3 answers

iVars references strong, weak or what?

In Obj-C, properties can be configured to be weak/strong. Instance variables. like following - @interface MyClass { NSObject *a; } Does the MyClass's object keep weak reference to a or strong, or something else? I think iVar is not released until…
user1559227
  • 1,021
  • 2
  • 10
  • 13
15
votes
6 answers

Java instance variables initialization with method

I am a little bit confused about the following piece of code: public class Test{ int x = giveH(); int h = 29; public int giveH(){ return h; } public static void main(String args[]) { Test t = new Test(); …
bucky
  • 392
  • 4
  • 18
15
votes
2 answers

Objective-C: Compiler error when overriding a superclass getter and trying to access ivar

I'm working on building an iOS 6 app. I have a class TDBeam which inherits from superclass TDWeapon. The superclass TDWeapon declares a @property in the TDWeapon.h file: @interface TDWeapon : UIView @property (nonatomic) int damage; @end I do not…
smileyborg
  • 30,197
  • 11
  • 60
  • 73
14
votes
4 answers

Declaring instance variables iterating over a hash!

i want to do the following: I want to declare the instance variables of a class iterating over a dictionary. Let's assume that i have this hash hash = {"key1" => "value1","key2" => "value2","key3" => "value3"} and i want to have each key as…
flyer88
  • 1,073
  • 3
  • 15
  • 33
14
votes
4 answers

Visual C# - Access instance of object created in one class in another

I apologize in advance with what will probably be a fairly easy/quick answer based on scope, but I've looked everywhere and am surprised to not come up with an answer. I have created a class called Soldier with roughly 100 class variables. I need a…
M. Black
  • 353
  • 1
  • 4
  • 20
13
votes
2 answers

Same instance variable for all actions of a controller

I have a rails controller with two actions defined: index and show. I have an instance variable defined in index action. The code is something like below: def index @some_instance_variable = foo end def show # some code end How can I access…
imran
  • 626
  • 2
  • 13
  • 24
13
votes
1 answer

In Ruby, why after starting irb, foo.nil? says undefined error, and @foo.nil? gives "true", and @@wah.nil? gives error again?

Same in Ruby 1.8.7 and 1.9.2: $ irb ruby-1.8.7-p302 > foo.nil? NameError: undefined local variable or method `foo' for # from (irb):1 ruby-1.8.7-p302 > @bar.nil? => true ruby-1.8.7-p302 > @@wah.nil? NameError: uninitialized…
nonopolarity
  • 146,324
  • 131
  • 460
  • 740
13
votes
3 answers

Why are instance variables considered bad practice by Apple?

In Apple's Programming with Objective-C the section on Encapsulating Data states that: You Can Define Instance Variables without Properties It’s best practice to use a property on an object any time you need to keep track of a value or another…
ColinE
  • 68,894
  • 15
  • 164
  • 232
13
votes
4 answers

Total newbie: Instance variables in ruby?

Pardon the total newbiew question but why is @game_score always nil? #bowling.rb class Bowling @game_score = 0 def hit(pins) @game_score = @game_score + pins end def score @game_score end end
George Mauer
  • 117,483
  • 131
  • 382
  • 612
13
votes
4 answers

Must all Python instance variables be declared in def __init__?

Or can they be declared otherwise? The code below does not work: class BinaryNode(): self.parent = None self.left_child = None Do they need to be declared in __init__?
aerain
  • 1,149
  • 3
  • 12
  • 17
12
votes
5 answers

In Ruby, how can I get instance variables in a hash instead of an array?

I have a Ruby class. I want to get an instance variable from an argument to a method in that class. I can do get all of the instance variables as an array: self.instance_variables However, I want to get the instance variable named arg,…
Totty.js
  • 15,563
  • 31
  • 103
  • 175