Home > OS >  How to access and call a variable in a static method to use in another Class in C#?
How to access and call a variable in a static method to use in another Class in C#?

Time:06-17

I have a static method which splits a person's name into 3 variables. FirstName, MiddleName and LastName. How do I access the firstname or middleName or LastName variables in another class.

Code:

public static string SplitFullName(string Full_Name)
    {
        string[] names = Full_Name.Split(' ');
        string FirstName = "";
        string MiddleName = "";
        string LastName = "";
        if (names.Length == 0)
        {
            FirstName = "";
            MiddleName = "";
            LastName = "";
        }
        else if (names.Length == 1)
        {
            FirstName = names[0];
        }
        else if (names.Length == 2)
        {
            FirstName = names[0];
            LastName = names[1];
        }
        else
        {
            FirstName = names[0];
        }
        for (int i = 1; i < names.Length - 1; i  )
        {
            MiddleName  = " "   names[i];
            continue;
        }
        LastName = names[names.Length - 1];
        Console.WriteLine("The first name is: "   FirstName   ";   The middle name is: "   MiddleName   ";    The last name is: "   LastName);
        return Full_Name;
    }
}

CodePudding user response:

Usually you would pass them out directly to the caller when they call the method. You could, for example, do something like:

public static void ParseFullName(string fullName,
    out firstName, out middleName, out lastName)
{
    // (remove FirstName/MiddleName/LastName declarations)
    firstName = middleName = lastName = "";
    // populate firstName, middleName, lastName etc instead of
    // your current FirstName etc locals
}
// ... and usage
YourType.ParseFullName(someValue, out var first, out var middle, out var last);
// now use first/middle/last

or

public static (string FirstName, string MiddleName, string LastName)
    ParseFullName(string fullName)
{
    // ...
    return (FirstName, MiddleName, LastName);
}
// ... and usage
var (first, middle, last) = YourType.ParseFullName(someValue);
// now use first/middle/last

CodePudding user response:

For now, your code is returning what it's reading, i.e. the Full_Name itself.

If you can modify the code to return a Name object that contains 3 properties, namely FirstName, MiddleName and LastName, that will allow you to access any of those properties from the caller method.

Like this:

//Create this type
public class Name{
  public string FirstName{get;set;}
  public string MiddleName{get;set;}
  public string LastName{get;set;}
}

//This is your method    
public static SplitFullName(string Full_Name)
{
   //Split names here
   ...
   
   //Return the Name object like this:
   var name = new Name{
     FirstName = firstName,
     MiddleName = middleName,
     LastName = lastName
   };
}

//This is where you call from - may be the Main().
CallerMethod(){
  var resultName = SplitFullName("Abc K Xyz");
  Console.WriteLine("Middle name is: "   resultName.MiddleName);
}

CodePudding user response:

I recomended you encapsulate this into a User class like this:

[DebuggerDisplay("User {FirstName},{MiddleName},{LastName}")]
public class User
{
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }

    public User()
    {
    }

    public User(string fullName)
    {
        this.SetFullName(fullName);
    }

    public void SetFullName(string fullName)
    {
        if (string.IsNullOrEmpty(fullName))
        {
            this.FirstName = this.MiddleName = this.LastName = string.Empty;
            return;
        }

        var index1 = fullName.IndexOf(' ');
        if (index1 < 0)
        {
            // No spaces
            this.FirstName = fullName;
            this.MiddleName = this.LastName = string.Empty;
            return;
        }

        var index2 = fullName.LastIndexOf(' ');
        if (index2 < 0 || index1 == index2)
        {
            // Only one space
            this.FirstName = fullName.Substring(0, index1);
            this.MiddleName = string.Empty;
            this.LastName = fullName.Substring(index1   1);
        }
        else
        {
            // At least 2 spaces
            this.FirstName = fullName.Substring(0, index1);
            this.MiddleName = fullName.Substring(index1   1, index2 - index1 - 1);
            this.LastName = fullName.Substring(index2   1);
        }
    }
}

In this way, you only need create an User and set the fullName. Then, you can access to each part of name with the user's properties.

My code for SetFullName maybe appear more complicated than your Split option but is more efficient because don't create the splitted array of strings. I only look for first and last space and get 1, 2 or 3 parts for the properties. Is more code, but for the machine is less effort.

You may check in this way:

var u1 = new User("");
var u2 = new User("Name");
var u3 = new User("Name Last");
var u4 = new User("Name Middle Last");
var u5 = new User("Name Mi dd le Last");

CodePudding user response:

Or you could pass out an object

public class ParseFullNameResult{
     public string firstName = "";
     public string middleName = "";
     public string lastName = "";
}

public static ParseFullNameResult ParseFullName(string fullName){
    ParseFullNameResult result = new ParseFullNameResult();
    // do your thing
    return result;
}
  •  Tags:  
  • c#
  • Related