• Unfortunately, we have experienced significant hard drive damage that requires urgent maintenance and rebuilding. The forum will be a state of read only until we install our new drives and rebuild all the configurations needed. Please follow our Facebook pagefor updates, or we will be back up shortly! (The forum could go offline at any given time due to the nature of the failed drives whilst awaiting the upgrades.)

Arrays in C# help.

Custom Title Activated
Loyal Member
Joined
Mar 17, 2009
Messages
1,911
Reaction score
538
Hey guys, I'm starting to do C# again a bit, and I need some array help.

Heres my array:
string[] names = new string[5] { "Riku", "Riku1", "Riku2", "Riku3", "Riku4" };

Now I was wondering if I could do something like, if my computer-name equals one of those 5 names it does something, anyone have any ideas?

Thanks,
Riku
 
Junior Spellweaver
Joined
Dec 15, 2010
Messages
179
Reaction score
247
PHP:
            string[] arrStr = { "wew", "awt", "an array", "so scary", "but yet delicious!" };
            foreach (string szStr in arrStr)
            {
                if( szStr == SystemInformation.ComputerName )
                    // Do something
            }
something like that?
 
Custom Title Activated
Loyal Member
Joined
Mar 17, 2009
Messages
1,911
Reaction score
538
Yeah, but first it scans all of the 5 strings, then if one of them equal the computer name it will do something.
 
Custom Title Activated
Loyal Member
Joined
Aug 16, 2007
Messages
1,378
Reaction score
581
dude, a for each loop scanns the whole array

that code already has what you asked for
 
Custom Title Activated
Loyal Member
Joined
Mar 17, 2009
Messages
1,911
Reaction score
538
dude, a for each loop scanns the whole array

that code already has what you asked for

Well what i'm doing is, making it display a console message that 'User is allowed' or 'user is not allowed'

For me it does this:
User is allowed
user is not allowed
user is not allowed
user is not allowed
user is not allowed
 
Junior Spellweaver
Joined
Dec 15, 2010
Messages
179
Reaction score
247
well, what do you exactly want to do? break the for loop when the pc name matches?
PHP:
            string[] arrStr = { "wew", "awt", "an array", "so scary", "but yet delicious!" };
            foreach (string szStr in arrStr)
            {
                if( szStr == SystemInformation.ComputerName )
                {
                    Console.WriteLine( "The user is allowed" );
                    break;
                }
            }
or something like...
PHP:
string[] arrStr = { "wew", "awt", "an array", "so scary", "but yet delicious!" };
public bool bRegistered()
{
   foreach (string szStr in arrStr)
   {
      if( szStr == SystemInformation.ComputerName )
         return TRUE;
   }
   return FALSE;
}
Console.WriteLine( bRegistered() ? "The user is registered" : "The user is not registered" );
(I'm not sure if the one above works, and sorry for the hungarian notation, i usually use c++ :>)
 
Last edited:
Experienced Elementalist
Joined
Oct 1, 2007
Messages
210
Reaction score
26
I don't understand he is trying to do but I know the code KrYpT0n posted is looping through the entire array and then performs the check. I don't see how that's not what you are looking for. Please give us more insight on what you are trying to do.
 
Experienced Elementalist
Joined
Apr 12, 2009
Messages
241
Reaction score
32
It's simple

Code:
string[] names = new string[5] { "Riku", "Riku1", "Riku2", "Riku3", "Riku4" };

if (names.Any(x=> x == Environment.MachineName))
{
   Do Something;
}
 
Back
Top