Pointers

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is it possible (in vb.net) to pass the address of a variable into a function, assign a pointer to the variable that was passed as a parameter, and change the pointer so that the variable will be changed in the calling function? Here is an example in C++

void changer(char &b

char *c = &b
c[0] = 'P'
c[1] = 'A'
c[2] = 'S'
c[3] = 'S'


void main(

char a[] = "Test"
cout << a << endl
changer(a[0])
cout << a << endl


variable a in main is changed to PASS.
 
Yes it is, use ByRef

Sub VoidChanger(ByRef b as String)
b="Changed"
End Sub

--Alex Papadimoulis

OS said:
Is it possible (in vb.net) to pass the address of a variable into a
function, assign a pointer to the variable that was passed as a parameter,
and change the pointer so that the variable will be changed in the calling
function? Here is an example in C++:
void changer(char &b)
{
char *c = &b;
c[0] = 'P';
c[1] = 'A';
c[2] = 'S';
c[3] = 'S';
}

void main()
{
char a[] = "Test";
cout << a << endl;
changer(a[0]);
cout << a << endl;
}

variable a in main is changed to PASS.
 
But can I assign the address of the ByRef b as string to a pointer? For example:

Public myVar as string (or some time of pointer)

Sub VoidChanger(ByRef b as string)
myVar = b
End sub

Now when myVar is used it will change the variable that b was pointing to.

----- Alex Papadimoulis wrote: -----

Yes it is, use ByRef

Sub VoidChanger(ByRef b as String)
b="Changed"
End Sub

--Alex Papadimoulis

OS said:
Is it possible (in vb.net) to pass the address of a variable into a
function, assign a pointer to the variable that was passed as a parameter,
and change the pointer so that the variable will be changed in the calling
function? Here is an example in C++:
void changer(char &b)
{
char *c = &b;
c[0] = 'P';
c[1] = 'A';
c[2] = 'S';
c[3] = 'S';
}
void main()
{
char a[] = "Test";
cout << a << endl;
changer(a[0]);
cout << a << endl;
}
variable a in main is changed to PASS.
 
But can I assign the address of the ByRef b as string to a pointer? For example:

No, VB.NET doesn't support pointer types.

Now when myVar is used it will change the variable that b was pointing to.

That's the idea with using ByRef parameters.



Mattias
 
Back
Top