Structure to MemoryStream

Posted on 2006-04-27
Last Modified: 2008-03-06
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

Question by:Kristao
    LVL 85

    Accepted Solution

    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
    LVL 1

    Author Comment

    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

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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
    Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now