0

I hate arrays! I have no idea how to create them or use them :/

So I wonder if you could help me...

I've got a basic understanding of arrays in Small Basic which I'm learning at school at the moment but Small Basic really limits you on what you can do since it's such a high level language.

I'm currently making a conjugator for verbs in Spanish using strings and arrays..

Could you help and translate this Small Basic code into C#?

Here is the code:

irrVerbPreterit["ser"] = "ser"
irrVerbPreterit["ser"]["verb1stpreterit"] = "fui"
irrVerbPreterit["ser"]["verb2ndpreterit"] = "fuiste"
irrVerbPreterit["ser"]["verb3rdpreterit"] = "fue"
irrVerbPreterit["ser"]["verb4thpreterit"] = "fuimos"
irrVerbPreterit["ser"]["verb5thpreterit"] = "fuisteis"
irrVerbPreterit["ser"]["verb6thpreterit"] = "fueron"

Also how would I look to see if a verb was in an array? In Small Basic I have...

If(Array.ContainsIndex(irrVerbPresent, verb)) Then 

Would be great if you could help me do this!

Kind Regards, ~Ben

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
benboy847
  • 35
  • 4
  • 2
    What you want is dictionaries, not arrays. I suggest you read more about data structures. – Geeky Guy May 09 '13 at 20:49
  • 5
    not to be funny but if you `Hate arrays and don't know how to use them` then perhaps you are in the wrong field.. why not read up on Arrays and how to index their Ordinal values.. – MethodMan May 09 '13 at 20:50
  • 2
    @Renan Technically a dictionary is an associative array, which is a type of array, which is why the the code that the OP is using, which is clearly a string-indexed collection, is still referred to as an "array". That said, the OP most certainly does want to use a Dictionary, and he will confuse most C# programmers by calling such a structure an array. – Servy May 09 '13 at 20:54
  • understandable ..but in order to not discourage you from learning perhaps you should just state that you are not familiar with or need more material, refresher, examples,..etc opposed to being negative about things that we programmers will see and or face sooner or later when coding.. Happy Noobness :) – MethodMan May 09 '13 at 21:15
  • Here is a good link very simple to read and understand which you can try the examples on your own to learn. [C# Arrays](http://www.dotnetperls.com/array) – MethodMan May 09 '13 at 21:47
  • @benboy847, why didn't you just delete your question instead of replacing it with gibberish? – Peter Ritchie Mar 09 '14 at 17:31

4 Answers4

1

I would create a Verb Class to make things easy:

class Verb
{
    public string Infinitive;
    public string verb1stpreterit;
    public string verb2ndpreterit;
    public string verb3rdpreterit;
    .....

} //this class substitutes the array's second coordinate, making it a lot easier to understand the code.

Could use it like that:

Verb Ser = new Verb();
Ser.Infinitive = "ser";
Ser.verb1stpreterit = "fui";
....
Verb Estar = new Verb();
Estar.Infinitive = "estar";
Estar.verb1stpreterit = "estive";
....

Then have a dictionary:

Dictionary<string, Verb> Verbs = new Dictionary<string, Verb>();
Verbs.Add(Ser.Infinitive, Ser); //this is the substitute for the arrays first dimension.
Verbs.Add(Estar.Infinitive, Estar);

You get the verbs by their infinitive.
Daniel Möller
  • 84,878
  • 18
  • 192
  • 214
0

The C# name for what you're looking for is a Dictionary. I won't do the whole thing for you, but here's a quick sample of how to use a dictionary.

  Dictionary<string,string> dic = new Dictionary<string,string>();
  dic["verb1stpreterit"] = "fui";
  bool contains = dic.ContainsKey("verb1stpreterit");

For what you're doing, it looks like you'll want to put a dictionary in another dictionary.

redtuna
  • 4,586
  • 21
  • 35
0

Just an alternate approach for Small Basic experts to see how the requested line of code can be equally checked and implemented in c#

Small Basic

If(Array.ContainsIndex(irrVerbPresent, verb)) Then

C# (Assumption is that we have an string array in c# namely 'verbArray' and 'toSearch' variable being the element to search)

if (Array.Exists(verbArray, eachElement => eachElement == toSearch)) { //We have toSearch is available }

S.N
  • 4,910
  • 5
  • 31
  • 51
-1

Instead of arrays (if you don't like them) you can use List, as they are dynamic in size. You can also have a List containing a Dictionary for example

List<String> myList = new List<String>();
myList.Add("hello world");
Half_Baked
  • 340
  • 4
  • 18
  • How does this help the `OP` in regards to what they are looking for.. – MethodMan May 09 '13 at 20:54
  • @DJKRAZE If he hates arrays, he could use list / dictionary, or list with dictionary. The question is unclear – Half_Baked May 09 '13 at 21:21
  • I think that he does not hate arrays he just doesn't understand its usage, advantages, disadvantages, etc.. – MethodMan May 09 '13 at 21:27
  • @benboy847 Still, lern both Array and Lists. They are good complements to each other. Good for different things – Half_Baked May 09 '13 at 21:36
  • they are very simple to understand. first thing I think is to understand the term `Zero-Based` meaning if you had an array for example `string[] mystring = new string[4];` you count `0,1,2,3` that equals 4 elements the easiest way to learn is count on your fingers believe it or not..then it will all make sense http://msdn.microsoft.com/en-us/library/aa288453%28v=vs.71%29.aspx – MethodMan May 09 '13 at 21:41
  • `to access the element in the 4th position if it's Zero-Based` would be `mystring[3]` look at the `MSDN Link that I posted in previous comment section. I will find you an even easier page that will give you examples of lots more things as well – MethodMan May 09 '13 at 21:43
  • this is one of the simplest tutorials I have seen to date [Arrays and how they work](http://www.dotnetperls.com/array) – MethodMan May 09 '13 at 21:46