Hi there,
Just a couple of questions:
1) How do I declare public members? The only way I found to access directly class variables is by creating properties...
2) In c#, if I declare a method as static, then I would simply use it by prepending the name of the class ( classname.method() ).
However, in Cobra I cannot use this method if I don't instantiate the class first. Why?
neuruss
Forums
public, static & friends
26 posts
• Page 1 of 3 • 1, 2, 3
Re: public, static & friends
newruss,
Are you looking for the "as shared" modifier (not sure what it's called)?
Are you looking for the "as shared" modifier (not sure what it's called)?
- Code: Select all
$ cat hello.cobra
class Hello
def hi is shared
print 'hello, world.'
class Program
def main is shared
Hello.hi
$ cobra -c hello.cobra
Compilation succeeded
$ mono hello.exe
hello, world.
- RichS
- Posts: 8
Re: public, static & friends
Rich,
You are right! Actually I asked the wrong question..
I was having problem accessing a static member, not a method.
For example, I have a class "Dude" (see the code below) were I defined a shared variable "lista", which is a list of type Dude.
Everytime an instance of Person is created, it adds itself to lista.
In c# I would simply access it this way: Person.lista , but in Cobra, the compiler indicates that I should create an instance in order to access it.
Trying to access it directly as above throws the following error:
hello2.cobra(7): error: Cannot access instance member "lista" on type "Dude". Ma
ke the member "shared" or create an instance of the type.
Compilation failed - 1 error
Not running due to errors above.
You are right! Actually I asked the wrong question..
I was having problem accessing a static member, not a method.
For example, I have a class "Dude" (see the code below) were I defined a shared variable "lista", which is a list of type Dude.
Everytime an instance of Person is created, it adds itself to lista.
In c# I would simply access it this way: Person.lista , but in Cobra, the compiler indicates that I should create an instance in order to access it.
Trying to access it directly as above throws the following error:
hello2.cobra(7): error: Cannot access instance member "lista" on type "Dude". Ma
ke the member "shared" or create an instance of the type.
Compilation failed - 1 error
Not running due to errors above.
class Dude
shared
var _lista = List<of Dude>()
var _name as String
var _last as String
var _age as int
def init(n as String, l as String, a as int)
_name = n.toUpper
_last = l.toUpper
_age = a
_lista.add(this)
pro lista from var
def toString as String is override
return '[_name] [_last]'
- neuruss
- Posts: 23
Re: public, static & friends
neuruss,
Is it worked for me a bad answer?
Is it worked for me a bad answer?
- Code: Select all
$ cat dude.cobra
class Dude
shared
var _lista = List<of Dude>()
var _name as String
var _last as String
var _age as int
def init(n as String, l as String, a as int)
_name = n.toUpper
_last = l.toUpper
_age = a
_lista.add(this)
pro lista from var
def toString as String is override
return '[_name] [_last]'
class Program
def main is shared
Dude('a','b',1)
Dude('c','d',2)
dudes=Dude('e','f',3)
for d in dudes.lista
print d
$ cobra -c dude.cobra
Compilation succeeded
$ mono dude.exe
A B
C D
E F
- RichS
- Posts: 8
Re: public, static & friends
It worked, but you actually created an instance of Dude (dudes).
What I mean is why can't I just access "lista" by simply prepending the classname, instead of having to instantiate the class.
For example:
Do you know what I mean?
What I mean is why can't I just access "lista" by simply prepending the classname, instead of having to instantiate the class.
For example:
for d in Dude.lista
print d
Do you know what I mean?
- neuruss
- Posts: 23
Re: public, static & friends
I do now.
I get the same error if I don't make an instance.
I'm too much of a programming rookie, it wouldn't occur to me not to use an instance.
I come from a mostly Python background.
I get the same error if I don't make an instance.
I'm too much of a programming rookie, it wouldn't occur to me not to use an instance.
I come from a mostly Python background.
- RichS
- Posts: 8
Re: public, static & friends
Well, I come from Python too, which is the only language I know fairly well (as a simple hobbyist).
I learned a little bit of c# while trying to understand Boo and static typing ...
I learned a little bit of c# while trying to understand Boo and static typing ...
- neuruss
- Posts: 23
Re: public, static & friends
Glad you guys worked this out while I was sleeping.
By the way, in your posts, if you use (cobra) ... (/cobra) tags instead of (code) ... (/code) then someday we'll have syntax highlighting. Of course, BBCode uses square brackets, not parens, which I only use for explanation so you can see the tags.
By the way, in your posts, if you use (cobra) ... (/cobra) tags instead of (code) ... (/code) then someday we'll have syntax highlighting. Of course, BBCode uses square brackets, not parens, which I only use for explanation so you can see the tags.
- Charles
- Posts: 2515
- Location: Los Angeles, CA
Re: public, static & friends
How is it possible?
I keep on getting an error.
If you see above, I had already typed "pro lista from var".
Now I tried placing it right below the shared var declaration, but it doesn't work either...
I keep on getting an error.
If you see above, I had already typed "pro lista from var".
Now I tried placing it right below the shared var declaration, but it doesn't work either...
- neuruss
- Posts: 23
26 posts
• Page 1 of 3 • 1, 2, 3
Who is online
Users browsing this forum: No registered users and 24 guests