[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2009-12-30
9
Medium Priority
?
259 Views
Last Modified: 2012-05-08
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..
0
Comment
Question by:gautam_reddyc
9 Comments
 
LVL 22

Expert Comment

by:p_davis
ID: 26146284
code please?
0
 
LVL 3

Expert Comment

by:tsqlguy
ID: 26146312
if (YOB = "")
{
 return string newyob = "    ";
}
else
{
return YOB;
}

something like that should do if you absolutely need a string with four spaces.
0
 

Author Comment

by:gautam_reddyc
ID: 26146449
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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 9

Expert Comment

by:magicdlf
ID: 26146529
return new string(' ', YOB.Length);
0
 
LVL 15

Expert Comment

by:rajeeshmca
ID: 26147169
can i know what is the need of giving spaces, instead of giving a space
0
 
LVL 13

Accepted Solution

by:
zadeveloper earned 2000 total points
ID: 26149621
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
 
LVL 13

Expert Comment

by:zadeveloper
ID: 26149629
BTW Yoy will notice that i did not initalise Phone number
0
 
LVL 13

Expert Comment

by:zadeveloper
ID: 26169842
Any feed back on this code that I sent you ?
0
 

Author Closing Comment

by:gautam_reddyc
ID: 31671245
Excellent... that worked... thank you...
0

Featured Post

Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question