Reputation: 3089
So I want a pointer to a pointer.
I've got a class that is updated in one object. In another object I want a reference to the current version of the object in the original Class. Here is sort of a simplified version of what I have going on.
Public Class Foo
'conaints information
End Class
Public Class myMainApp
Dim myHelper As HelperClass
Dim currentFoo As Foo
Private Sub init()
currentFoo = New Foo()
myHelper = New HelperClass(currentFoo)
End Sub
Private Sub UpdatingUI()
currentFoo = GetFooFromContext()
End Sub
Private Function GetFooFromContext() As Foo
Return New Foo()
End Function
End Class
Public Class HelperClass
Private myFoo As Foo
Public Sub New(ByVal aFoo As Foo)
myFoo = aFoo
End Sub
End Class
if thise was C++, currentFoo would be a Foo* and HelperClass's myFoo would be a Foo** so that whenever we updated currentFoo's refrence to a new object the HelperClass would also be accessing this new object.
Is there syntax to accomplish this in the .net world?
Upvotes: 1
Views: 151
Reputation: 61233
yes, you could do exactly what you're thinking in C# using unsafe code - but seriously, don't.
Reed's advice is the best so far; i suspect with more context we could suggest a better solution, for example:
mostly i just wanted to emphasize the omg-don't-do-that aspect of this question ;-)
Upvotes: 0
Reputation: 16121
You need to share a reference to a class that has Foo as a property. Sorry to write in C#;-)
class MyMainApp
{
private HelperClass myHelper;
private FooBar myFooBar;
public void Init()
{
myFooBar=new FooBar(new Foo());
myHelper=new HelperClass(myFooBar);
}
public void UpdateFromUI()
{
myFooBar.CurrentFoo = GetFooFromContext();
myHelper.DoSomethingWithTheFoo();
}
private Foo GetFooFromContext()
{
return new Foo();
}
}
class FooBar
{
public Foo CurrentFoo;
public FooBar(Foo new_foo)
{
CurrentFoo = new_foo;
}
}
class HelperClass
{
private FooBar myFooBar;
public HelperClass(FooBar new_foobar)
{
myFooBar = new_foobar;
}
public void DoSomethingWithTheFoo()
{
myFooBar.CurrentFoo.JumpThroughHoop();
}
}
class Foo
{
public void JumpThroughHoop()
{}
}
Upvotes: 0
Reputation: 101565
Reed's answer is the typical idiomatic C# way to handle this, but if you really want it, you can always just introduce your own "reference wrapper" helper class:
public class Reference<T> where T : class
{
public T Value { get; set; }
}
Then declare both currentFoo
and myFoo
as Reference<Foo>
, and also make sure that currentFoo
is readonly
, so that no-one can assign a different Reference
to it.
Upvotes: 1
Reputation: 564413
There isn't a direct equivalent to this.
The best option is usually to have your HelperClass actually keep a reference to your main class, and read/write Foo as needed from a property on your main class. This way, it has a reference to the class that holds Foo, instead of Foo itself. This is still not the same, though, since if you change the main classes "Foo" instance, you'll lose the reference to the original (unless you've saved that, as well).
Upvotes: 3