Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
227 views
in Technique[技术] by (71.8m points)

c# - Is there an equivalent to MS DOS BASIC instructions like data, restore and read?

I'm a bit new to C# and I'm trying to write code where I embed a table which data that never changes. Is there an equivalent to MS DOS BASIC instructions like data, restore and read? Please take a look at the block I pasted below...

    1170 '
    1180 ' Error trapping routine.
    1190 '
    1200 EN=ERR:EL=ERL:' error number:error line.
    1210 IF EN=53 AND EL=460 THEN RESUME 480
    1220 IF EN=53 THEN EXIST=0:RESUME 880
    1230 SCREEN 0:PRINT
    1240 RESTORE 1470:' Error data
    1250 FOR MYERR=1 TO 35
    1260 READ ERNUM,ERTYPE$
    1270 IF ERNUM=EN THEN 1290
    1280 NEXT
    1290 PRINT "Error #"EN"("ERTYPE$") in line"EL
    1300 PRINT:LIST 510-610
    1310 '
    1440 '
    1450 ' Error listing.
    1460 '
    1470 DATA 2,A sintaxis error has been found
    1480 DATA 3,Path not found
    1490 DATA 4,There is no more data
    1500 DATA 5,An Illegal function call has been found
    1510 DATA 6,An overflow error has occurred
    1520 DATA 7,Not enought memory!
    1530 DATA 8,Undefined line number
    1540 DATA 9,Index out of range
    1550 DATA 11,Division by zero error
    .
    .
    .

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The DATA statement defines a row in a "table" that you then iterate over: RESTORE sets the "iterator", and READ dereferences the iterator and advances it to the next item. This is, in essence, a key-value "dictionary", and you could use the C#'s Dictionary container:

using System;
using System.IO;
using System.Collections.Generic;
                    
public class Program
{
    Dictionary<int, string> errors = new Dictionary<int, string>() {
        [2] = "A syntax error has been found",
        [3] = "Path not found",
        [4] = "There is no more data" // etc.
    };
    

This is how you'd approach it if you actually needed such a dictionary. C# offers powerful error diagnostic capabilities, and you don't need to manually manage error message dictionaries. The errors are called exceptions, and the exceptions can be caught, and they convey lots more information than what you had in most BASICs.

Here's the closest equivalent code for error handling a failing file opening:

    void someMethod() {
        object stream;
        try {
            // Presumed equivalent of line 460
            stream = File.Open("path", FileMode.Open, FileAccess.Read);
            // Line 470 would be here
        } catch (FileNotFoundException) {} // if the file didn't open, we just continue
        // Line 480 onwards would be here
    }

And here's how you could handle arbitrary errors, providing not only their description, but also a stack trace showing all the methods active (i.e. entered but not exited) in the current thread when the exception was thrown:

    void doAndCatchErrors() {
        try {
            someMethod();
        } catch (Exception e) {
            Console.WriteLine("Error: {1}
Stack trace:
{2}", e.ToString(), e.StackTrace.ToString());
        }
    }
};

C# is a bit like Java in that it doesn't support free functions: all functions must be methods within a class. They may be static functions - and then they act exactly as free functions would, but this way you're forced to locate them in a non-global namespace. Global scope functions don't scale, because in large projects you'd be constantly fighting name collisions. Imagine a project with several thousand free functions - you get access to that many in fairly simple projects, and some of them indeed have duplicated names but each name is in a separate class, and thus they don't clash.

Now a more general note: it'd be extremely rare that a BASIC program would be structured like and use idioms similar to a good and readable C# program. Those are, after all, separate programming languages, and the most common, understandable and idiomatic way of expressing an idea in BASIC will most likely be extremely clumsy and appear alien in C#. Even programs that were "ported" from BASIC should not really resemble the original code when you re-express them in C#, because the vocabulary of both languages is quite different in scope: modern C# is way, way more expressive than any BASIC, and I'm not exaggerating here at all. The data structures and operations that had to be implemented from scratch in BASIC will likely be found in the framework you target your C# program to (that would be .Net Core framework for modern stuff) - so I expect that any BASIC program re-written in C# will be shorter, easier to maintain, easier to understand, and will perform potentially much better as well.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...