C# array issue

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Daxthecon
    New Member
    • Jul 2008
    • 63

    C# array issue

    Code:
    foreach (SearchResult result in search.FindAll())
    		{
    
    			//pieces = result.Properties["samaccountname"][0].ToString().Split(new Char[] { '\'' });
    			//for (int i = 0; i < pieces.GetLength(0); i++)
    			//{
    				//name = name + pieces[i];
    			//}
    
    			manager.Items.Add(new ListItem(name, result.Properties["samaccountname"][0].ToString()));
    
    			name = "";
    This bit of code take the aduser samaccount and gets the number of characters and sets an absolute length for the drowdownlist(ma nager) to display the names. Well it has stopped working and I have no clue why.

    The errors are as follows: Cannot implicity convert type 'string[]' to 'string'
    'string' does not contain a definition for 'GetLength'

    Any help asap would be really nice.
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    Well, you didn't show us the definition of pieces, but I believe that pieces is delcared as a string, and you are trying to use it as an array.

    Make sure that pieces is defined as a string[].

    Also, I'm not sure why all your code is commented out.

    Comment

    • Daxthecon
      New Member
      • Jul 2008
      • 63

      #3
      Originally posted by insertAlias
      Well, you didn't show us the definition of pieces, but I believe that pieces is delcared as a string, and you are trying to use it as an array.

      Make sure that pieces is defined as a string[].

      Also, I'm not sure why all your code is commented out.
      The whole thing was crashing and I wanted to see if it still worked without this and it just displays a small drowdown list with no names but a scroll bar and space for names. Could you be more specific about pieces to be defined as string[]. This isn't my code it was the guys before me and I am clueless on c#.

      Also this is the only place pieves is used. Everything you see is everything you get.

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        It actually doesn't look like .NET?
        I've never used GetLength() before, always just used .Length

        string[] .Length will return the number of elements in the array (number of strings in the array)
        string .Length returns the number of characters in the string.

        Comment

        • Daxthecon
          New Member
          • Jul 2008
          • 63

          #5
          Originally posted by Plater
          It actually doesn't look like .NET?
          I've never used GetLength() before, always just used .Length

          string[] .Length will return the number of elements in the array (number of strings in the array)
          string .Length returns the number of characters in the string.
          When I used .Length and it needs more specific parameters in my opinion. Like I said C# is not my cup of tea and this is old .NET. This program is a few years old with hardcoded emails and such. Well one day after I loaded VS 2008 up and opened the IIS and edited one of the emails it was sending too(I am in the middle of writing all these programs correctly for future reference) and saved it and it didn't work. And ever since have had this issue. I need to get it done because people are unhappy that it's not working and I hadn't realized it wasn't working until just a few days ago.

          Comment

          • Curtis Rutland
            Recognized Expert Specialist
            • Apr 2008
            • 3264

            #6
            Did this use to work? or has it never worked?

            pieces has to be defined somewhere. Right click on it and select "go to definition" or something like that.

            @Plater:
            Maybe it's this method, but I've never seen it either.

            Comment

            • Daxthecon
              New Member
              • Jul 2008
              • 63

              #7
              I forgot it wasn't in my copied code. But it's still not all that helpful.


              Code:
              DirectoryEntry entry = new DirectoryEntry("LDAP://OU=DAXCON,DC=hq,DC=local", "username", "password");
              		DirectorySearcher search = new DirectorySearcher(entry);
              
              		search.Filter = "(&(objectclass=organizationalPerson) (mail=*daxcon.com)(!(useraccountcontrol=514)) (sn=*) (samaccountname=*) (!(extensionAttribute15=*)))";
              
              		search.PropertiesToLoad.Add("samaccountname");
              		search.PropertiesToLoad.Add("mail");
              		search.PropertiesToLoad.Add("name");
              		search.PropertiesToLoad.Add("sn");
              
              		search.Sort.PropertyName = "sn";
              
              		string pieces;
              		string name = "";
              
              		foreach (SearchResult result in search.FindAll())
              		{
              
              			pieces = result.Properties["samaccountname"][0].ToString().Split(new Char[] { '\'' });
              			for (int i = 0; i < pieces.GetLength(0); i++)
              			{
              				name = name + pieces[i];
              			}
              
              			manager.Items.Add(new ListItem(name, result.Properties["samaccountname"][0].ToString()));
              
              			name = "";
              
              		}
              
              		entry.Close();
              	}
              Trust me before the fanatics see the amount of bad this isn't mine and I am dedicating learning and redoing all of this code and AD paths and what not. With that being said that is the code and how it is displayed as of right now and as of when it worked for as much as I know.

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                Ok, this line:
                Code:
                string pieces;
                should be change to
                Code:
                string[] pieces;
                Since pieces is going to receive an array of strings from that split(). I believe that is what one of the error messages you are getting. Actually I think it will clear up both of them, since pieces will then be a string[], it will also contain the .GetLength() function.
                HOWEVER, based on the apparent logic in that function, I would change .GetLength() to .Length anyway.
                As GetLength(0) will return the number of characters in the first string contained in the pieces array, and not how many elements are in the array.

                Comment

                • Daxthecon
                  New Member
                  • Jul 2008
                  • 63

                  #9
                  Stack trace says
                  ; expected


                  I don't think I am missing a ; anywhere that I wasn't before.

                  Comment

                  • Plater
                    Recognized Expert Expert
                    • Apr 2007
                    • 7872

                    #10
                    If you were missing a in CODE code, doing abuild would throw up an error message in visual studio.
                    However, if you were missing a ; in some kind of query string or something, that might be harder to find.
                    Error messages should give you a line number?

                    Comment

                    • Daxthecon
                      New Member
                      • Jul 2008
                      • 63

                      #11
                      Line 111

                      Code:
                      for (int i = 0; i < pieces.Length(0); i++)
                      which is also giving me a Method name expected error.

                      Comment

                      • Plater
                        Recognized Expert Expert
                        • Apr 2007
                        • 7872

                        #12
                        Ahh, oopsy. I should have told you that Length is a property, not a function. It does not require any arguments.

                        I am trying to figure out what that bit of code is doing.
                        It looks like it's taking this:
                        "something/someotherthing/somename"
                        and making it be
                        "somethingsomeo therthingsomena me".

                        Seems like a lot of work that could have been handled with the .Replace() function.

                        Comment

                        • Daxthecon
                          New Member
                          • Jul 2008
                          • 63

                          #13
                          Originally posted by Plater
                          Ahh, oopsy. I should have told you that Length is a property, not a function. It does not require any arguments.

                          I am trying to figure out what that bit of code is doing.
                          It looks like it's taking this:
                          "something/someotherthing/somename"
                          and making it be
                          "somethingsomeo therthingsomena me".

                          Seems like a lot of work that could have been handled with the .Replace() function.
                          I got it working again and I have no idea how... I just did pieces. and GetLength was there... Now is there a way to do this without having to change a ton of things around like this code is doing.

                          Comment

                          • Plater
                            Recognized Expert Expert
                            • Apr 2007
                            • 7872

                            #14
                            Originally posted by Daxthecon
                            I got it working again and I have no idea how... I just did pieces. and GetLength was there... Now is there a way to do this without having to change a ton of things around like this code is doing.
                            [code=c#]
                            string[] pieces;
                            string name = "";

                            foreach (SearchResult result in search.FindAll( ))
                            {
                            name = result.Properti es["samaccountname "][0].ToString().Rep lace("\","");
                            manager.Items.A dd(new ListItem(name, result.Properti es["samaccountname "][0].ToString()));
                            }
                            [/code]
                            That is assuming you were splitting on the \ character.

                            Comment

                            • Daxthecon
                              New Member
                              • Jul 2008
                              • 63

                              #15
                              What about VB.net. Would this little aray be easier to make or am I barking up the wrong tree. I am new to AD for the most part and can't get a web server to display AD stuff in a dropdownlist. But this project after it broke exhibited the same behavior as the project I was working on and was stuck on.

                              Comment

                              Working...