2

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
    .
    .
    .
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Mariles
  • 83
  • 5
  • 2
    C# can do anything BASIC can. What do these commands in BASIC do? –  Dec 30 '20 at 20:50
  • 2
    @Amy Although you're correct, it's a bit funky to make a claim that C# can do anything that BASIC can, and then ask, in effect, "OK, but, ah, what does BASIC do here, anyway?" :) But I can see a pedagogical value of having the asker explain to themselves what does `DATA` actually do, since that may point to an equivalent C# solution. – Kuba hasn't forgotten Monica Dec 30 '20 at 20:53
  • 1
    You can create an array of strings - or a dictionary that maps error codes to messages. Or if they need to be localized, add them as a resources. – 001 Dec 30 '20 at 20:54
  • 8
    The way you program in C# is quite different than the way you would program in BASIC. You're better off learning C# without thinking about how it was done in BASIC. – Heretic Monkey Dec 30 '20 at 20:56
  • 1
    @Kubahasn'tforgottenMonica I get your point about my juxtaposed sentences, but I would be *shocked* if something computable in BASIC cannot be computed in C#. I was prompting them to describe those methods for us for the sake of completeness of the question. –  Dec 30 '20 at 21:16
  • @Amy We've clearly come a long way from when MS BASIC was something everyone who did anything on microcomputers pretty much knew by heart :) I certainly don't remember much of it myself, but recently I got my hands on a nice oddball retro i8085 development board and those Intel mnemonics sure took a day to get used to for this boy bred and raised on Z80 :) As silly as it may sound, the one thing I sorely miss in all retro programming environments is a scroll wheel, a scroll bar, and full screen editor, but at the same time those WordStar block mark anchors were nicer than holding Shift down... – Kuba hasn't forgotten Monica Dec 30 '20 at 21:31
  • 1
    I had a working knowledge of VB6 and wanted to transition to VB.NET. I found that to be so hard that I switched to C#, because you need to think about programming in a completely different way and the apparent (but largely non-existent) similarities between VB6 and VB.NET kept confusing me. Trying to shoehorn old paradigms into a language that is not suited for it may well hamper your learning. So I would go with @HereticMonkey's advice. – Rno Dec 30 '20 at 21:33
  • @Kubahasn'tforgottenMonica Hehe, I bet! :) I've been playing around with Ben Eater's custom computer project (google him), and learning homebrew assembly there. As for BASIC, I haven't touched it in maybe 22 years! :) Cheers. –  Dec 30 '20 at 21:34

2 Answers2

3

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}\nStack trace:\n{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.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
-3

Try something like this :

           object[][] data = new object[][] {
                new object[] {2, "A sintaxis error has been found"},
                new object[] {3, "Path not found"},
                new object[] {4, "There is no more data"},
                new object[] {5, "An Illegal function call has been found"},
                new object[] {6, "An overflow error has occurred"},
                new object[] {7, "Not enought memory!"},
                new object[] {8, "Undefined line number"},
                new object[] {9, "Index out of range"},
                new object[] {11, "Division by zero error"}
            };

            int en = 1234;
            int el = 12;
            foreach(object[] d in data)
            {
                if ((int)d[0] == en)
                {
                    Console.WriteLine("Error #{0} in line {1}", (string)d[1], el); 
                }
            }
jdweng
  • 33,250
  • 2
  • 15
  • 20