ByVal copy a variable

  • Thread starter Thread starter Miro
  • Start date Start date
M

Miro

Hi,

(vb.net vs2008)

I understand the significance between memory pointers with a function using
byVal and byRef.

However I have code that I want to do something like this:

Dim UsMailmsg As MailMessage = EmailOrderClass.createEmailOrder(False,
cartOrderID)
For nI As Integer = 0 To SendToEmailAddresses.Count - 1
EmailOrderClass.sendEmailOrder(CustomerAddress,
ToEmailAddress, UsMailmsg) <------- this line

Now When I am using the UsMailmsg, even though the function is specified as
"byVal" it will always pass in as a reference to a memory pointer.
How do I pass it in as a 'Direct Copy' of the variable.

So basically I can create an 'orig' version of the UsMailmsg, then it passes
a copy of it into a function where that function changes parameters left and
right.
But on the next "for next loop" it will send in the orig for the next run of
something else.

Hope that makes sense.

Thanks,

Miro
 
Hi,

(vb.net vs2008)

I understand the significance between memory pointers with a function using
byVal and byRef.

However I have code that I want to do something like this:

Dim UsMailmsg As MailMessage = EmailOrderClass.createEmailOrder(False,
cartOrderID)
For nI As Integer = 0 To SendToEmailAddresses.Count - 1
EmailOrderClass.sendEmailOrder(CustomerAddress,
ToEmailAddress, UsMailmsg) <------- this line

Now When I am using the UsMailmsg, even though the function is specified as
"byVal" it will always pass in as a reference to a memory pointer.
How do I pass it in as a 'Direct Copy' of the variable.

The common way would to implement the IClonable.Clone method - which would
return a deep copy of the object.
 
The common way would to implement the IClonable.Clone method - which would
return a deep copy of the object.

Building upon this - you should realy take a combination of mine and Cor's
replies :) A common way to make a deep copy of an object is to serialize it
to a memorystream and then desearialize from the memorystream....
 
As I understand your question, you need to create a new object, pass it to
sendEmail Order(), and then discard it. Perhaps new object could be created
by calling createEmailOrder(), or perhaps by UsMailmsg.MemberwiseClone(), or
perhaps you need to write a custom cloner. Regardless, if sendEmailOrder
changes object properties and you want to ignore them, then you need a fresh
(new or cloned) object at each iteration.

AMercer
 
Hello Miro

You might find this generic code interesting ( i made it in VB 10 but just
convert the auto property to a normall property and it will also work in VB
2008 )
The solution for your propblem is the cloneobject method


regards

Michel



'Michel Posseth
Option Infer On
Option Strict On
Option Explicit On
Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters
Imports System.Runtime.Serialization.Formatters.Binary
Public Class BinarySerialization
Public Sub SerializeToFile(Of T)(ByVal path As String, ByVal obj As T)
Using fs As New FileStream(path, FileMode.Create)
bf = New BinaryFormatter(Nothing, New
StreamingContext(StreamingContextStates.File))
bf.Serialize(fs, obj)
End Using
End Sub
Public Property bf As BinaryFormatter
Public Function SerializeToMStream(Of T)(ByVal obj As T) As MemoryStream
Using ms As New MemoryStream(2048)
bf = New BinaryFormatter(Nothing, New
StreamingContext(StreamingContextStates.Clone))
bf.Serialize(ms, obj)
ms.Seek(0, SeekOrigin.Begin)
Return ms
End Using
End Function
Public Function SerializeToByteArray(Of T)(ByVal obj As T) As Byte()
Return SerializeToMStream(obj).ToArray
End Function
Public Function DeserializeFromFile(Of T)(ByVal path As String) As T
Using fs As New FileStream(path, FileMode.Open)
bf = New BinaryFormatter(Nothing, New
StreamingContext(StreamingContextStates.File))
Return DirectCast(bf.Deserialize(fs), T)
End Using
End Function
Public Function DeserializeFromMstream(Of T)(ByVal ms As MemoryStream) As T
bf = New BinaryFormatter(Nothing, New
StreamingContext(StreamingContextStates.Other))
ms.Seek(0, SeekOrigin.Begin)
Return DirectCast(bf.Deserialize(ms), T)
End Function
Public Function DeserializeFromByteArray(Of T)(ByVal buffer As Byte()) As T
bf = New BinaryFormatter(Nothing, New
StreamingContext(StreamingContextStates.Other))
Using ms As New MemoryStream(buffer)
ms.Seek(0, SeekOrigin.Begin)
Return DirectCast(bf.Deserialize(ms), T)
End Using
End Function
Public Function CloneObject(Of T)(ByVal obj As T) As T
Using ms As New MemoryStream(2048)
Return DirectCast(bf.Deserialize(SerializeToMStream(obj)), T)
End Using
End Function
End Class
 
I didnt want to do a "new" as within the "new" function I do a lot of
calculations for 'defaults' that I would rather not do through each
itteration.

Thats why I want to setup a 'base original' and then just copy from there.

Cheers'

Miro
 
Back
Top