🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #7457

Re: Passing a Variable to CopyFile

#7457
From: Andrue Cope [TeamB]
Date: Mon, 31 Jan 2005 17:03
91 lines
3633 bytes
Pat wrote:

> The error message I get is  "Cannot convert 'AnsiString' to 'char *'"
> or something to that effect depending on whether I try AnsiSting
> drive or char drive.

That error occurs because you are passing an AnsiString as an argument
where a 'char *' is expected eg;

  void charStar( char * Widdle )
  {
     // do something
  }

  void wobble()
  {
    AnsiString fred="Hello";

    charStar( fred ); // << Error here
  }

This is a fundamental problem. You can't pass one thing to a function
when it is expecting something different(*). Luckily AnsiString is a
sensibly designed string handling class and it has a method called
"c_str()" that returns a pointer to the array of 'char's that the
AnsiString is holding.

  wibble( fred.c_str() ); // Would compile in the above code.

Specific solution for you:

  AnsiString drive=AnsiString( DriveCombo->Drive )+":\\";

  char * To=drive.c_str();

(*)This might confuse you further and is a lengthy read :)

The reverse is allowed. You can do:

  void wibble( AnsiString wibble );

  wibble( "Hello" );

Although this looks like it's passing a 'char *' to a function that
expects an AnsiString it isn't really. What happens is that the
compiler looks at AnsiString and sees that it has a constructor that
takes a 'char *'. This means it can generate code to create a temporary
AnsiString to save you the bother. In effect it produces:

  {
    AnsiString tmp="Hello";
    wibble( tmp );
  }

Actually this could be more properly written as:

  wibble( AnsiString( "Hello" ) );

This only works because /someone/ took the time to create that
constructor for the class. There is no practical way for anyone to do
the same for 'char *' and that's why you need to use the .c_str()
method.

Going back to:

  AnsiString drive=AnsiString( DriveCombo->Drive )+":\\";

Why the second AnsiString?

You can assign the ::Drive member on its own because AnsiString knows
how to assign a character to itself. I had to put the second AnsiString
in because otherwise the right hand side becomes:

  <char> + <char *>

And that makes no sense. The second AnsiString converts the right hand
side into:

  <AnsiString>+<char *>

..which is acceptable because AnsiString also knows how to append a
'char *' to itself. The result is another AnsiString which means you
end up assigning an AnsiString to an AnsiString.

Finally it might be worth pointing out that as string handling classes
go AnsiString is unusually flexible in this respect. Personally I love
that fact but it can cause problems and purists generally dislike it :)
-- 
Andrue Cope [TeamB]
[Bicester, Uk]
http://info.borland.com/newsgroups/guide.html
Technical Details
Message-ID: <41fe650d@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!213.200.89.82.MISMATCH!tiscali!newsfeed1.ip.tiscali.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <41fe59c2$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,c07811462fe8d980
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Andrue Cope [TeamB]" <no.spam@not.a.valid.address>
Subject: Re: Passing a Variable to CopyFile
Newsgroups: borland.public.cppbuilder.nativeapi
References: <41fe59c2$1@newsgroups.borland.com>
Date: Mon, 31 Jan 2005 17:03:23 +0000
User-Agent: XanaNews/1.17.1.2
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
NNTP-Posting-Host: 217.207.223.34
Message-ID: <41fe650d@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1107191053 217.207.223.34 (31 Jan 2005 09:04:13 -0800)
Lines: 91
X-Authenticated-User: acope
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!213.200.89.82.MISMATCH!tiscali!newsfeed1.ip.tiscali.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail