digitalmars.D - Initializing variables within classes
- jicman (27/27) Mar 13 2005 I have this class,
- J C Calvarese (34/63) Mar 13 2005 It sounds like you need to use a "constructor". In D, it's looks kind of...
- jicman (4/67) Mar 14 2005 Thanks JCC. That one may work, but these functions are in different file...
- Ben Hinkle (14/20) Mar 13 2005 It looks like the compiler isn't too creative when it comes to figuring ...
- Andrew Fedoniouk (12/44) Mar 13 2005 use constructors for initializing fields by values know in runtime only:
- jicman (4/58) Mar 14 2005 Thank Andrew. Sometimes I think I know English and I keep figuring out ...
I have this class, |class HTTPCall |{ | char[] Method = null; | char[] HOST = null; | int PORT = 80; | char[] REQ = null; | char[] Separator = GetMIMESeparator(); | char[] HTTPbody = null; |} I am getting this error, 19:26:05.11>build FormMgr.d FormMgr.d(89): non-constant expression GetMIMESeparator() |char[] GetMIMESeparator() |{ | char[] sep = | "XXXXXXX---Separator---multi---mime---encoding---XXXXXX"; | return sep; |} There were other things here, but I made it as simple as above and it still fails. I know how I could go around this, but I want to have that value when I call a "new HTTPCall()". Is this possible? I was looking into the classes documentation, but some of those examples are not (according to me) what I am looking for. Oh yeah, I tried "static char[] sep = ...." and that didn't work either. Please help a poor D knowledgable man out. thanks.
Mar 13 2005
jicman wrote:I have this class, |class HTTPCall |{ | char[] Method = null; | char[] HOST = null; | int PORT = 80; | char[] REQ = null; | char[] Separator = GetMIMESeparator(); | char[] HTTPbody = null; |} I am getting this error, 19:26:05.11>build FormMgr.d FormMgr.d(89): non-constant expression GetMIMESeparator() |char[] GetMIMESeparator() |{ | char[] sep = | "XXXXXXX---Separator---multi---mime---encoding---XXXXXX"; | return sep; |} There were other things here, but I made it as simple as above and it still fails. I know how I could go around this, but I want to have that value when I call a "new HTTPCall()". Is this possible? I was looking into the classes documentation, but some of those examples are not (according to me) what I am looking for.It sounds like you need to use a "constructor". In D, it's looks kind of like a function called "this". The constructor is called whenever you "new" an object. See if this code works... class HTTPCall { char[] Method; char[] HOST; int PORT; char[] REQ; char[] Separator; char[] HTTPbody; char[] GetMIMESeparator() { char[] sep = "XXXXXXX---Separator---multi---mime---encoding---XXXXXX"; return sep; } this() { Method = null; HOST = null; PORT = 80; REQ = null; Separator = GetMIMESeparator(); HTTPbody = null; } } There are more details available at http://www.digitalmars.com/d/class.html#constructors -- Justin (a/k/a jcc7) http://jcc_7.tripod.com/d/
Mar 13 2005
Thanks JCC. That one may work, but these functions are in different files, so I just did the same what Andrew suggested and that worked. thanks. J C Calvarese says...jicman wrote:I have this class, |class HTTPCall |{ | char[] Method = null; | char[] HOST = null; | int PORT = 80; | char[] REQ = null; | char[] Separator = GetMIMESeparator(); | char[] HTTPbody = null; |} I am getting this error, 19:26:05.11>build FormMgr.d FormMgr.d(89): non-constant expression GetMIMESeparator() |char[] GetMIMESeparator() |{ | char[] sep = | "XXXXXXX---Separator---multi---mime---encoding---XXXXXX"; | return sep; |} There were other things here, but I made it as simple as above and it still fails. I know how I could go around this, but I want to have that value when I call a "new HTTPCall()". Is this possible? I was looking into the classes documentation, but some of those examples are not (according to me) what I am looking for.It sounds like you need to use a "constructor". In D, it's looks kind of like a function called "this". The constructor is called whenever you "new" an object. See if this code works... class HTTPCall { char[] Method; char[] HOST; int PORT; char[] REQ; char[] Separator; char[] HTTPbody; char[] GetMIMESeparator() { char[] sep = "XXXXXXX---Separator---multi---mime---encoding---XXXXXX"; return sep; } this() { Method = null; HOST = null; PORT = 80; REQ = null; Separator = GetMIMESeparator(); HTTPbody = null; } } There are more details available at http://www.digitalmars.com/d/class.html#constructors -- Justin (a/k/a jcc7) http://jcc_7.tripod.com/d/
Mar 14 2005
|class HTTPCall |{ | char[] Separator = GetMIMESeparator(); |} 19:26:05.11>build FormMgr.d FormMgr.d(89): non-constant expression GetMIMESeparator()It looks like the compiler isn't too creative when it comes to figuring out if something is constant. Will it one day be able to figure out if a return value from a function is constant? I don't know. I hope so because it would be nice. You should be able to write something like |private const char[] sep = | "XXXXXXX---Separator---multi---mime---encoding---XXXXXX"; |class HTTPCall |{ | char[] Separator = sep; |} |char[] GetMIMESeparator() |{ | return sep; |}
Mar 13 2005
use constructors for initializing fields by values know in runtime only: class HTTPCall { char[] Separator; this() // constructor { Separator = GetMIMESeparator(); } } Andrew. "jicman" <jicman_member pathlink.com> wrote in message news:d12n1s$8to$1 digitaldaemon.com...I have this class, |class HTTPCall |{ | char[] Method = null; | char[] HOST = null; | int PORT = 80; | char[] REQ = null; | char[] Separator = GetMIMESeparator(); | char[] HTTPbody = null; |} I am getting this error, 19:26:05.11>build FormMgr.d FormMgr.d(89): non-constant expression GetMIMESeparator() |char[] GetMIMESeparator() |{ | char[] sep = | "XXXXXXX---Separator---multi---mime---encoding---XXXXXX"; | return sep; |} There were other things here, but I made it as simple as above and it still fails. I know how I could go around this, but I want to have that value when I call a "new HTTPCall()". Is this possible? I was looking into the classes documentation, but some of those examples are not (according to me) what I am looking for. Oh yeah, I tried "static char[] sep = ...." and that didn't work either. Please help a poor D knowledgable man out. thanks.
Mar 13 2005
Thank Andrew. Sometimes I think I know English and I keep figuring out I don't. Thanks. jic Andrew Fedoniouk says...use constructors for initializing fields by values know in runtime only: class HTTPCall { char[] Separator; this() // constructor { Separator = GetMIMESeparator(); } } Andrew. "jicman" <jicman_member pathlink.com> wrote in message news:d12n1s$8to$1 digitaldaemon.com...I have this class, |class HTTPCall |{ | char[] Method = null; | char[] HOST = null; | int PORT = 80; | char[] REQ = null; | char[] Separator = GetMIMESeparator(); | char[] HTTPbody = null; |} I am getting this error, 19:26:05.11>build FormMgr.d FormMgr.d(89): non-constant expression GetMIMESeparator() |char[] GetMIMESeparator() |{ | char[] sep = | "XXXXXXX---Separator---multi---mime---encoding---XXXXXX"; | return sep; |} There were other things here, but I made it as simple as above and it still fails. I know how I could go around this, but I want to have that value when I call a "new HTTPCall()". Is this possible? I was looking into the classes documentation, but some of those examples are not (according to me) what I am looking for. Oh yeah, I tried "static char[] sep = ...." and that didn't work either. Please help a poor D knowledgable man out. thanks.
Mar 14 2005