Solved

easy question: vb keyword --> c#

Posted on 2003-12-06
7
446 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
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.

730 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