Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello, here's the scenario, i have ID no. which is 2014-11-0001, im trying to convert this text to Int, so that i can i can increment it like 2014-11-0002, is there a way to convert DASH to an Int? Please help
Posted
Updated 11-Dec-15 18:33pm
v2
Comments
PIEBALDconsult 12-Dec-15 0:31am    
No. You're looking at it backward. The ID may be displayed that way, but it should never be stored and manipulated that way.
Go back and re-architect.
Rencyrence 12-Dec-15 0:43am    
What do you mean?

Looked at from a "different" point of view, I see this as asking the question: "how can I have a .NET object that will behave like this: it will have a counter that is sequential, and is unique for all instances of the object; it will embody a DateTime, and it will 'do the right thing' to produce a formatted string result of a certain type."

One way you can approach this is create a Class:
C#
using System;

namespace YourNameSpace
{
    public class DateWithCounter
    {
        public int ID { set; get; }
        public DateTime Date { set; get; }

        public DateWithCounter(DateTime theDate)
        {
            Date = theDate;
            ID = counter++;
        }

        // ToString override
        public override string ToString()
        {
            return string.Format("{0}{1}{2}{3}{4}", 
                Date.Year, 
                dash, 
                Date.Month.ToString().PadLeft(4,zero), 
                dash, 
                ID.ToString().PadLeft(4,zero));
        }

        // static elements

        private static char dash = '-';
        private static char zero = '0';
        private static int counter = 0;
    }
}
Okay, let's test the class:
C#
private void SomeButton_Click(object sender, EventArgs e)
{
    List<DateWithCounter> dtCounters = new List<DateWithCounter>();

    DateTime adate;

    for (int i = 1; i < 13; i++)
    {
       adate = new DateTime(2015,i, i*2);
       dtCounters.Add(new DateWithCounter(adate));

       // test string output
       Console.WriteLine(dc);
    }
}
Here's the output:
C#
2015-0001-0000
2015-0002-0001
2015-0003-0002
2015-0004-0003
2015-0005-0004
2015-0006-0005
2015-0007-0006
2015-0008-0007
2015-0009-0008
2015-0010-0009
2015-0011-0010
2015-0012-0011
I would hope that your take-away from this example is that C#/.NET offers you powerful tools to "design Objects/Types," as well as solve problems by clever coding.

I would also argue that developing the skill to design Types/Objects results in code that has a higher quality of "encapsulation."

However, I should disclaimer the above statement by saying that knowing when to take a particular coding problem and create a small utility/tool to use in that problem, and in other similar problems, is an equally valuable skill.

In this particular case, I don't see a need to create a tool/utility. Of course, it would be easy to create a generic class that had a static counter variable !
Quote:
Tell me and I forget. Teach me and I remember. Involve me and I learn.
Benjamin Franklin
 
Share this answer
 
v2
Comments
Rencyrence 12-Dec-15 2:11am    
Thank you!
No.
Dash is a string literal, and does not have numeric meaning, you need to customize the logic to accomplish your task
Split variable with - and then convert it to int and then do increment
see below snippet
C#
string szValue = "2014-11-0001";
int iCount = convert.toInt32(szValue.Split('-')[2]);
iCount = iCount + 1;
int val= iCount.ToString("D").Length + 3;
szValue =  "2014-11-" + convert.toString(val);

in above logic you can check of value '9999' and increment another value

Hope it helps
 
Share this answer
 
v2
Comments
ridoy 12-Dec-15 1:54am    
5ed.
Rencyrence 12-Dec-15 1:56am    
I think it works, but the output was 2014-11-2, i wanted to show 2014-11-0002, i need the zeros infront...
koolprasad2003 12-Dec-15 2:05am    
It is simple just use toString method, see below snippet
int value = 1;
int decimalLength = value.ToString("D").Length + 3;
I have update my solution accordingly
Rencyrence 12-Dec-15 2:11am    
Thank you!
PIEBALDconsult 12-Dec-15 10:44am    
Don't use Convert!
You are taking the problem from the wrong end.
You have to answer the question "How is built the reference ?"
In your case, the reference is build from a year, a month and a counter.
Normal app are keeping track of the components of this reference and only build the reference on request.

If your app don't keep track of the components, you have to split the reference in its components, then generate the next reference.

This look like HomeWork, so start to work, and come back here when you have a problem in your code.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900