Solved

easy question: vb keyword --> c#

Posted on 2003-12-06
7
447 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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!

 
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

Technology Partners: 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!

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

726 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