• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 450
  • Last Modified:

easy question: vb keyword --> c#

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
2pac4ever
Asked:
2pac4ever
  • 3
  • 2
  • 2
2 Solutions
 
AlexFMCommented:
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
 
smitty22Commented:
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
 
2pac4everAuthor Commented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
smitty22Commented:
Using the Visual Studio .NET Command Prompt (in the VS .NET Tools menu), you would type

csc /unsafe YourClass.cs
0
 
2pac4everAuthor Commented:
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
 
AlexFMCommented:
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
 
2pac4everAuthor Commented:
omg i just discovered how to do what i wanted :)

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

Featured Post

Industry Leaders: 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!

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