Return empty string containing the number of characters in the length of string

hi ... i have a method which takes empid and returs his year of birth (YOB)- 4 characters...

if the employeeid does not exist it should return the empty string with 4 characters...
how is it possible...
right now it returns empty string- just one blank space..
gautam_reddycAsked:
Who is Participating?
 
zadeveloperConnect With a Mentor Commented:
string.Length will not work for you as if the string has not been initalised you will get null, if not populated you will get 0 etc
you will need to do something like this:
 Added Image of results and console application code to demo it.

using System;

public class ConsolApp
{
    [STAThread]
    static void Main()
    {
        var dataObject = new MyDataObject {Yob = "1981"};

        var s1 = dataObject.Yob;
        var s2 = dataObject.PhoneNumber;

        Console.WriteLine("YOB = [" + s1 + "] Length = " + s1.Length);
        Console.WriteLine("Phone Number = [" + s2 + "] Length = " + s2.Length);
        Console.ReadLine();
    }
}

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
public sealed class FieldAttribute : Attribute
{
    public string FieldName { get; private set; }
    public int FieldLength { get; set; }

    public FieldAttribute(string fieldName)
    {
        FieldName = fieldName;
    }
}

public class MyDataObject
{
    
    private string _Yob { get; set; }

    [FieldAttribute("YOB", FieldLength = 4)]
    public string Yob
    {
        get 
        {
            return string.IsNullOrEmpty(_Yob) ? new string(' ', GetFieldAttribute("YOB").FieldLength) : _Yob;
        }
        set
        {
            _Yob = value;
        }
    }

    
    private string _PhoneNumber { get; set; }

    [FieldAttribute("PhoneNumber", FieldLength = 10)]
    public string PhoneNumber
    {
        get
        {
            return string.IsNullOrEmpty(_PhoneNumber) ? new string(' ', GetFieldAttribute("PhoneNumber").FieldLength) : _PhoneNumber;
        }
        set
        {
            _PhoneNumber = value;
        }
    }

    protected FieldAttribute GetFieldAttribute(string fieldName)
    {
        foreach (var member in GetType().GetMember(fieldName))
        {
            var attributes = member.GetCustomAttributes(true);

            if (attributes == null || attributes.Length == 0) continue;

            foreach (FieldAttribute attribute in attributes)
            {
                if (attribute != null) return attribute;
            }
        }
        return null;
    }
}

Open in new window

results.jpg
0
 
p_davisCommented:
code please?
0
 
tsqlguyCommented:
if (YOB = "")
{
 return string newyob = "    ";
}
else
{
return YOB;
}

something like that should do if you absolutely need a string with four spaces.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
gautam_reddycAuthor Commented:
thanks tsqlguy..

 but it has to give the spaces randomly based on the length of the variable..

say if i want to send my phone number... it should retun the empty string with the ph no. lenght..

Thanks in advance.. Appreciate your help
0
 
magicdlfCommented:
return new string(' ', YOB.Length);
0
 
rajeeshmcaCommented:
can i know what is the need of giving spaces, instead of giving a space
0
 
zadeveloperCommented:
BTW Yoy will notice that i did not initalise Phone number
0
 
zadeveloperCommented:
Any feed back on this code that I sent you ?
0
 
gautam_reddycAuthor Commented:
Excellent... that worked... thank you...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.