Solved

easy question: vb keyword --> c#

Posted on 2003-12-06
7
442 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now