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();
}
}
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 .
This is not a direct answer, but File.ReadAllBytes()
method is provided for Read.ReadFileMethod() to read the entire file.
© 2024 OneMinuteCode. All rights reserved.