assign object array to class attribute vba - vb6

I have maintain array of class objects using another class member variable. I was unable to set the object array to the class variable. Able to call the setter method but gives error "can't assign a array"
Class module AA
private c as integer
private d as integer
Class module B
private a(50) as AA
public sub setA(byref a1() as AA)
a = a1 ' this assignment not work.
end sub
public function getA() as AA()
getA = a ' this work
end function
Module main
dim tags() as A
dim tag as A
with B
Redim preserve tags(ubound(.getA()) ' get the class module b array and set the element as 50
for i =0 to ubound(tags)
if tags(i) is nothing then
tag.setC(3)
tag.setD(5)
tags(i) = tag
end if
next i
.setA tags ' call the setter method but gives error can't assign a array
end with

You cannot assign to an array if you declare the size of the array. Try something like this:
Class module B
private a() as AA
public sub setA(byref a1() as AA)
a = a1 ' this should work now.
end sub
public function getA() as AA()
getA = a ' this work
end function
public sub class_initialize
redim a(50)
end sub

Related

Initializing class attribute using a method

The goal is to initialize a class attribute using a class method that's overridden in sub-classes. Following is the definition of my ruby classes:
class A
class_attribute :query
self.query = self.generate_query
def self.generate_query
return "abcd"
end
end
class B < A
def self.generate_query
query_part_1 = "ab"
return query_part_1 + generate_query_part_2
end
def self.generate_query_part_2
return part_2
end
end
The reason I want to do this is because query is a constant string per class and should not be created again on instantiation but it's a complex string which is generated in multiple independent parts. Separating this logic out in methods would make the code cleaner. However, with this code, I get the undefined method generate_query for class A.
I have tried lazy initialization of the class attribute while instantiating the class like the following:
def initialize
query = self.class.get_query
end
def self.get_query
self.query = self.generate_query if self.query.nil?
end
However, this initializes the query to same value for both class A and B if A is instantiated first because self.query.nil? would then return false for B also.
The solution to your problem is simple:
You are calling self.query = self.generate_query before your generate_query method has been defined! Remember - Ruby is interpreted top to bottom and your class body is no different. You cannot call a method before it is defined.
Simply changing the code around to
class A
class_attribute :query
def self.generate_query
return "abcd"
end
self.query = self.generate_query
end
will make it work, but then you will have another problem, as the line self.query = self.generate_query will only get evaluated once in your class - B will reference the "abcd" query, not "ab2".
To achieve the behavior you want - you need to define a getter method yourself which acts as an attribute (class_attribute does the same thing under the hood btw)
Solution
class A
def self.query
#query ||= self.generate_query
end
def self.generate_query
return "abcd"
end
end
class B < A
def self.generate_query
query_part_1 = "ab"
return query_part_1 + generate_query_part_2
end
def self.generate_query_part_2
return '2'
end
end

Vbscript multiple Statements for single Return Statement

I am developing a VBScript like follows
Dim a
Set a = New Class1
a("text").doSomething 'Has to execute doSomething in Class1
a("text").anotherSomething 'Has to execute doSoemthing in Class2
class Class1
Dim b
Dim c
public default Function init(str)
Set b = New Class2
Set c = New Class3
'Some more operations to perform
If **What is the condition can be?** Then
Set init = c
Else
Set init = b
End If
End Function
End class
class Class2
public Function doSomething()
'Stuff to do something
End Function
End class
class Class3
public Function anotherSomething()
'Stuff to do something
End Function
End class
Here, Object "a" has parameters and this parameter can be same.
So, i can't keep is parameter "=" or "<>"
And, I can't place those functions in Class1.
So, What can be that condition which can decide.
It looks like you need to leverage delegators. Basically you need internal objects that are of the type you want to access methods that would be in your parent classes in OOP.
Here is an example:
Class ScreenPoint
'' Properties
'Ancestor Point2D
Private P2D
'Point color
Private Color
'----------------------
'' Methods
'Constructor - called automatically
Private Sub Class_Initialize()
Set P2D = new Point2D
End Sub
'----------------------
'Destructor - called automatically
Private Sub Class_Terminate()
Set P2D = Nothing
End Sub
'----------------------
'A pair of methods to access private property X
Property Get X
X = P2D.X
End Property
Property Let X(ByVal in_X)
P2D.X = in_X
End Property
'----------------------
'A pair of methods to access private property Y
Property Get Y
Y = P2D.Y
End Property
Property Let Y(ByVal in_Y)
P2D.Y = in_Y
End Property
'----------------------
End Class
If you are lost, try reading the source article here: http://automation-beyond.com/2008/11/16/oop-vbscript-2/

Recognizing an array of constructor

What can I do to recognize an array which is declared in a constructor, by method, in this class?
I received an error:
undefined local variable or method 'myArray'
class Calc
def initialize()
myArray = []
end
def add4ToArray()
myArray.push(4)
puts myArray.size
end
end
obj1 = Calc.new
obj1.add4ToArray()
You need to declare it as an instance variable as opposed to a local variable. In the constructor, use #myarray = [].

Can we write class inside VBScript function?

Function CWSLogin(strApplicationPath,strUser,strMember,strPassword)
Class TryCatchFinally
Private Sub Class_Initialize 'Try
End Sub
End Class
End Function
No. Functions are in classes where they become known as methods.

Ruby- read the value of a variable in another class?

I have something like the following
class A
def initialize
#var = 0
end
def dosomething
#var+=1
end
end
class B < A
def initialize
super
end
def func
puts #var
end
end
The problem is when I call
a = A.new
a.dosomething
b = B.new
the value which #var returns is 0 how would I change my code so it would return the "new" value of var (1)?
Quick answer, for if you actually understand Classes, Inheritance and Objects : replace #var (an instance variable, and therefore different in a and b) with ##var (a class variable, and therefore the same in all instances of class A).
Otherwise, your question indicates you have a fundamental misunderstanding of what's going on with classes, objects and inheritance.
Your code does the following:
Defines a class, called A. This is essentially a blueprint from which you can create objects.
Declares that when an object of type A is created, that object should be given it's own private copy of an attribute, called var, which is set to 0.
Declares that objects of type A can be asked to dosomething, which increases the value of that object's var by 1.
Defines a class called B, which is a special case of an A
Therefore, in your second snippet, you create an object a, which is an A. It has its own attribute called var, which is set to 0 and then incremented. You then create b, which is a B (and is therefore also an A). b has its own attribute called var, separate from a's var, which is set to 0.
Variables like #var are called instance variables because they are unique to every instance of a class. You've created two separate instances, a and b, and they have their own instance variables.
You can use class variables if you want to see them in your subclass:
class A
##var = 0
def dosomething
##var += 1
end
end
class B < A
def func
puts ##var
end
end
You can also use class-level accessor methods:
class A
class << self
attr_accessor :var
def dosomething(n)
self.var = n
end
end
end
class B < A
def func
puts A.var
end
end
irb(main):031:0> A.dosomething(5)
=> 5
irb(main):032:0> b = B.new
=> #<B:0x2b349d>
irb(main):033:0> b.func
5
Note that inheritance is not needed for this to work.
You could use class variables:
class A
##var = 0
def dosomething
##var += 1
end
end
class B < A
def func
puts ##var
end
end
a = A.new
a.dosomething
a.dosomething
b = B.new
b.func # => 2
#var is an instance variable, each instance of this class has its own value of this variable. So it is correct that b does return 0, since it is a different instance compared to a.
To update the value in b use:
b = B.new
b.dosomething
Here is more information on instance variables.
Or if you want a class variable (ie a variable which is the same in all instances of that class), use ##var. Then your given example works.
Here is some more information on class variables.
Which solution you need, depends on your application.
You need to use class variables, not instance variables. Try something like this:
class A
##var = 0
def dosomething
##var+=1
end
end
class B < A
def func
puts ##var
end
end

Resources