Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getting a variable to point at another... byref

Status
Not open for further replies.

ThatRickGuy

Programmer
Oct 12, 2001
3,841
US
Hey guys, I'm having a brain fart this afternoon.

I have a method that takes a parameter byRef. I need to use that variable, and modify it, and I want to sender to see the same changes. The problem is I need to use the variable out side of the scope of that specific method.

here's a short version:
Code:
class MyClass
  private _MyVar as string

  public sub New(byRef MyVar as string)
    [b]_MyVar = MyVar[/b]
  end sub

  Public sub SomethingElse()
    _MyVar &= "SomethingElse"
  End sub
end class

The bolded line is, as expected copying the value that MyVar contained when it was passed in. But I want _MyVar to point at the same object in memory. And for the life of me, I can't remember how.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
I think that won't work with a string try doing it with an object. Wait I'll try that.



Christiaan Baes
Belgium

My Blog
"In a system where you can define a factor as part of a third factor, you need another layer to check the main layer in case the second layer is not the base unit." - jrbarnett
 
Unfortunately, no. At least, not with out going through a good bit of extra work...

The object that is getting passed around is an object that stores credential info for database logins. The first time I pass the credentials into the DAL it is nothing. The DAL instantiates and populates it, and stores it local to that object, but I wanted to keep a reference to that single credentials object. so that back at the calling code, after the first DAL call is completed, the variable that I passed in would then be a populated credentials object.

I have a workaround in place, I just pull the value out manually on the next line via a property. But it would have been cleaner to do it via the byRef parameter.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
actually I was right

Code:
Public Class Class2
    Private _s As String

    Public Property S() As String
        Get
            Return _s
        End Get
        Set(ByVal value As String)
            _s = value
        End Set
    End Property
End Class

I created an extra class as above and used that and then it works. I think it has something to witht the fact that strings are immutable.

And then this

Code:
Dim p As New Class2
        p.S = "first"
        Me.TextBox1.AppendText(p.S & ControlChars.CrLf)
        Dim s As New Class1(p)
        Me.TextBox1.AppendText(p.S & ControlChars.CrLf)
        s.SomethingElse()
        Me.TextBox1.AppendText(p.S & ControlChars.CrLf)

with this

Code:
Public Class Class1
    Private _MyVar As Class2

    Public Sub New(ByRef MyVar As Class2)
        _MyVar = MyVar
    End Sub

    Public Sub SomethingElse()
        _MyVar.S &= "SomethingElse"
    End Sub
End Class





Christiaan Baes
Belgium

My Blog
"In a system where you can define a factor as part of a third factor, you need another layer to check the main layer in case the second layer is not the base unit." - jrbarnett
 
Ahhhhh, I think I've found it.

If you pass in a null reference it will fail. Which as soon as I re-typed it on purpose I was like "doh! This ain't gonna work!"

if you change the Somethingelse method like so:
Code:
Public Sub SomethingElse()
    if _MyVar is nothing then _MyVar = new Class2 
    _MyVar.S &= "SomethingElse"
End Sub

Then run this code:
Code:
  Dim p As Class2
 'p is nothing
  Dim s As New Class1(p)
 'p is nothing  
  s.SomethingElse()
 'p is still nothing, even though the var inside s IS something

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top