Skip to main content
gamedev.net
🔒 Locked

VB Module Variable

Started by Mark04 Mar 23, 2005 at 10:46 AM 6 replies 850+ views
Original Post
Mark04
Mark04
Hi, If I am using a Public Const in a Module how can I change the value in a form, e.g. In Module1: Public Const Length = 10 In Form 1 I have a text box called Txt_Length I want to be able to update the value but if I use the code Public Const Length = Form1.Txt_Length I get the following error: "Constant Expression Required". Any info on how I can resolve this? Thanks Mark.
Thr33d
Thr33d
Consts aren't meant to change (that's the point.)
omit the Const part and you'll be fine

Public Length
or Public Length as Long

or something like that.
You'll probably either want to assign the value at form startup (form_load) or form_initialize or maybe from sub main in the module (if you're starting from the module). Or you'll want to update it whenever the user changes (?) or leaves (?) the textbox in question. (textbox_change and textbox_lostfocus) or something.
VB is complaining 'cause the textbox's text property is dynamic, and the const can't take a dynamic value.


(misc/off-topic:
Depending upon the version of vb, it may not be the sharpest knife in the drawer. I had one program (in vb6 I believe) pass a const byref, then pass it again byref, at which point my program tweaked with it. VB didn't catch that it was a const, and allowed the modification. Weird... not right... Took a little while to figure out why that was happening.)

-Michael
Mark04
Mark04
Hi,

Thanks for the reply. I tried modifying my code as follows:

In the module:

Public Length as Long
Length = Form1.Txt_Length

When I run the code now I get an "Invalid Outside Procedure" message?

Any ideas on this? Thanks

Mark.
Thr33d
Thr33d
Quote:
You'll probably either want to assign the value at form startup (form_load) or form_initialize or maybe from sub main in the module (if you're starting from the module).


That's why consts (in vb) are nice: you can assign the value right there.
In this case you'll have something in the form like

Public Sub Form_Load()

'...

End Sub

You can assign values in there (where I put the ellipsis), if that's where your program starts, or in the module in

Sub Main()

if your program starts there.

Spend time learning what they're teachin' ya,
you'll get it quickly enough.

-Michael
Mark04
Mark04
Hi,

The code in my module now reads:

Public Length as Long

I have set Form_Load as the start-up and have the following in form_load:

Length = TxtLength

But when I try to run the program it goes to the module code first, and the value is not set until run-time...??

Mark.
alexmoura
alexmoura
The value will be set when the form is created and it's load event runs - trying to use it before means that you're trying to use the textbox before it exists.
Mark04
Mark04
Quote:
Original post by alexmoura
The value will be set when the form is created and it's load event runs - trying to use it before means that you're trying to use the textbox before it exists.


Hi,

My module reads

Public Const Length = 20

But I want to be able to say = Form1.TxtLength.

If I type this I get the "Constant Expression Required" error.

Also, if I put that line into form_load I get the message "constants not allowed as public members of object module"

Could you give me a code sample so I can understand a bit more about how I can do this?

Thanks

Mark
technomancer
technomancer
Quote:

My module reads

Public Const Length = 20

But I want to be able to say = Form1.TxtLength.



should be

public const DefLength = 20
public Length as integer

on form_load do this

Length = DefLength

on Txt_Length_LostFocus do this

Length = cint(txt_Length)

this should do what you need

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.