0

I've readed a few articles on stackoverflow but i couldn't figure out how to do it. I've made a small code for reading mixed\binary files (making a new one, with separated methods, the older one was messed) - but i'm in doubt on how to call the method on the program's main.

I figured out i should instantiate each variable before using them on the method's call - but how do i do that with string ? I know this is a newbie question but i'm new to c# - also i've searched over stackoverflow and other places and couldn't find an suitable answer.

Thanks.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Windows.Forms;

namespace ConsoleApplication2
{
    class Program
    {
        public void FileOpen(ref OpenFileDialog of, ref string filename, ref int nfiles)
        {
            of.Filter = "PPF Files | *.ppf";
            of.Multiselect = true;
            of.ShowDialog();
            filename = of.FileName;
            nfiles = of.FileNames.Count();
        }
        public void FileRead(ref OpenFileDialog of, ref string filename, ref List<string> freaded, ref int nfiles, ref string filecontents)
        {
            string aux;
            List<string> filesreaded = new List<string>();
            if (string.IsNullOrEmpty(of.FileName) == false)
            {
                for(int i=0; i<nfiles; i++)
                {
                    // Read the file into <bits>
                    using (var fs = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
                    {
                        var len = (int)fs.Length;
                        var bits = new byte[len];
                        fs.Read(bits, 0, len);
                        // Dump 16 bytes per line
                        for (int ix = 0; ix < len; ix += 16)
                        {
                            var cnt = Math.Min(16, len - ix);
                            var line = new byte[cnt];
                            Array.Copy(bits, ix, line, 0, cnt);
                            // Convert non - ascii characters to .
                        for (int jx = 0; jx < cnt; ++jx)
                            { 
                                if (line[jx] < 0x20 || line[jx] > 0x7f)
                                    line[jx] = (byte)'.';
                            }
                            aux = Encoding.ASCII.GetString(line);
                            filecontents += aux;
                        }



                    }//Closing FileStream
                }//Closing files loop (operations for each file)
            }//Closing IF Statement
        }//Closing FileRead function

        [STAThread]
        static void Main(string[] args)
        {
            Program a = new Program();
            OpenFileDialog openf = new OpenFileDialog();
            int filenumber = new int();
            string fullfilename //What to do here? 
//How can i instantiate a new string so i can use it when calling the method ?


            a.FileOpen(ref openf , ref fullfilename , ref filenumber);
        }
    }
}
Pablo Costa
  • 125
  • 4
  • 14
  • strings are a reference type http://stackoverflow.com/questions/10792603/how-are-strings-passed-in-net – M pollack Feb 23 '16 at 05:09
  • I was trying to pass the string by reference. But @Mpollack comment gave me a direction on what to do (perhaps pass the string by value instead of reference). I'm trying to separate the code in methods - and i'll need some variables from the other methods on the main program. I tought about creating a new separated class and instantiate variables from there - but i would like to at least try this solution first. – Pablo Costa Feb 23 '16 at 05:12
  • Strings are already reference types. **Do not pass them by reference**. You pass by reference when you wish to *modify a variable*, but you very rarely need to modify a variable. Instead of passing in a variable by reference to be modified, **return the string** as a return value. If you do wish for some reason to pass the variable by reference, **pass it as out, not ref**. – Eric Lippert Feb 23 '16 at 05:29
  • If i pass the string by value , in the method's call it'll show only the local string value(i initialize the string with "" , it'll show "" ). If i used ref and pass by reference it works. – Pablo Costa Feb 23 '16 at 05:32
  • 1
    You don't **read** the filename after you pass it by ref, which means that it should not be passed by ref. Refs are for variable aliases that must be both read and written. If you're only writing, pass it **out**, not **ref**. But better to not do it at all; do not mutate a variable, return a value. – Eric Lippert Feb 23 '16 at 05:33

0 Answers0