|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
ByVal copy a variable(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 You have to serialize it first.
Show quoteHide quote "Miro" <m***@beero.com> wrote in message news:ual7L0u5KHA.3292@TK2MSFTNGP06.phx.gbl... > 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 > > > > On 2010-04-28, Miro <m***@beero.com> wrote:
Show quoteHide quote > Hi, The common way would to implement the IClonable.Clone method - which would> > (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. > return a deep copy of the object. -- Tom Shelton On 2010-04-28, Tom Shelton <tom_shel***@comcastXXXXXXX.net> wrote:
Show quoteHide quote > On 2010-04-28, Miro <m***@beero.com> wrote: Building upon this - you should realy take a combination of mine and Cor's>> 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. > 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 Thanks tom - I will pull out my vb.net book and try to do some reading.
Many thanks, Miro Show quoteHide quote "Tom Shelton" <tom_shel***@comcastXXXXXXX.net> wrote in message news:e#FWeFv5KHA.5016@TK2MSFTNGP02.phx.gbl... > On 2010-04-28, Tom Shelton <tom_shel***@comcastXXXXXXX.net> wrote: >> On 2010-04-28, Miro <m***@beero.com> 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 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 Show quoteHide quote "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 > > > > . > 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 Show quoteHide quote "AMercer" <AMer***@discussions.microsoft.com> wrote in message news:FF8D93B1-8FBD-41FA-8CB9-9DFA4F4A4D62@microsoft.com... > 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 >> >> >> >> . >> 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 Show quoteHide quote "Miro" <m***@beero.com> schreef in bericht news:ual7L0u5KHA.3292@TK2MSFTNGP06.phx.gbl... > 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 > > >
design pattern examples
string split by first occurens number Re: How to send email with no outlook and SMTP server ? Is it possible with Linq to update a dataset? Math drawing question Determine depend on services Integrating the webBrowser controlling form Can't evaluate expressionin IDE debug mode Upload/Download files in Chunks on FTP The string font is ugly on chart |
|||||||||||||||||||||||