Hack Forums
[Delphi] Globally Assign a Record Struct? - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Coders Lounge (https://hackforums.net/forumdisplay.php?fid=5)
+---- Thread: [Delphi] Globally Assign a Record Struct? (/showthread.php?tid=5307437)



Globally Assign a Record Struct? - ReversɘЯ - Jun 16, 2016

Hey Guys, was just wondering if I could somewhere globally assign a Records Struct I have... you see I don't wanna assign it anywhere in a local scope but in the global scope so it can be used anywhere:

Code
type
   Color = record
     Red : integer;
     White : integer;
     Lime : integer;
     Yellow : integer;
   end;

<...>

  With Color do
  begin
     Color.Red := 1;
     Color.White := 2;
     Color.Lime := 3;
     Color.Yellow := 4;
  end;

Any Ideas? I tried assigning itself but it won't let me..


* - Volty - Jun 16, 2016

Globally? It already is global. Even if you put record "color" into another unit, you can still use it. Please be a little more specific.
My Code:
Code
program prjTest;
{$APPTYPE CONSOLE}
uses
  UOther in 'UOther.pas';
var
Test : Color;
begin

With test do
  begin
     Red := 1;
     White := 2;
     Lime := 3;
     Yellow := 4;
  end;
end.
Code of UOther:
Code
unit UOther;
interface
type
   Color = record
     Red : integer;
     White : integer;
     Lime : integer;
     Yellow : integer;
   end;
implementation
end.



* - ReversɘЯ - Jun 17, 2016

(Jun 16, 2016 06:28 PM)Volty Wrote:  Globally? It already is global. Even if you put record "color" into another unit, you can still use it. Please be a little more specific.
My Code:
Code
program prjTest;
{$APPTYPE CONSOLE}
uses
  UOther in 'UOther.pas';
var
Test : Color;
begin

With test do
  begin
     Red := 1;
     White := 2;
     Lime := 3;
     Yellow := 4;
  end;
end.
Code of UOther:
Code
unit UOther;
interface
type
   Color = record
     Red : integer;
     White : integer;
     Lime : integer;
     Yellow : integer;
   end;
implementation
end.

ok alright it's global, but I don't wanna use it in another unit, I wanna use it locally and within my app.. how can I call Color.Red and be read without giving me the error: "Expression has to be constant" or idk... ?


* - Volty - Jun 17, 2016

(Jun 17, 2016 10:38 AM)ReversɘЯ Wrote:  ok alright it's global, but I don't wanna use it in another unit, I wanna use it locally and within my app.. how can I call Color.Red and be read without giving me the error: "Expression has to be constant" or idk... ?

Maybe this? (No errors for me)
Code
program prjTest;
{$APPTYPE CONSOLE}
uses Dialogs,SysUtils;

type
   Color = record
     Red : integer;
     White : integer;
   end;

var
Test : Color;
begin
Test.Red := 10;
ShowMessage(IntToStr(Test.Red));
end.



* - ReversɘЯ - Jun 17, 2016

(Jun 17, 2016 12:42 PM)Volty Wrote:  Maybe this? (No errors for me)
Code
program prjTest;
{$APPTYPE CONSOLE}
uses Dialogs,SysUtils;

type
   Color = record
     Red : integer;
     White : integer;
   end;

var
Test : Color;
begin
Test.Red := 10;
ShowMessage(IntToStr(Test.Red));
end.

well it's working, i just wanted to use it in a function where I'm using a Case scenario:

Code
function yadayada(colorid: integer): string;
Case colorID do
begin
      Colors.Red:
                  begin
                          <...>
                  end;

      Colors.White:
                  begin
                          <...>
                  end;
end;
end;

it gives me an error saying Expression must be constant. or something like that... I just use it like this anyways...

Code
function yadayada(colorid: integer): string;
Case colorID do
begin
      1:
                  begin
                          <...>
                  end;

      2:
                  begin
                          <...>
                  end;
end;
end;

Thx for helping out though