Hi
as u all probably notice Im a noob with Delphi
Is it possible to code a case like I did below If it is please correct my faults I made so that it would run
redSpan is a rich edit
edt1s is a edit
chelsea and arsenal are textfiles that I already declared
and ArsenalS and ChelseaS are the lines within the textfiles
I can also e-mail the program form to a willing helper ..
case (edt1s.text) of
'Chelsea' : Reset(chelsea);
while not Eof(chelsea) do
begin
Readln(chelsea, chelseaS);
redSpan1.Lines.Add(chelseaS);
end;
'Arsenal' : reset (arsenal);
while not Eof(arsenal) do
begin
Readln(arsenal, arsenalS);
redSpan1.Lines.Add(ArsenalS);
end;No, you cannot use strings in a case. It accepts only ordinal types (booleans, sets, integers). There are work-arounds for this. One tutorial can be found here .
No, you cannot use strings in a case. It accepts only ordinal types (booleans, sets, integers). There are work-arounds for this. One tutorial can be found here .
....Thank you for the reply, I am actually very deperate for help as I am trying to finish a very simple program . Its just that I dont understand the delphi way of coding properly, I have set up the form already I just need coding .Like I say its very simple. Do u think it is possible for me to email you this form so that u can maybe help me with the programming,U will notice once u have the form that it actually is very simple
regards
You can post code in zipped file, once you push th Go Advanced or reply by Use Advanced Editor, attached to your post, email communication is not according to rules, as it excludes others from discussion.
You can post code in zipped file, once you push th Go Advanced or reply by Use Advanced Editor, attached to your post, email communication is not according to rules, as it excludes others from discussion.
I understand but what I want to do is I want to e-mail or upload the whole program folder.
Please I am very desperate for help. What must I do
You can do something like this:
var
i: Integer;
begin
i := 0;
if Lowercase(edt1s.Text) = 'chelsea' then
i := 1
else if Lowercase(edt1s.Text) = 'arsenal' then
i := 2;
case i of
1: begin
Reset(chelsea);
while not Eof(chelsea) do
begin
Readln(chelsea, chelseaS);
redSpan1.Lines.Add(chelseaS);
end;
end;
2: begin
Reset(arsenal);
while not Eof(arsenal) do
begin
Readln(arsenal, arsenalS);
redSpan1.Lines.Add(ArsenalS);
end;
end;
end;
end;A case statement only works on cardinal values (numbers), but you can perform a simple lookup to convert strings into an index into the list. Here's a little unit I wrote some years ago to do just that.
unit uCaseStringOf;
{
Use strings in case statements
Copyright (c) 2006 Michael Thomas Greer
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
----------------------------------------------------------------------------
E X A M P L E
----------------------------------------------------------------------------
s := 'fooey';
case caseString(
s,
['grumble', 'ack', 'fooey', 'woah']
) of
0: writeln( 'nope' ); //matches 'grumble'
1: writeln( 'nope' ); //matches 'ack'
2: writeln( 'yep' ); //matches 'fooey'
3: writeln( 'nope' ) //matches 'woah'
else writeln( 'nope' ) //no match
end;
CaseString() returns -1 if s is not matched (illustrated above with the
'else' clause).
You can control how caseString() looks for s in the string list using one
or both of the optional parameters.
The function is overloaded so to make it unimportant which order you
specify optional arguments.
Optional argument values are:
ctCaseSensitive (Default) Character case matters.
ctCaseInSensitive Character case is ignored.
ctExact (Default) The string must match exactly in length.
ctBegins The string need only match the beginning.
ctEnds The string need only match the end.
ctSubString The string need only match a substring.
Examples:
caseString(
'Hello',
['greetings', 'why, hello', 'hello there!', '_HELLO_', 'hello'],
ctCaseInSensitive,
cfn
)
if cfn == ctExact --> no match (since we're using case sensitive test)
if cfn == ctBegins --> 2 ('hello there!')
if cfn == ctEnds --> 1 ('why, hello')
if cfn == ctSubString --> 1 Yes, it would also match '_HELLO_'. But then
it would match any of the strings except the
first...
}
//////////////////////////////////////////////////////////////////////////////
interface
//////////////////////////////////////////////////////////////////////////////
uses
SysUtils;
type
tCaseStringCase = (ctCaseSensitive, ctCaseInSensitive);
tCaseStringCompare = (ctExact, ctBegins, ctEnds, ctSubString);
function caseString(
s: string;
ls: array of string;
sfn: tCaseStringCompare = ctExact;
cfn: tCaseStringCase = ctCaseSensitive
): integer; overload;
function caseString(
s: string;
ls: array of string;
cfn: tCaseStringCase;
sfn: tCaseStringCompare = ctExact
): integer; overload;
//////////////////////////////////////////////////////////////////////////////
implementation
//////////////////////////////////////////////////////////////////////////////
//----------------------------------------------------------------------------
// caseString
//----------------------------------------------------------------------------
// This is the function that does all the work
function caseString(
s: string;
ls: array of string;
sfn: tCaseStringCompare = ctExact;
cfn: tCaseStringCase = ctCaseSensitive
): integer;
var i: integer;
begin
if cfn = ctCaseInSensitive then begin
s := lowercase( s );
for i := 0 to high( ls ) do ls[ i ] := lowercase( ls[ i ] )
end;
result := -1;
for i := 0 to high( ls ) do begin
case sfn of
ctExact: if s = ls[ i ] then result := i;
ctBegins: if pos( s, ls[ i ] ) = 1 then result := i;
ctEnds: if s = copy( ls[ i ], length( ls[ i ] ) -length( s ), length( s ) )
then result := i;
ctSubstring: if pos( s, ls[ i ] ) > 0 then result := i
end;
if result >= 0 then break
end
end;
//----------------------------------------------------------------------------
// caseString
//----------------------------------------------------------------------------
function caseString(
s: string;
ls: array of string;
cfn: tCaseStringCase;
sfn: tCaseStringCompare = ctExact
): integer;
begin
result := caseString( s, ls, sfn, cfn )
end;
end. Hope this helps.