random between certain ranges

skaryChinezeGuie

Well-known member
Joined
Apr 23, 2006
Messages
94
Programming Experience
Beginner
I was trying to make the prices change at random between certain ranges every time the location changes. here's what i did.
public variables:

Public NewLocation As String
Public LocOkolona As String = "Okolona"
Public LocBuechel As String = "Buechel"
Public LocFernCreek As String = "Fern Creek"
Public LocPRP As String = "PRP"
Public LocStMatthews As String = "St. Matthews"
Public LocJtown As String = "J-Town"
Public LocFairdale As String = "Fairdale"
Public LocValleyStation As String = "Valley Station"
Public LocMiddleTown As String = "Middletown"
Public LocPortland As String = "Portland"

Public randomObject As Random = New Random
Public acidPrice = randomObject.Next(1000, 4500)
Public cocainePrice = randomObject.Next(15000, 29000)
Public PcpPrice = randomObject.Next(1000, 4500)
Public HeroinPrice = randomObject.Next(5000, 18000)
Public ecstasyPrice = randomObject.Next(15, 55)
Public weedPrice = randomObject.Next(300, 1000)
Public hashPrice = randomObject.Next(5000, 10000)
Public speedPrice = randomObject.Next(75, 200)
Public shroomsPrice = randomObject.Next(600, 1200)

main form:

Private Sub BtnMap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnMap.Click
Dim nFrmMap As Form = New FrmMap
nFrmMap.ShowDialog()
LblPlace.Text = NewLocation
TxtPriceAcid.Text = acidPrice
TxtPriceCocaine.Text = cocainePrice
TxtPriceEcstasy.Text = ecstasyPrice
TxtPricePCP.Text = PcpPrice
TxtPriceHeroin.Text = HeroinPrice
TxtPriceWeed.Text = weedPrice
TxtPriceShrooms.Text = shroomsPrice
TxtPriceSpeed.Text = speedPrice
TxtPriceHash.Text = hashPrice

but it only works once. it does seem to work though.
Thanks in advance.:D
 
You declare a variable and (at the same time) assign it a value based on a formula which calculates only at the time the variable is declared. If you want those variables to be another value later you have to change them. What you can do is to create a method that will change all those values each time you call it. Example:
VB.NET:
Sub getNewValues()
acidPrice = randomObject.Next(1000, 4500)
cocainePrice = randomObject.Next(15000, 29000)
PcpPrice = randomObject.Next(1000, 4500)
HeroinPrice = randomObject.Next(5000, 18000)
ecstasyPrice = randomObject.Next(15, 55)
weedPrice = randomObject.Next(300, 1000)
hashPrice = randomObject.Next(5000, 10000)
speedPrice = randomObject.Next(75, 200)
shroomsPrice = randomObject.Next(600, 1200)
End Sub
In your click event you would then:
VB.NET:
...
nFrmMap.ShowDialog()
[B]getNewValues()[/B] 
LblPlace.Text = NewLocation
...
 
Oh, I almost forgot to nag at you about the bad coding ;) You must strong type those variables. The Random class Next method returns Integer values and that is also what you must type your variables, by default they are of type Object which is very bad coding when you don't need it. Carry on. :)

While I'm at it... Also try to give your forum requests some meaningful titles. Always posting "Drug Wars" doesn't mean a thing related to what you really ask. (Thread title changed.) This is also not a request any way related to Forms, it is variable values you ask about here so I will shortly move this to the VB.Net General forum.
 
my bad my bad i don't even remember what strong typing is. I was put into a 2-years experience required class with no experience at all. All things considering I'm doin pretty good. but thanks.
 
2yrs experience class? - you wouldn't need more than a few weeks spare time to get into the basics of VB.Net. I wish you good fortune in the forthcoming.
 
no vb.net

according to the teacher we should've had 2 years experience with vb, sql, front page, asp, and some other stuff.
 
since i've been helping a matt guy on making a dopewars game, here's the incomplete dopewars game i made for him as reference, it's got everything that you're asking about so far (just needs the code for the cops)
 
Back
Top