[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 253
  • Last Modified:

How too use a assembly in Delphi .Net

I have made a simpel assembly in .Net. But how too use it?

I have included in by adding in in add Reference.

And i have added a buton. In C# the code is:

private void btnMyMorningGreeting_Click(object sender, System.EventArgs e)
{
  MyGreeting.TMyGreeting myGreeting = new MyGreeting.TMyGreeting();
  MessageBox.Show(myGreeting.InTheMorning());
}

but in Delphi i have
procedure TWinForm.btnMyMorningGreeting_Click(sender: System.Object; e: System.EventArgs);
begin
  //WHAT TO WRITE HER?
end
0
liljen_80
Asked:
liljen_80
  • 6
  • 6
1 Solution
 
atul_parmarCommented:
In delphi code, first you will have to create an object of the C# class in which your btnMyMorningGreeting_click resides.

e.g. InDelphi Code
var
  myOb : YourC#Assembly.Classname;
begin
  myObj := YourC#Assembly.Classname.create;
  myObj.btnMyMorningGreeting_Click(sender, e);
  myObj.free;
end;
 
0
 
liljen_80Author Commented:
The C# code was only for showing what I needs too do. The assembly (.dll) is writen in Delphi, and I want too have it all in delphi.
0
 
liljen_80Author Commented:
myOb : YourC#Assembly.Classname;

YourC#Assembly, is that the name of the assembly, witch in my case is AssemblyTest.dll?!?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
atul_parmarCommented:
yep
0
 
liljen_80Author Commented:
The code in my assembly is (The name of the assembly is: MyFirstAssembly.dll):

unit MyGreeting;

interface

type
  TMyGreeting = class
  private
    { Private Declarations }
  public
    constructor Create;
    function InTheMorning(): string;
  end;

implementation

constructor TMyGreeting.Create;
begin
  inherited Create;
  // TODO: Add any constructor code here
end;

function TMyGreeting.InTheMorning: string;
begin
  Result := 'Good morning';
end;
end.

I HAVE NO ERROR WHEN I COMPILE THE PROJECT!


I THEN START A NEW PROJECT, WINDOWS FORMS APPLICATION - DELPHI FOR .NET, AND INSIDE I ADD THE REFERANCE, MyFirstAssembly.dll.
I then add a button, with a click. And starts too write the code, but get the error:

procedure TWinForm.Button1_Click(sender: System.Object; e: System.EventArgs);
var
  myObj: MyFirstAssembly.TMyGreeting;
begin

end;

[Error] TestAssemblyWinForm.pas(95): E2003 Undeclared identifier: 'MyFirstAssembly'

Got any ide what I'm doing wrong?
0
 
atul_parmarCommented:
add MyGreeting to the uses clause
0
 
atul_parmarCommented:
e.g.
uses
MyFirstAssembly.MyGreeting
0
 
liljen_80Author Commented:
With
uses
  MyFirstAssembly.MyGreeting, i got error, but it was non error with
uses
  MyGreeting.

But I still have the error:
[Error] TestAssemblyWinForm.pas(95): E2003 Undeclared identifier: 'MyFirstAssembly'

This is the code for my Windows Forms Application:
unit TestAssemblyWinForm;

interface

uses
  System.Drawing, System.Collections, System.ComponentModel,
  System.Windows.Forms, System.Data, MyGreeting;

type
  TWinForm = class(System.Windows.Forms.Form)
  {$REGION 'Designer Managed Code'}
  strict private
    /// <summary>
    /// Required designer variable.
    /// </summary>
    Components: System.ComponentModel.Container;
    Button1: System.Windows.Forms.Button;
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    procedure InitializeComponent;
    procedure Button1_Click(sender: System.Object; e: System.EventArgs);
  {$ENDREGION}
  strict protected
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    procedure Dispose(Disposing: Boolean); override;
  private
    { Private Declarations }
  public
    constructor Create;
  end;

  [assembly: RuntimeRequiredAttribute(TypeOf(TWinForm))]

implementation

{$AUTOBOX ON}

{$REGION 'Windows Form Designer generated code'}
/// <summary>
/// Required method for Designer support -- do not modify
/// the contents of this method with the code editor.
/// </summary>
procedure TWinForm.InitializeComponent;
begin
  Self.Button1 := System.Windows.Forms.Button.Create;
  Self.SuspendLayout;
  //
  // Button1
  //
  Self.Button1.Location := System.Drawing.Point.Create(72, 112);
  Self.Button1.Name := 'Button1';
  Self.Button1.TabIndex := 0;
  Self.Button1.Text := 'Button1';
  Include(Self.Button1.Click, Self.Button1_Click);
  //
  // TWinForm
  //
  Self.AutoScaleBaseSize := System.Drawing.Size.Create(5, 13);
  Self.ClientSize := System.Drawing.Size.Create(292, 266);
  Self.Controls.Add(Self.Button1);
  Self.Name := 'TWinForm';
  Self.Text := 'WinForm';
  Self.ResumeLayout(False);
end;
{$ENDREGION}

procedure TWinForm.Dispose(Disposing: Boolean);
begin
  if Disposing then
  begin
    if Components <> nil then
      Components.Dispose();
  end;
  inherited Dispose(Disposing);
end;

constructor TWinForm.Create;
begin
  inherited Create;
  //
  // Required for Windows Form Designer support
  //
  InitializeComponent;
  //
  // TODO: Add any constructor code after InitializeComponent call
  //
end;

procedure TWinForm.Button1_Click(sender: System.Object; e: System.EventArgs);
var
  myObj: MyFirstAssembly.TMyGreeting;
begin

end;

end.

0
 
atul_parmarCommented:
Try

procedure TWinForm.Button1_Click(sender: System.Object; e: System.EventArgs);
var
  myObj: TMyGreeting;
begin
 
end;
0
 
liljen_80Author Commented:
It seems too work, but I want too test it by using ShowMessage. But I only get errors, why? Is it because I uses WINDOWS FORMS APPLICATION - DELPHI FOR .NET?
0
 
atul_parmarCommented:
procedure TWinForm.Button1_Click(sender: System.Object; e: System.EventArgs);
var
  myObj: TMyGreeting;
begin
  myObj :=   TMyGreetin.create;
  MessageBox.Show(myObj.InTheMorning());
end;

0
 
liljen_80Author Commented:
Thak you!!
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now