Data Between Source Files in C#

Asked 2 years ago, Updated 2 years ago, 75 views

I'm sorry for the rudimentary question, but I'd like to ask you a question.

languages:C#

We are currently working on an application that runs in the following order.
Read file data → Convert read data → Convert and output
This is what I've done. I've made each process a separate file and a separate class for my study.

I would like to read, convert, and output file data into a variable called ByteWork.
How
the location of the ByteWork instance at that time and the argument return value when calling each function, etc. I'm at a standstill to see if it works well.
The number of elements to be taken when you instance ByteWork is to take the number of bytes of the file data that you read, and I put it in the Read class for ByteWork to remember...
I think it will disappear if I skip Re.ReadFileMethod().

Thank you for your guidance and encouragement on how to do it well.

Indicates the file configuration and the program that has questions.
I'm sorry for the program on the way. Please forgive me.
Form1.cs:main, invoking each action
Read.cs:loading file data
Convert.cs:convert read data
Out.cs:output

Form1.cs↓↓↓↓↓↓

publicForm1()
    {
        InitializeComponent();
    }

    // Press button to start processing
    private void ConvertB_Click(object sender, EventArgse)
    {
        // loading
        Read Re=new Read();
        Re.ReadFileMethod();
        // Conversion
        Convert Cn = new Convert();
        Cn.ConvertMethod();
        // Fishing
        OutPutOp = new OutPut();
        Op.OutputMethod();
    }
}

Read.cs↓↓↓↓↓↓

public class Read
{
    public void ReadFileMethod()
    {
        FileStream fs=new FileStream(@"C:\cWork\bin.bin", FileMode.Open);
        byte [ ] ByteWork = new byte [fs.Length];
        fs.Read (ByteWork, 0, ByteWork.Length);
        fs.Close();
    }
}

c# algorithm

2022-09-30 21:22

2 Answers

As is feared, ByteWork is a local variable in the method, so it is not referenced after processing the method, and it will be discarded and cannot be referenced by any other code.In such cases, the return value is usually used to return the results to the caller.First ReadFileMethod return value

public byte[]ReadFileMethod()
{
    FileStream fs=new FileStream(@"C:\cWork\bin.bin", FileMode.Open);
    byte [ ] ByteWork = new byte [fs.Length];
    fs.Read (ByteWork, 0, ByteWork.Length);
    fs.Close();

    return ByteWork;
}

Rewrite to .Caller adds variable declaration and initialization

Read Re=new Read();
byte [ ] ByteWork = Re.ReadFileMethod();

would be good.

To pass this variable to ConvertMethod, add the provisional argument ByteWork to

class Convert
{
    public byte [ ] ConvertMethod (byte[]ByteWork)
    {
        // actual processing
    }
}

and so on.

Convert Cn=new Convert();
byte[] ByteWork2 = Cn. ConvertMethod (ByteWork);

Call with the actual arguments as shown in .


2022-09-30 21:22

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.