How do I add methods and properties?

I

Ikke

Hi everybody,

Using Microsoft Visual C# 2005 Express Edition, I'm trying to create a
class library (.dll) to include in a Delphi project.

Since I'm new to building .dlls, I've searched the internet and am now
following an example I've found at
http://www.dotnetheaven.com/Uploadfile/mahesh/pr1202172006014101AM/pr12.a
spx , which explains how to build a .dll, and how to call it. So far so
good.

But when I come to the part of adding methods and properties, I'm asked
to right-click on the class-name and select 'Add->Add Method' (or similar
for properties), which is not available in the Express Edition! I've
checked the internet again, and this is indeed not available in the EE.

However, the Express Edition is the only edition I have, so I would
really like to know how I can add these methods and properties myself,
which I assume *is* possible (otherwise it would be rather daft to
include the ability to create class libraries, imho).

Would anybody please care to explain to me how I do this? Or point me in
the right direction to a nice tutorial?

Thanks in advance!

Ikke
 
B

Barry Kelly

Ikke said:
Using Microsoft Visual C# 2005 Express Edition, I'm trying to create a
class library (.dll) to include in a Delphi project.

What is your point of integration with Delphi? Using Delphi for .NET?
(I work for CodeGear (now part of Embarcadero), on the Delphi compiler.)

In any case, programming is mostly a text-based discipline. I wouldn't
waste time with GUI/menus/etc. for writing new code. Here's a simple C#
class library:

---8<---
using System;
public class MyClass
{
string _myProperty;

public void MyMethod()
{
// a method
Console.WriteLine("Current MyProperty: {0}", MyProperty);
}

public string MyProperty
{
get { return _myProperty; }
set { _myProperty = value; }
}
}
--->8---

Save it as 'Lib.cs' and compile with 'csc /t:library Lib.cs', and you'll
end up with a Lib.dll, a fresh new C# class library.

Alternatively, replace the body of the main source file in your VS
Express project with the above text, and compile from VS, and you'll get
a similar result, albeit with a different name (based on your project
name).

Other details depend on your scenario.

-- Barry
 
T

Todd Carnes

Peter said:
[...]
However, the Express Edition is the only edition I have, so I would
really like to know how I can add these methods and properties myself,
which I assume *is* possible (otherwise it would be rather daft to
include the ability to create class libraries, imho).

Would anybody please care to explain to me how I do this? Or point me in
the right direction to a nice tutorial?

You can just type them in manually. Open the appropriate .cs file where
the class is defined and add them yourself.

In fact, I'm amazed anyone would even use the GUI to add a method or a
property from scratch (as opposed to those required to implement an
interface, override an existing method, etc. where the IDE has enough
information to save some real typing). Such a feature adds so little
value that I didn't even realize it existed in VS. :)

Pete

I add methods, properties, etc. via the class diagram (which is also not
available in the express edition) and I love it....but then I'm only a
beginner and appreciate the GUI making sure I get it right the first time.

To us "noobs" it adds a LOT of value, IMHO. :)

Todd
 
T

Todd Carnes

Ikke said:
Hi everybody,

Using Microsoft Visual C# 2005 Express Edition, I'm trying to create a
class library (.dll) to include in a Delphi project.

Since I'm new to building .dlls, I've searched the internet and am now
following an example I've found at
http://www.dotnetheaven.com/Uploadfile/mahesh/pr1202172006014101AM/pr12.a
spx , which explains how to build a .dll, and how to call it. So far so
good.

But when I come to the part of adding methods and properties, I'm asked
to right-click on the class-name and select 'Add->Add Method' (or similar
for properties), which is not available in the Express Edition! I've
checked the internet again, and this is indeed not available in the EE.

However, the Express Edition is the only edition I have, so I would
really like to know how I can add these methods and properties myself,
which I assume *is* possible (otherwise it would be rather daft to
include the ability to create class libraries, imho).

Would anybody please care to explain to me how I do this? Or point me in
the right direction to a nice tutorial?

Thanks in advance!

Ikke

You're going to have to type them in by hand, which isn't really all
that hard. :)

Todd
 
I

Ikke

You can just type them in manually. Open the appropriate .cs file
where the class is defined and add them yourself.

In fact, I'm amazed anyone would even use the GUI to add a method or a
property from scratch (as opposed to those required to implement an
interface, override an existing method, etc. where the IDE has enough
information to save some real typing). Such a feature adds so little
value that I didn't even realize it existed in VS. :)

Thanks for the information, Peter. Since Barry and Todd basically gave me
the same advice, I've gone ahead and tried it. But it doesn't work.

I know I can type in the methods and properties myself, without using a
GUI (and yes, I prefer it that way), but surely there must be some extra
stuff that I'm missing here? If you just add a method, it doesn't mean it
is exposed in the .dll as well, or does it?

Anyway, here's my small example:

--- C# code ---
using System;
using System.Collections.Generic;
using System.Text;


namespace development
{
public class dev
{
public int Add(int val1, int val2)
{
return val1 + val2;
}
}
}
--- /C# code ---

As you can see, a simple example (taken from one of the many pages I've
found) with a simple function, adding two integers.

Now, here's the Delphi code to call the .dll - first I've defined a
function:
function Add(val1, val2 : Integer) : Integer; stdcall; far; external
'development.dll';
And I use said function as Add(1, 2); somewhere in my code.

Everything compiles fine: C# compiles and builds me a .dll, and my Delphi
project builds as well. But as soon as I try to run it, I get an access
violation.

I assume I need to 'expose' the methods I'm using in C# in my .dll
somehow, but I don't know how...

Is it even possible to call a .Net .dll like this?

Thanks for any info!

Ikke
 
I

Ikke

What is your point of integration with Delphi? Using Delphi for .NET?
(I work for CodeGear (now part of Embarcadero), on the Delphi
compiler.)
<snip>

We would like to write a small application, involving a 3D renderer. One
of us is developing the renderer in C#, mainly because of the
possibilities with the new XNA platform. However, the rest of us are
more familiar with Delphi (not Delphi .Net), so we're trying to come up
with some sort of interface.

Basically, the main project will be a Delphi project, which calls one of
two .dll's: - one .dll written in Delphi, which is a 'fake' renderer to
allow us to test our code - one .dll written in C#, which is the real
renderer.

It looked really nice on paper :)

Thanks,

Ikke
 
T

Todd Carnes

Ikke said:
Thanks for the information, Peter. Since Barry and Todd basically gave me
the same advice, I've gone ahead and tried it. But it doesn't work.

I know I can type in the methods and properties myself, without using a
GUI (and yes, I prefer it that way), but surely there must be some extra
stuff that I'm missing here? If you just add a method, it doesn't mean it
is exposed in the .dll as well, or does it?

Anyway, here's my small example:

--- C# code ---
using System;
using System.Collections.Generic;
using System.Text;


namespace development
{
public class dev
{
public int Add(int val1, int val2)
{
return val1 + val2;
}
}
}
--- /C# code ---

As you can see, a simple example (taken from one of the many pages I've
found) with a simple function, adding two integers.

Now, here's the Delphi code to call the .dll - first I've defined a
function:
function Add(val1, val2 : Integer) : Integer; stdcall; far; external
'development.dll';
And I use said function as Add(1, 2); somewhere in my code.

Everything compiles fine: C# compiles and builds me a .dll, and my Delphi
project builds as well. But as soon as I try to run it, I get an access
violation.

I assume I need to 'expose' the methods I'm using in C# in my .dll
somehow, but I don't know how...

Is it even possible to call a .Net .dll like this?

Thanks for any info!

Ikke

In .Net, after you make the dll, you have to go into the program that
will be using it and add a reference to the dll. You also have to add a
using statement to the new program so it will have access to the dll's
namespace. In your example it would be "using development".

Another thing that someone else here will have to confirm or deny,
because I can't remember off the top of my head, but I think all the
methods in the dll have to be declared as static.

I'm not sure what the equivalent would be in Delphi. I've never messed
with Delphi.

Todd
 
B

Barry Kelly

Ikke said:
We would like to write a small application, involving a 3D renderer. One
of us is developing the renderer in C#, mainly because of the
possibilities with the new XNA platform. However, the rest of us are
more familiar with Delphi (not Delphi .Net), so we're trying to come up
with some sort of interface.

Ah, so you're going the COM interop route. I understand.

-- Barry
 
T

Todd Carnes

Peter said:
[...]
Another thing that someone else here will have to confirm or deny,
because I can't remember off the top of my head, but I think all the
methods in the dll have to be declared as static.

Not true. In fact, being able to only call static methods in a DLL
would make all of .NET pretty useless. Every instance member of a .NET
class is accessed from a .NET DLL.

Pete

Thank you for clearing that up, Pete. Like I said, I'm still learning
and wasn't sure.

Todd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top