"Pointers" in VB.NET?

  • Thread starter Thread starter Sean
  • Start date Start date
S

Sean

I know I can pass a reference to a String into a Form by
overloading the Constructor.

Public Sub New(ByRef strDX As String)
MyBase.New()

InitializeComponent()

End Sub

Is there anyway to set a local value to point to that
memory address so that as I make changes on this form, the
referenced string is modified back on the calling form? Or
do I have implicitly set a public value or property or
call a function on the calling form from the active form
to update the value? Thanks.

Sean
 
Sean said:
I know I can pass a reference to a String into a Form by
overloading the Constructor.

Public Sub New(ByRef strDX As String)
MyBase.New()

InitializeComponent()

End Sub

Is there anyway to set a local value to point to that
memory address so that as I make changes on this form, the
referenced string is modified back on the calling form? Or
do I have implicitly set a public value or property or
call a function on the calling form from the active form
to update the value? Thanks.


The string itself can not be modified because strings are "immutable". If
you pass a string variable to the constructor, you can store the reference
to the string in a local variable or in a field of the class. You can not
store the reference to the passed string variable, neither locally nor as a
field.

You can create a class containing a string variable and pass an instance of
the class to the constructor. The object reference can be stored. As the
caller and the Form point to the same object, modifications made to the
string field within the object are visible from both locations.

Out of interest: What are you trying to accomplish?
 
Firstly, strings are immutable in VB.NET ( cannot be changed ).
Secondly, your post is gibberish to me ( I cannot understand you).

No Offence meant.

Regards - OHM
 
* "Sean said:
I know I can pass a reference to a String into a Form by
overloading the Constructor.

Public Sub New(ByRef strDX As String)
MyBase.New()

InitializeComponent()

End Sub

Is there anyway to set a local value to point to that
memory address so that as I make changes on this form, the
referenced string is modified back on the calling form? Or
do I have implicitly set a public value or property or
call a function on the calling form from the active form
to update the value? Thanks.

Strings are immutable in .NET, you cannot change their value without
(implicitly) creating a new string.

You can pass a reference to an instance of a class "wrapping" the string
class. Quick and Dirty:

\\\
Public Class MyString
Public Value As String
End Class
///

\\\
Dim m As New MyString()
m.Value = "Foo"
Dim x As New Foo(m)
///

\\\
Public Sub New(ByVal m As MyString)
m.Value = "Bla"
End Sub
///
 
Probably just my confusion moving from a C++ world. I just
want to make a generic form that can set a string value on
the calling form. In C++, I would just do something like:

CString csSomeString = "Some string data";
MyForm* form = New MyForm(&csSomeString);

void MyForm::MyForm(CString *pcsString)
{
pcsLocalString = pcsString;

*pcsLocalString = "New string data";
}

.....and I was trying to find a comparable way to do it in
VB.
 
Hi Sean,

Your messages are something confusing yes, are you talking about real memory
adresses or about virtual pointers?

Cor
 
Sean said:
Probably just my confusion moving from a C++ world. I just
want to make a generic form that can set a string value on
the calling form. In C++, I would just do something like:

CString csSomeString = "Some string data";
MyForm* form = New MyForm(&csSomeString);

void MyForm::MyForm(CString *pcsString)
{
pcsLocalString = pcsString;

*pcsLocalString = "New string data";
}

....and I was trying to find a comparable way to do it in
VB.

If you want to change a property of an object you need a reference to the
object. Consequently you should pass the reference to the Form (to the
constructor). Later you can use the Form reference to change the property.
 
Cor,

I was trying to figure out a way to use real memory
addresses, al la C, and wasn't sure if it is possible in
VB.NET. Basically pass in the memory address of some
string from one form calling another, and then point to
that memory address from with the child form, in a C
fashion. I could pass in a reference to the calling form,
and set it's property that way, but I wanted to keep it
more general than that, as there are many uses for the
child form. Seems like the best was from the responses
I've gotten is to just wrap the string in a class. Thanks
everybody for your input by the way.

Sean
 
Sean said:
I was trying to figure out a way to use real memory
addresses, al la C, and wasn't sure if it is possible in
VB.NET. Basically pass in the memory address of some
string from one form calling another, and then point to
that memory address from with the child form, in a C
fashion. I could pass in a reference to the calling form,
and set it's property that way, but I wanted to keep it
more general than that, as there are many uses for the
child form. Seems like the best was from the responses
I've gotten is to just wrap the string in a class. Thanks
everybody for your input by the way.

You have to think in a more OO way. :-) Which object is the string (i.e. the
string reference/variable) part of?
 
* "Sean said:
Probably just my confusion moving from a C++ world. I just
want to make a generic form that can set a string value on
the calling form. In C++, I would just do something like:

CString csSomeString = "Some string data";
MyForm* form = New MyForm(&csSomeString);

void MyForm::MyForm(CString *pcsString)
{
pcsLocalString = pcsString;

*pcsLocalString = "New string data";
}

....and I was trying to find a comparable way to do it in
VB.

2 ways:

Use a wrapper class for 'String' and pass an instance of this class.

- or -

Pass a reference to the form directly and access the property through
this reference.
 
* "Sean said:
I was trying to figure out a way to use real memory
addresses

That's in general not possible (there are some cases when it's needed
(p/invoke), but it's something which is not "preferred").
 
Back
Top