Diese Seite mit anderen teilen ...

Informationen zum Thema:
Forum:
WinDev Forum
Beiträge im Thema:
8
Erster Beitrag:
vor 6 Jahren, 2 Monaten
Letzter Beitrag:
vor 6 Jahren, 2 Monaten
Beteiligte Autoren:
Pragma Tix, David Egan, Ruben Fdez..pcs.crosspost, Peter Holemans, Stefan Bentvelsen

[WD17] Using constants in a class

Startbeitrag von David Egan am 13.06.2012 20:40

Hi
I have an 'application' class which is called at the opening of the project. This class basically holds all my global variables & constants which are all declared as public.

When I try to access one of the constants in my code as 'application:Capital' I get an error which tells me I need to use 'application::Capital' to access the constant. If I change the code to that, as soon as I exit the code line or Windev does a refresh it deletes the first letter of the constant name and tells me that 'application::apital' doesn't exist!

I haven't used constants in a class like this before so I'm not sure if I've misunderstood something or not. Has anyone else experienced this, is it not possible to do it?
Thanks

David

Antworten:

Hi David,

can you tell us where you declare your constants in your class ?

We also use constants in classes. We declare them in the code 'Declaration of class', before '[yourClass] is class'. When you use this constants in your program, outside your class, you have to use [YourClass]:YourConstant (or [YourClass]::YourConstant) instead of [YourInstanceOfClass]:YourConstant.

And after changing a class, I always recompile the whole project. It could help you avoid problems like you mentioned.

von Stefan Bentvelsen - am 14.06.2012 09:08
You can trie this. Works 4 me (TM)


cApplication is class
GLOBAL

CONSTANT
APPNAME = "MyApp"
CAPITAL = "A"
END


END

PROCEDURE PRIVATE Constructor()
// Make ctor private .. disallow class instances

PROCEDURE GLOBAL GetCapital()
RESULT CAPITAL



and use it as follows :


Trace (cApplication.APPNAME)
Trace(cApplication.GetCapital())


This works 'cause Constants and Procedures are GLOBAL (static)
HTH Bjoern

von Pragma Tix - am 14.06.2012 09:36
Hi David,

Constants are like globals. These are shared between all instances. You thus need to access them via the class definition itself.

E.g.
MyClass is Class
PUBLIC
CONSTANT
MyConst = 1
END
PROTECTED
PRIVATE


Now to access the constant from anywhere in your code. You don't even need an instance.

MyClassInstance is a MyClass
//Get constant from class definition
x is int = MyClass::MyConst (or MyClass..MyConst)

As you see we refer to the class definition itself and not the instance.
The same goes for globaly declared members as these are shared between all instances...

Cheers,

Peter H.

von Peter Holemans - am 14.06.2012 11:23
Quote
Peter H
MyClassInstance is a MyClass
//Get constant from class definition
x is int = MyClass::MyConst (or MyClass..MyConst)


No need to create a class instance at all/


x is int = MyClass.:MyConst


is all you have to do.
I am not very happy with your design. Consider using variables.

AppSettings is class
GLOBAL
CONSTANT
APPNAME ="MyFunkyApp "
END

// of course you want READ ONLY access
GLOBAL PRIVATE
SerialNumber is string = "xxxx-5454-54545-54545"
END

// Nobody wants several instances of an AppSettings class, so disable the constructor
// Take this design as Singleton
PROCEDURE PRIVATE Constructor()

PROCEDURE GLOBAL GetSerialNumber()
RESULT SerialNumber



hope you agree :)

von Pragma Tix - am 14.06.2012 13:09
Another optin is declare constants in project code. "PROJECT -> PROJECT CODE -> INITIALIZATION OF ... ".
IF you do that these constants, variables, connections... are useful during all the execution.
Hope this help you.

Message forwarded from pcsoft.us.windev

von Ruben Fdez..pcs.crosspost - am 14.06.2012 14:02
Quote
Ruben Fdez..pcs.crosspost
Another optin is declare constants in project code. "PROJECT -> PROJECT CODE -> INITIALIZATION OF ... ".
IF you do that these constants, variables, connections... are useful during all the execution.
Hope this help you.

Message forwarded from pcsoft.us.windev


Sure you can do that. But you miss the point. Global variables/constants are insane. The whole effort is done 'cause 1) want to encapsulate,these informations .2) Save and Restore Settings to/from disk. Serialise()/Deserialize() 3) Operate on theses Setting (Generate keys, logs, whatever) within a single class.

But well, you can do it old school.. it is more a matter of taste, i guess.

von Pragma Tix - am 14.06.2012 14:54
That's it, thanks guys.:cheers:

I was indeed instanciating the class & then using the instance, rather than the class.

I agree with Bjorn's last comment. I have previously declared them in project initialization but thought I would get smart this time. I just hadn't realised I don't really need an instance and/or that I should refer to the base class.

Dave

von David Egan - am 14.06.2012 19:34
Zur Information:
MySnip.de hat keinen Einfluss auf die Inhalte der Beiträge. Bitte kontaktieren Sie den Administrator des Forums bei Problemen oder Löschforderungen über die Kontaktseite.
Falls die Kontaktaufnahme mit dem Administrator des Forums fehlschlägt, kontaktieren Sie uns bitte über die in unserem Impressum angegebenen Daten.