[Last Call] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 659
  • Last Modified:

Structure to MemoryStream

How to write in Memorystream, Structure. Or mybe its inposible :) i doubt that.

Structor its self is stored in memory so in theory it is posible to write it in Memorystream.

in delphi its easy m'kay =)


 stream.write(buf, sizeof(buf));

thats how it is done in Delphi.

In APS.NET i tried more than i want to continue it.

so i have somethin like this now


Imports System
Imports System.Net
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.IO
Imports System.IO.IsolatedStorage
Imports System.Threading
Imports System.Net.Sockets
Imports System.Text
Imports System.Globalization

Public Structure sHeder
    Dim Versija As Integer
End Structure

Partial Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim st As New MemoryStream
        Dim hed As sHeder

        hed.Versija = 1

        st.Write(hed, 0, 11) ''Ye ye ye i know it doesn't work like this, here i need some ideas.
        st.Seek(0, SeekOrigin.Begin)

    End Sub
End Class

1 Solution
Mike TomlinsonMiddle School Assistant TeacherCommented:
Not sure what your end goal is...this might spark some ideas:

Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary

Public Class Form1

    <Serializable()> Public Structure sHeder

        Public Versija As Integer

        Public Function Clone() As sHeder
            Dim bf As BinaryFormatter = New BinaryFormatter
            Dim ms As MemoryStream = New MemoryStream
            bf.Serialize(ms, Me)
            ms.Seek(0, SeekOrigin.Begin)
            Dim sh As sHeder = CType(bf.Deserialize(ms), sHeder)
            Return sh
        End Function

    End Structure

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim hedA, hedB As sHeder
        hedA.Versija = 1
        hedb.Versija = 2
        Debug.WriteLine("Two different hSeder structures:")
        Debug.WriteLine("hedA.Versija = " & hedA.Versija)
        Debug.WriteLine("hedB.Versija = " & hedb.Versija)

        Debug.WriteLine("After cloning:")
        hedb = hedA.Clone
        Debug.WriteLine("hedA.Versija = " & hedA.Versija)
        Debug.WriteLine("hedB.Versija = " & hedb.Versija)
    End Sub

End Class
KristaoAuthor Commented:
Yep this is what i seek.

Now why i need it. I thik you noticed this "Imports System.Net.Sockets" =)

I cut out litle part of tcpclient functions. So it wouldn't do mess in exsample. In Delphi comunication, i transfer all data in one big stream. Stream wich includes heders, wich countains information about stream, wich comes in. So becouse i need to connect to server wich is writen in Delphi, i need to remake comunication in ASP.NET. I say nothing hard, but with my litle experiance in ASP.NET don't know all posibilitys in ASP.NET. For exsample to this day i didn't realy know is it posible to make tcp/ip client. But i found its prety easy, love asp.net.

Don't realy know will this exsample what you gave me will work like i wan't, but you gave me the idea, and the way i need to look. thx =)

If you are interested about results i can put here an exsample of my code, after it will be ready :).


Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now