PC Review


Reply
Thread Tools Rate Thread

ByVal copy a variable

 
 
Miro
Guest
Posts: n/a
 
      28th Apr 2010
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



 
Reply With Quote
 
 
 
 
Cor Ligthert[MVP]
Guest
Posts: n/a
 
      28th Apr 2010
You have to serialize it first.


"Miro" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>
>
>

 
Reply With Quote
 
Tom Shelton
Guest
Posts: n/a
 
      28th Apr 2010
On 2010-04-28, Miro <(E-Mail Removed)> 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
 
Reply With Quote
 
Tom Shelton
Guest
Posts: n/a
 
      28th Apr 2010
On 2010-04-28, Tom Shelton <(E-Mail Removed)> wrote:
> On 2010-04-28, Miro <(E-Mail Removed)> 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
 
Reply With Quote
 
AMercer
Guest
Posts: n/a
 
      28th Apr 2010
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
>
>
>
> .
>

 
Reply With Quote
 
Michel Posseth [MCP]
Guest
Posts: n/a
 
      28th Apr 2010


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" <(E-Mail Removed)> schreef in bericht
news:(E-Mail Removed)...
> 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
>
>
>


 
Reply With Quote
 
Miro
Guest
Posts: n/a
 
      29th Apr 2010
Thanks tom - I will pull out my vb.net book and try to do some reading.

Many thanks,

Miro

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


 
Reply With Quote
 
Miro
Guest
Posts: n/a
 
      29th Apr 2010
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" <(E-Mail Removed)> wrote in message
news:FF8D93B1-8FBD-41FA-8CB9-(E-Mail Removed)...
> 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
>>
>>
>>
>> .
>>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ByVal copy a variable Miro Microsoft ASP .NET 7 29th Apr 2010 01:19 AM
Using a Range variable to call a procedure with a ByVal statement... R Tanner Microsoft Excel Programming 14 18th Sep 2008 08:40 PM
copy a table with variable name and set variable default value for =?Utf-8?B?YSBtIGpvdWRp?= Microsoft Access VBA Modules 0 24th Sep 2004 07:17 AM
What does (ByVal sender As System.Object, ByVal e As System.EventArgs) mean? Kathy Burke Microsoft ASP .NET 2 1st Aug 2003 01:22 PM
Re: How to set default to TRUE: Overrides Function OnBubbleEvent(ByVal source As Object, ByVal args As EventArgs) As Boolean? David Waz... Microsoft ASP .NET 0 4th Jul 2003 08:32 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:47 PM.