-1
  private XElement AuthorSeparate(List<string> authorName)
    {
        string surName = string.Empty;
        string initalName = string.Empty;
        string givenName = string.Empty;
        int j = 1;

        for (int i = 0; i < authorName.Count; i++)
        {
            XElement Author = new XElement("author");
            Author.Add(new XAttribute("Seq", j));
            else
            {
                char[] initalArray = splitedName[0].ToCharArray();
                initalName = initalArray[0] + '.'.ToString();
                surName = splitedName.LastOrDefault();
                splitedName = splitedName.Reverse().Skip(1).Reverse().ToArray();
                givenName = string.Join(" ", splitedName);
            }

            if (!string.IsNullOrEmpty(initalName))
            {
                XElement InitalElement = new XElement("initials", initalName);
                Author.Add(InitalElement);
            }

            if (!string.IsNullOrEmpty(surName))
            {
                XElement SurnameElement = new XElement("surname", surName);
                Author.Add(SurnameElement);
            }

            if (!string.IsNullOrEmpty(givenName))
            {
                XElement GivenNameElement = new XElement("given-name", givenName);
                Author.Add(GivenNameElement);
            }
        }
 return Author;
       }

This is my method.. Form this method i need to return xelement. in this method i declared xelement in for loop. after for loop completed i need to return that xelement named as author. how to return that xelement?

Malathi
  • 43
  • 7
  • You need to declare `Author` outside of the loop. put `XElement Author = new XElement("author");` before the for loop. – Chetan May 06 '17 at 04:16
  • but i will not create every time – Malathi May 06 '17 at 04:23
  • So if your loop has 10 iterations it will create 10 Author objects, which one you want to return ? – Chetan May 06 '17 at 04:25
  • What are you intending to do? From what you describe, you want to create a new Author each time through the loop, but then you want to return only one of those Authors at the end of the function? That's what you seem to be asking for, but I doubt that's what you really want. What are you trying to do? – RJM May 06 '17 at 04:26
  • Did it give an error for some reason? You know this is C#, right? Most objects/instances are in actuality references. So, you should be able to return a variable declared inside a loop. At any time inside the loop you just "return" it. The runtime does the reference counting and garbage collecting. – Joseph Willcoxson May 06 '17 at 04:28

2 Answers2

1

If you declare the variable in the loop, you can use it only inside the loop. So in your case, you should declare it outside of the loop because you can't put the return statement inside for loop.

Edit:

private ArrayList AuthorSeparate(List<string> authorName)
    {
        string surName = string.Empty;
        string initalName = string.Empty;
        string givenName = string.Empty;
        int j = 1;
        ArrayList authors = new ArrayList();
        for (int i = 0; i < authorName.Count; i++)
        {
            XElement Author = new XElement("author");
            Author.Add(new XAttribute("Seq", j));
            else
            {
                char[] initalArray = splitedName[0].ToCharArray();
                initalName = initalArray[0] + '.'.ToString();
                surName = splitedName.LastOrDefault();
                splitedName = splitedName.Reverse().Skip(1).Reverse().ToArray();
                givenName = string.Join(" ", splitedName);
            }

            if (!string.IsNullOrEmpty(initalName))
            {
                XElement InitalElement = new XElement("initials", initalName);
                Author.Add(InitalElement);
            }

            if (!string.IsNullOrEmpty(surName))
            {
                XElement SurnameElement = new XElement("surname", surName);
                Author.Add(SurnameElement);
            }

            if (!string.IsNullOrEmpty(givenName))
            {
                XElement GivenNameElement = new XElement("given-name", givenName);
                Author.Add(GivenNameElement);
            }
          authors.Add(Author)
        }
    return authors;
       }

If you want to create an XElement in each round, you can put them all in an ArrayList then return it.

osumatu
  • 410
  • 1
  • 8
  • 25
  • if i put outside of loop means it will not create every time when loop executed.. but i need to create every time.. – Malathi May 06 '17 at 04:22
  • So you want to create a new Author in every iteration and return all of them ? – Chetan May 06 '17 at 04:23
0

I believe instead of returning XElement from function you should return List of XElement.

So you can write something like this:

  private List<XElement> AuthorSeparate(List<string> authorName)
{
    string surName = string.Empty;
    string initalName = string.Empty;
    string givenName = string.Empty;
    int j = 1;
    var AuthorList = new List<XElement>();

    for (int i = 0; i < authorName.Count; i++)
    {
        XElement Author = new XElement("author");
        Author.Add(new XAttribute("Seq", j));
        else
        {
            char[] initalArray = splitedName[0].ToCharArray();
            initalName = initalArray[0] + '.'.ToString();
            surName = splitedName.LastOrDefault();
            splitedName = splitedName.Reverse().Skip(1).Reverse().ToArray();
            givenName = string.Join(" ", splitedName);
        }

        if (!string.IsNullOrEmpty(initalName))
        {
            XElement InitalElement = new XElement("initials", initalName);
            Author.Add(InitalElement);
        }

        if (!string.IsNullOrEmpty(surName))
        {
            XElement SurnameElement = new XElement("surname", surName);
            Author.Add(SurnameElement);
        }

        if (!string.IsNullOrEmpty(givenName))
        {
            XElement GivenNameElement = new XElement("given-name", givenName);
            Author.Add(GivenNameElement);
        }
        AuthorList.Add(Author);
    }
        return AuthorList;
}

It will be better to return the list in the way above because here you will get a new XElement for every iteration of the loop.

Abhishek Maurya
  • 321
  • 1
  • 3
  • 13