Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
i define string[] name=new string[100];
when i fill this variable,i don't now how i can access to last value???it's changed,sometime 78 sometime 50 sometime 60,...
how i can get last value???
Posted
Comments
Maciej Los 13-Sep-13 8:22am    
Why do you use static array instead dynamic?

 
Share this answer
 
Comments
ridoy 13-Sep-13 7:44am    
intersting,+5
Maciej Los 13-Sep-13 8:36am    
Agree!
+5!
Maybe that would do:
C#
public int LastNonEmptyIndex(string[] array)
{
   int i = array.Length - 1;
   do {
      if (!string.IsNullOrEmpty(array[i])) {
         break;
      }
   } while (--i > -1);
   return i;
}

This function returns the last index of an array containing a non emty/null value.

Hope this helps.
 
Share this answer
 
Comments
Maciej Los 13-Sep-13 8:43am    
Nice piece of code ;)
phil.o 13-Sep-13 8:58am    
Thank you :)
Edit : maybe I missed the static keyword in the method declaration that would make it even more useful ^^
First of all, please, read my comment to the question.

Solution 2 by Rakesh Meel is very good and i would suggest you to improve your knowledge by studying these articles:
Arrays Tutorial[^]
Arrays (C# Programming Guide)[^]
Code: Initializing an Array (Visual C#)[^]
 
Share this answer
 
C#
string[] dinosaurs = {"Compsognathus", "Amargasaurus", "Oviraptor",
            "Velociraptor",  "Deinonychus",  "Dilophosaurus",
            "Gallimimus",    "Triceratops" };
        //Predicate<string> dinoType=null;
        //string lastvalue = Array.FindLast(dinosaurs, dinoType).ToString();
        string lastItem = dinosaurs[dinosaurs.Length - 1];
 
Share this answer
 
v3
Comments
Member 8453437 13-Sep-13 7:38am    
it's not related to my question,
i don't now where is the last value???maybe50,60,70,89,....
phil.o 13-Sep-13 8:59am    
Plus, this has nothing to do at all with any generic method :)
[no name] 13-Sep-13 9:22am    
Plus plus it has nothing to do with the question asked.
try this


int i=name.length-1;

string last=name[i].ToString();
 
Share this answer
 
Comments
[no name] 13-Sep-13 9:22am    
Please read the question before answering.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900