Solved

Reading and writing multidimensional arrays to a file in VB.NET

Posted on 2007-11-16
8
787 Views
Last Modified: 2008-02-01
I have the following array:
   Private Structure ObjData
      Dim Image As image
      Dim Path As String
      Dim Name As String
   End Structure
   Dim GoData(2, 8, 7) As ObjData
I nead an easy method to store and retrieve this data within my program.
Also can I append this data to the program itself?
0
Comment
Question by:hlominac
  • 3
  • 3
  • 2
8 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20298095
Where are you looking to store the structure?  Database?  File?

Bob
0
 

Author Comment

by:hlominac
ID: 20298260
File -- I haven't been using databases.  I do not know what form to use if I can append it to the execution file.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20298351
You can embed readonly files within an executable, but not files that you need to update.  What are you trying to achieve by embedding the data within the executable?

Bob
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 63

Accepted Solution

by:
Fernando Soto earned 450 total points
ID: 20298417
Hi hlominac;

If you want to serialize the object to a disk file and be able to read it back into an object of ObjData you can follow the sample code attached.


    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
 
        Dim img As Image = Image.FromFile("Dice.gif")
        Dim GoData(2, 8, 7) As ObjData
        Dim obj1 As New ObjData
        obj1.Image = img
        obj1.Path = "C:\Temp"
        obj1.Name = "Dice0"
        GoData(0, 0, 0) = obj1
        obj1 = New ObjData
        obj1.Image = img
        obj1.Path = "C:\Temp"
        obj1.Name = "Dice1"
        GoData(0, 1, 0) = obj1
        obj1 = New ObjData
        obj1.Image = img
        obj1.Path = "C:\Temp"
        obj1.Name = "Dice2"
        GoData(0, 1, 1) = obj1
        obj1 = New ObjData
        obj1.Image = img
        obj1.Path = "C:\Temp"
        obj1.Name = "Dice3"
        GoData(0, 1, 2) = obj1
        obj1 = New ObjData
        obj1.Image = img
        obj1.Path = "C:\Temp"
        obj1.Name = "Dice4"
        GoData(1, 1, 3) = obj1
        obj1 = New ObjData
        obj1.Image = img
        obj1.Path = "C:\Temp"
        obj1.Name = "Dice5"
        GoData(1, 2, 5) = obj1
 
        Dim fs As New FileStream("C:\Temp\SerialObjData.bin", FileMode.Create)
        Dim bf As New BinaryFormatter
        bf.Serialize(fs, GoData)
        fs.Flush()
        fs.Close()
 
        Console.WriteLine("Hello")
 
    End Sub
 
    Dim DeSerialObjData As ObjData(,,)
 
    Private Sub Button2_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button2.Click
 
        Dim fs2 As New FileStream("C:\Temp\SerialObjData.bin", FileMode.Open)
        Dim bf2 As New BinaryFormatter
        DeSerialObjData = DirectCast(bf2.Deserialize(fs2), ObjData(,,))
        fs2.Close()
 
        PictureBox1.Image = DeSerialObjData(0, 0, 0).Image
 
    End Sub
End Class
 
<Serializable()> _
Public Class ObjData
    Public Image As Image
    Public Path As String
    Public Name As String
End Class

Open in new window

0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 20298445
Hi hlominac;

I also changed your structure to a class. The reason for this is that a structure is stored on the stack and with the size of the array and images being stored you will most likely get a stack overflow. A class is stored on the heap and managed by the garbage collection.

Fernando
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 20298510
Sorry hlominac;

You will also need the following two imports to test my code.

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

Fernando
0
 

Author Comment

by:hlominac
ID: 20299687
I generally use the register to save parameters.  I had hoped that I could read/write data to it.  What is normally stored there.  I suspect this is checked for viruses, is that true?
0
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 50 total points
ID: 20300213
If you are storing application settings, using an app.config file is the general approach to the problem.

Bob
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need a starter for ETL protocol? 4 57
VB.NET Repostiory Pattern 7 38
VB.NET (2008) - Refactoring Question 2 21
Complex SQL statement in VB.NET 7 16
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

831 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