|
-
May 15th, 2000, 11:31 AM
#1
Thread Starter
Lively Member
In Class1, there's a public variable Flag of type boolean.
The defined class is used in most forms, so a variable is defined in the class instead declaring the same variables on all required forms.
When I call a function, passing this calss variable as parameter (by reference) to change the value of Flag, the value does not change. Why?
Code:
' code declared in a form
private sub Command1_Click()
dim c as new class1
c.Flag = False
sub1 c.flag
msgbox c.Flag ' The value is still false afterward
end sub
' code declared in main module
private sub sub1(Value as boolean)
Value = True
end sub
I'm using VB5 SP3.
-
May 15th, 2000, 02:46 PM
#2
Addicted Member
Hi Carolyn,
Try putting the c.Flag into a variable and then work with the variable like:
' code declared in a form
Private Sub Command1_Click()
Dim Value As Boolean
Dim c As New Class1
c.Flag = False
Value = c.Flag 'Put c.Flag into Value
sub1 Value
MsgBox Value ' Now Value assumes the value TRUE
End Sub
' code declared in main module
Private Sub sub1(Value As Boolean)
Value = True
End Sub
André
-
May 15th, 2000, 03:50 PM
#3
Frenzied Member
tht's not the reason FantastichenEin it's nowt to do with scope because c.flag is not being refernced within the sub1
-
May 15th, 2000, 04:01 PM
#4
Fanatic Member
I have to agree with Mark on this one. What you should be looking at is a function.
Code:
' code declared in a form
private sub Command1_Click()
dim c as new class1
c.Flag = False
c.flag = sub1
msgbox c.Flag ' The value is still false afterward
end sub
' code declared in main module
private Function sub1() As Boolean
sub1 = True
end sub
Iain, thats with an i by the way!
-
May 15th, 2000, 09:06 PM
#5
transcendental analytic
Why are you guys making things so complicated when they aren't?
private Function sub1() As Boolean
sub1 = True
end sub
Replace that function with a single keyword: True
And for the class1, are you sure you have a public variable named flag that is boolean? Or is it a property get/let? You may have a bug in that.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
May 16th, 2000, 11:38 AM
#6
Thread Starter
Lively Member
Thank you for all your replies.
'Flag' in the class is declared as a public variable, not property.
I have serveral functions that need to do something, depending on the Flag, and some set the flag to T or F after doing something.
I've done serveral tests. The value of 'Flag' just refused to change when passed as parameter.
Isn't passing the variable by reference means passing the address, and the function which sets this variable access the address directly?
-
May 16th, 2000, 12:29 PM
#7
Hyperactive Member
Passing Class data Members by reference to functions does not work in VB.
Classes just plan suck in VB, although they are getting better. Go Java & C++!
Just my $0.03
-
May 16th, 2000, 12:36 PM
#8
Thread Starter
Lively Member
MrShickadance
Thank you for confirming that class meembers cannot be passed by reference.
I agre that VB is not a good development tool. It doesn't have the OO that other tools (SQL*Windows and PowerBuilder) provide. I've had various problems with it. Unfortunately, I'm stuck at the moment. I'll eventually move to C++ or Delphi.
-
May 16th, 2000, 01:20 PM
#9
Fanatic Member
Public Vars in Classes seems to go against the whole point of OO though if you ask me. This works though...
Code:
'class mod
Private bFlag As Boolean
Public Property Get Flag() As Boolean
Flag = bFlag
End Property
Public Property Let Flag(Value1 As Boolean)
bFlag = Value1
End Property
' form code
Private Sub Command1_Click()
Dim C As Class1
Set C = New Class1
C.Flag = False
Call sub1(C)
MsgBox C.Flag ' The value is Now True !!
End Sub
Public Sub sub1(ByRef Value As Class1)
Value.Flag = True
End Sub
If you're using public vars, just stick to modules, every thing is simpler.
This (OO) way is more code but OO always is as the extra property code is what makes it reusable
My 4c worth
Cheers
Paul Dwyer 
Network Engineer
Aussie In Tokyo
Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)
-
May 16th, 2000, 02:23 PM
#10
Member
Hi guys
You can not pass the class memebers by ref to a function or sub procedure. But you can pass the entire class as by ref.
The following code works fine.
Code:
'<<<CLASS MODULE (Class1.cls)>>>
Public Flag As Boolean
'<<<FORM MODULE (Form1.frm)>>>
Public Sub Sub1(ByRef a As Object)
a.flag = True
End Sub
Private Sub Command1_Click()
dim c as new class1
c.Flag = False
Sub1 c
MsgBox c.Flag
End Sub
-
May 16th, 2000, 02:30 PM
#11
Fanatic Member
Isn't that what I did?
(just without the Public Var)
Is it worse to misunderstood than ignored??? 
See Ya
Paul Dwyer 
Network Engineer
Aussie In Tokyo
Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)
-
May 16th, 2000, 06:59 PM
#12
New Member
I should really get some work done, but...
Yes, Carolyn, VB's OO abilities (inabilities?) are questionable at best.
To everyone who suggested band-aiding the problem with a return instead of a pass-by-reference. For shame, you people need to get out and learn a language that supports classes and pass-by-reference/value correctly.
I have a found something odd in using API calls to Registry queries. I pass in a buffer, and have to explicitly say "ByVal" in the function CALL. The function sets the queried value to the buffer I pass, and returns a pass/fail code.
Troubles me.
-Travis
Travis, BS in CSC
MQ/Series Administration
VB6E on NT4sp4
-
May 16th, 2000, 08:07 PM
#13
transcendental analytic
I agree, don't mix up objects with vars.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
May 17th, 2000, 07:27 AM
#14
Fanatic Member
I'm not going to defend VB's OO implimentation but it's all we have till the next version so...
Use what works, the language will improve (although no faster for whinging avout it)
Or go buy BCB
Paul Dwyer 
Network Engineer
Aussie In Tokyo
Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|