Thread overview
Newbie needs help with getting (what should be a) simple program to compile...
Dec 07, 2005
Tyler K.
Dec 07, 2005
Chris Miller
Dec 07, 2005
James Dunne
Dec 07, 2005
Frank Benoit
Dec 08, 2005
Tyler K.
December 07, 2005
I'm having a problem in this simple program trying to create an object and use it.  This is my first attempt at anything object-oriented (I know that D probably isn't the best place to start out with no OO experience, but it is a very appealing language to me coming from a C backgroung).  Here is the relevant code:

...
try
{
__CFileEx configfile = new __CFileEx("filename", "w+")
}
catch(OpenException oe)
{
/*Handle exception*/
}
configfile.readLine(configlinetemp);
...

__CFileEx is an extention to the CFile class I wrote that handles opening the file in addition to being a stream wrapper for C files (and a few other functions specific to my program).  The problem I have is that when I try to use the configfile object, the compiler (DMD 0.140 for Linux) complains that about undefined identifier configfile and that type int doesn't have property readLine.  What am I doing wrong?


December 07, 2005
On Tue, 06 Dec 2005 23:32:49 -0500, Tyler K. <Tyler_member@pathlink.com> wrote:

> try
> {
> __CFileEx configfile = new __CFileEx("filename", "w+")
> }
> catch(OpenException oe)
> {
> /*Handle exception*/
> }
> configfile.readLine(configlinetemp);
> ...
>
> __CFileEx is an extention to the CFile class I wrote that handles opening the
> file in addition to being a stream wrapper for C files (and a few other
> functions specific to my program).  The problem I have is that when I try to use
> the configfile object, the compiler (DMD 0.140 for Linux) complains that about
> undefined identifier configfile and that type int doesn't have property
> readLine.  What am I doing wrong?
>

configfile only exists in your try block. Declare it where enough scopes have access:

__CFileEx configfile;
try
{
configfile = new __CFileEx("filename", "w+")
}
catch(OpenException oe)
{
/*Handle exception*/
}
// Note: if __CFileEx constructor fails (throws), configfile will remain null.
// You should reconsider the location of the next line.
configfile.readLine(configlinetemp);
December 07, 2005
Tyler K. wrote:
> I'm having a problem in this simple program trying to create an object and use
> it.  This is my first attempt at anything object-oriented (I know that D
> probably isn't the best place to start out with no OO experience, but it is a
> very appealing language to me coming from a C backgroung).  Here is the relevant
> code:
> 
> ...
> try
> {
> __CFileEx configfile = new __CFileEx("filename", "w+")
> }
> catch(OpenException oe)
> {
> /*Handle exception*/
> }
> configfile.readLine(configlinetemp);
> ...
> 
> __CFileEx is an extention to the CFile class I wrote that handles opening the
> file in addition to being a stream wrapper for C files (and a few other
> functions specific to my program).  The problem I have is that when I try to use
> the configfile object, the compiler (DMD 0.140 for Linux) complains that about
> undefined identifier configfile and that type int doesn't have property
> readLine.  What am I doing wrong?
> 
> 

First and foremost, welcome to D!

Second and most importantly - your answer to your question:

It's a problem of scope, nothing new from C.  You've defined your __CFileEx configfile in the try { } block's scope.  You can't access that variable outside the try block.

Simple solution.  Change your code to this:

__CFileEx configfile;
try
{
 configfile = new __CFileEx("filename", "w+")
}
catch(OpenException oe)
{
 /*Handle exception*/
}
configfile.readLine(configlinetemp);


Also, I'd recommend not using double-underscore to prefix your class names as
  1) they are reserved by the D language specification for implementation purposes. (slightly surprised the compiler doesn't yell at you for this)
  2) they're just plain fugly.


Enjoy delving into the world of D.  Lot's of experienced people can surely help you out here!
December 07, 2005
try
{
        __CFileEx configfile = new __CFileEx("filename", "w+")
        char[] configlinetemp;
        configfile.readLine(configlinetemp);
}
catch(OpenException oe)
{
        /*Handle exception*/
}


As the others said, configfile is local to the scope it is defined in. If
you place the use of configfile behind the catch, you have to check if
configfile is not null. So it is better to make all the work with
configfile inside the try block. If OpenException is thrown, you probably
don't want to read from it.
.
December 08, 2005
Thanks everyone!  I declared it outside of the try block and it works now.