various cobra & .net questions re: static and global vars
Posted: Sun May 06, 2012 1:19 pm
folks:
the "program" below is a do-nothing-useful accumulation of every cobra/.net concept that's been confusing me for the last week as i struggle to learn cobra and .net. please forgive me for throwing so many issues at this forum in one posting, but i figure better to get it all over with at once.
please feel free to tell me to look up a certain .net concept, but if so please name it precisely. i have not been shy over the last week about looking up .net concepts as best as i can identify their relevance.
1) how am i to provide global vars, consts, types to every or any class in the entire program? let's assume a large program with multiple namespaces. yes, i realize that global vars are somewhat frowned upon, but even if i were to use some other approach, my underlying question is of scope/visibility: how do i get other classes to see, e.g., the Coin type, the someConstant constant, the ticksPerSecond property?
2) how do i access the MyRandom class from elsewhere in the program? sure, rather than learn how to deal with "shared" i can simply instantiate everywhere i need MyRandom, but i'm not wanting to repeatedly instantiate because i need base.init to do its job of providing a seed number to the .netRandom class -- such that any further calls to base.next (directly or indirectly) return the next number in the endless series of random numbers based on that one seed. i.e., i don't want to have more than one seed (i'm afraid that a fast program will result in identical seeds across multiple instances), and want any entity in the program who ever needs a random number to go looking for it from the same source, with the same one original seed.
thanks for any help, and sorry if the way i'm asking reflects fuzzy thinking.
-paul
the "program" below is a do-nothing-useful accumulation of every cobra/.net concept that's been confusing me for the last week as i struggle to learn cobra and .net. please forgive me for throwing so many issues at this forum in one posting, but i figure better to get it all over with at once.
please feel free to tell me to look up a certain .net concept, but if so please name it precisely. i have not been shy over the last week about looking up .net concepts as best as i can identify their relevance.
1) how am i to provide global vars, consts, types to every or any class in the entire program? let's assume a large program with multiple namespaces. yes, i realize that global vars are somewhat frowned upon, but even if i were to use some other approach, my underlying question is of scope/visibility: how do i get other classes to see, e.g., the Coin type, the someConstant constant, the ticksPerSecond property?
2) how do i access the MyRandom class from elsewhere in the program? sure, rather than learn how to deal with "shared" i can simply instantiate everywhere i need MyRandom, but i'm not wanting to repeatedly instantiate because i need base.init to do its job of providing a seed number to the .netRandom class -- such that any further calls to base.next (directly or indirectly) return the next number in the endless series of random numbers based on that one seed. i.e., i don't want to have more than one seed (i'm afraid that a fast program will result in identical seeds across multiple instances), and want any entity in the program who ever needs a random number to go looking for it from the same source, with the same one original seed.
thanks for any help, and sorry if the way i'm asking reflects fuzzy thinking.
-paul
namespace ESutils
class Globalz
enum Coin
heads, tails
const someConstant as uint32 = 37
var _ticksPerSecond as decimal
pro ticksPerSecond as decimal
get
return _ticksPerSecond
set
_ticksPerSecond = value
class SomeMain
def main
a = 3 + Globalz.someConstant
a = a + MyRandom.next(7, 8)
print [a]
class MyRandom inherits Random
cue init is shared
base.init
def next(lowValue as int, highValue as int) as int #fix next from random class,
assert lowValue <= highValue
assert highValue + 1 <= int.maxValue
return base.next(lowValue, highValue + 1)
def coinToss as Globalz.Coin
if .next(0, 1) == 0
return Coin.heads
else
return Coin.tails