ByVal copy a variable

Discussion in 'ASP .Net' started by Miro, Apr 28, 2010.

  1. Miro

    Miro Guest

    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
     
    Miro, Apr 28, 2010
    #1
    1. Advertising

  2. You have to serialize it first.


    "Miro" <> wrote in message
    news:...
    > 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
    >
    >
    >
    >
     
    Cor Ligthert[MVP], Apr 28, 2010
    #2
    1. Advertising

  3. Miro

    Tom Shelton Guest

    On 2010-04-28, Miro <> wrote:
    > 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.

    --
    Tom Shelton
     
    Tom Shelton, Apr 28, 2010
    #3
  4. Miro

    Tom Shelton Guest

    On 2010-04-28, Tom Shelton <> wrote:
    > On 2010-04-28, Miro <> wrote:
    >> 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.
    >


    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....

    --
    Tom Shelton
     
    Tom Shelton, Apr 28, 2010
    #4
  5. Miro

    AMercer Guest

    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

    "Miro" wrote:

    > 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
    >
    >
    >
    > .
    >
     
    AMercer, Apr 28, 2010
    #5
  6. 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





    "Miro" <> schreef in bericht
    news:...
    > 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
    >
    >
    >
     
    Michel Posseth [MCP], Apr 28, 2010
    #6
  7. Miro

    Miro Guest

    Thanks tom - I will pull out my vb.net book and try to do some reading.

    Many thanks,

    Miro

    "Tom Shelton" <> wrote in message
    news:e#...
    > On 2010-04-28, Tom Shelton <> wrote:
    >> On 2010-04-28, Miro <> wrote:
    >>> 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.
    >>

    >
    > 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....
    >
    > --
    > Tom Shelton
     
    Miro, Apr 29, 2010
    #7
  8. Miro

    Miro Guest

    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

    "AMercer" <> wrote in message
    news:...
    > 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
    >
    > "Miro" wrote:
    >
    >> 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
    >>
    >>
    >>
    >> .
    >>
     
    Miro, Apr 29, 2010
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Waz...
    Replies:
    0
    Views:
    410
    David Waz...
    Jul 4, 2003
  2. Kathy Burke
    Replies:
    2
    Views:
    5,149
    Kevin Spencer
    Aug 1, 2003
  3. Showjumper
    Replies:
    2
    Views:
    1,815
    Showjumper
    Aug 2, 2003
  4. Andreas Klemt
    Replies:
    1
    Views:
    133
    Tim Pacl
    Jul 18, 2003
  5. David Lozzi
    Replies:
    2
    Views:
    352
    Harolds
    Jul 22, 2005
Loading...

Share This Page