Results 1 to 9 of 9

Thread: What is the counterpart of this C pointer programme in VB.Net?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2008
    Posts
    16

    Question What is the counterpart of this C pointer programme in VB.Net?

    I'd like to know how the following programme on pointer written in C can be written in VB.Net, especially how to use the keywords in the C programme like &i and *p in VB.Net. Please clarify.

    Code:
    #include <stdio.h>
    
    void f(int *p, int *q)
    {
    	p = q;
    	*p = 2;
    }
    
    int i = 0, j = 1;
    
    int main()
    {
    	f(&i, &j);
    	printf("%d %d \n", i, j);
    	return 0;
    }

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: What is the counterpart of this C pointer programme in VB.Net?

    You've posted this question in both the VB.NET and C# forums. You don't need to write the same app in both languages so how about you make up your mind which you want and save us wasting our time doing twice the work? Instead of expecting us to translate code for you, how about you understand the functionality implemented in the C code and then work out how to implement the same functionality in your chosen language? C# supports pointers while VB.NET does, but you almost certainly shouldn't be using pointers in C# for something this trivial. They should only be used in specific situations where they make a significant difference to performance.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,500

    Re: What is the counterpart of this C pointer programme in VB.Net?

    Hi,

    You may find this useful.


    Poppa
    Along with the sunshine there has to be a little rain sometime.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: What is the counterpart of this C pointer programme in VB.Net?

    Quote Originally Posted by Poppa Mintin View Post
    Hi,

    You may find this useful.


    Poppa
    I didn't see the link at first, and thought you had either forgotten something, or were making some kind of ironic comment that I didn't understand.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Feb 2008
    Posts
    16

    Re: What is the counterpart of this C pointer programme in VB.Net?

    Quote Originally Posted by jmcilhinney View Post
    You've posted this question in both the VB.NET and C# forums. You don't need to write the same app in both languages so how about you make up your mind which you want and save us wasting our time doing twice the work? Instead of expecting us to translate code for you, how about you understand the functionality implemented in the C code and then work out how to implement the same functionality in your chosen language? C# supports pointers while VB.NET does, but you almost certainly shouldn't be using pointers in C# for something this trivial. They should only be used in specific situations where they make a significant difference to performance.
    The question got posted in VB.Net forum redundantly as that's when the router got to be restarted. I thought it didn't get posted here. However, we've got a significant answer from OptionBase1 here. So let this post remain for future reference.
    Last edited by priyamtheone; Mar 18th, 2021 at 03:02 PM.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Feb 2008
    Posts
    16

    Re: What is the counterpart of this C pointer programme in VB.Net?

    Quote Originally Posted by Poppa Mintin View Post
    Hi,

    You may find this useful.


    Poppa
    May be I misunderstood, but I don't see any option in that site to convert code from "C" to C#. Am I missing something?

  7. #7
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,630

    Re: What is the counterpart of this C pointer programme in VB.Net?

    From the book Inside C#, ISBN 0735616485:

    Code:
    Operator Description
    &   The address-of operator returns a pointer that represents the memory address of the variable.
    *   The dereference operator is used to denote the value pointed at by the pointer.
    ->  The dereferencing and member access operator is used for member access and pointer dereferencing.
    Code:
    // Compile this application with the /unsafe option.
    
    using System;
    
    class Unsafe1App
    {
        public static unsafe void GetValues(int* x, int* y)
        {
            *x = 6;
            *y = 42;
        }
    
        public static unsafe void Main()
        {
            int a = 1;
            int b = 2;
            Console.WriteLine("Before GetValues() : a = {0}, b = {1}", a, b);
            GetValues(&a, &b);
            Console.WriteLine("After GetValues() : a = {0}, b = {1}", a, b);
        }
    }
    Edited to add:

    The book this came from was published for C# from VS.NET 2002, which was the first version of C#. That's also the only version of C# I've ever used, so it is entirely possible that things have changed since then, such as the whole "unsafe" stuff mentioned above.

    Good luck.
    Last edited by OptionBase1; Mar 18th, 2021 at 02:59 PM.

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Feb 2008
    Posts
    16

    Re: What is the counterpart of this C pointer programme in VB.Net?

    Quote Originally Posted by OptionBase1 View Post
    From the book Inside C#, ISBN 0735616485:

    Code:
    Operator Description
    &   The address-of operator returns a pointer that represents the memory address of the variable.
    *   The dereference operator is used to denote the value pointed at by the pointer.
    ->  The dereferencing and member access operator is used for member access and pointer dereferencing.
    Code:
    // Compile this application with the /unsafe option.
    
    using System;
    
    class Unsafe1App
    {
        public static unsafe void GetValues(int* x, int* y)
        {
            *x = 6;
            *y = 42;
        }
    
        public static unsafe void Main()
        {
            int a = 1;
            int b = 2;
            Console.WriteLine("Before GetValues() : a = {0}, b = {1}", a, b);
            GetValues(&a, &b);
            Console.WriteLine("After GetValues() : a = {0}, b = {1}", a, b);
        }
    }
    Edited to add:

    The book this came from was published for C# from VS.NET 2002, which was the first version of C#. That's also the only version of C# I've ever used, so it is entirely possible that things have changed since then, such as the whole "unsafe" stuff mentioned above.

    Good luck.
    Very precise and relevant answer. I was looking for a reference exactly like this. This answer set the ball rolling for me. Now I can take the pointer concepts in C# further. Thank you very much!

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: What is the counterpart of this C pointer programme in VB.Net?

    Well, you're going to HAVE to keep going in C#, because unsafe pointers have never been a part of VB.
    My usual boring signature: Nothing

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width