Solved

easy question: vb keyword --> c#

Posted on 2003-12-06
7
444 Views
Last Modified: 2012-05-04
sup sup...

what is the c# equivalent of visual basic's AddressOf keyword?

also what does it return, for example an int, IntPtr or something else?

thankz in advance
0
Comment
Question by:2pac4ever
  • 3
  • 2
  • 2
7 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 70 total points
ID: 9891458
Suppose you have some function, for example:

class MyClass
{
    int SomeFunction(int x1, int x2)
    {
         // ...
    }
};

To get address of this function you shoud define delegate (C# function pointer):

public delegate int SomeFunctionDelegate(int x1, int x2);   // delegate type

and create delegate instance initializing it with pointer to the function:

SomeFunctionDelegate m_Address = new SomeFunctionDelegate(this.SomeFunction);

Now use m_Address as AddressOf SomeFunction.


Full code:

class MyClass
{
    public delegate int SomeFunctionDelegate(int x1, int x2);   // delegate type
    SomeFunctionDelegate m_Address;                                  // instance

    int SomeFunction(int x1, int x2)
    {
         // ...
    }

    void CreateAddressOfFunction()
    {
        m_Address = new SomeFunctionDelegate(this.SomeFunction);
    }

    void UseAddressOfFunction()
    {
         // use m_Address as AddressOf SomeFunction
    }
};
0
 
LVL 2

Assisted Solution

by:smitty22
smitty22 earned 60 total points
ID: 9891465
Like C, C# uses the unary & operator for addressOf.  A few things to note... in order to work with pointers in a C# method, you must mark the method as "unsafe" using the unsafe keyword.   Working with C# in unsafe mode is generally discouraged, but can be necessary in some situations.


int x = 2;
int* p = &x;
0
 

Author Comment

by:2pac4ever
ID: 9893815
smitty22

I think that this is exactly what I am looking for but I am having trouble compiling the code.

Is there a way to compile unsafe code using visual c# .net's build from the build menu? if not, can you tell me what must i type into the command-line compiler (the error message says i have to include "/unsafe" but i am not familiar with the command-line compiler)
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 2

Expert Comment

by:smitty22
ID: 9893920
Using the Visual Studio .NET Command Prompt (in the VS .NET Tools menu), you would type

csc /unsafe YourClass.cs
0
 

Author Comment

by:2pac4ever
ID: 9894424
ok ive got that working...

lets say this is my method:

public int test(int x)
{
         return(x*2);
}

how do i refer to the method instead of just an int?
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 9894920
Please be more specific. What is "refer to the method"? Your question was "c# equivalent of visual basic's AddressOf keyword". What is the context where you want to use it?
0
 

Author Comment

by:2pac4ever
ID: 9895001
omg i just discovered how to do what i wanted :)

both u guyz were very helpful so ima split the points.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question