3

I have a list which is being populated from the database. The list is of this format:

class Event
{
    public int EventID { get; set; }
    public string EventName { get; set; }
    public string EventManager { get; set; }
    public int EventManagerID { get; set; }
    public string EventTicketID { get; set; }
}

class Manager
{
    public int ManagerID { get; set; }
    public string ManagerName { get; set; }
    public string ManagerShortName { get; set; }
    public bool IsActive { get; set; }
}

The EventTicketID is usually populated by an integer. But at times, the ticketID also has strings in it. This happens when an Event Manager sub-contracts to another management company in which case the EventTicketId is populated with the format ManagerShortName_TicketID. I want to know check if an event is contracted to an event manager or is contracted to a sub-manager. So I want to check if the EventTickedId is an integer or has strings in it.

I tried checking by using:

//EventList being populated from db in an instance of List<Event> called listEvents
int directContractTicketID = Convert.ToInt32(listEvent[0].EventTicketID);

But I get Unhandled exception. can someone tell me how I can check if if EventTicketId is string or integer?

4 Answers 4

9

Use int.TryParse it will return true if the value is parsable to int , otherwise false. If the return value is false, then you can safely assume that the string contains some non-numeric value.

int directContractTicketID;
if (int.TryParse(listEvent[0].EventTicketID, out directContractTicketID))
{
    // valid int value, parsed value in directContractTicketID
}
else
{
    //  listEvent[0].EventTicketID contains some non-numeric value
}
Sign up to request clarification or add additional context in comments.

2 Comments

I upvoted all answers as they all helped me. Your's and Bradley's answer offers exactly the same solution. I marked it as his was first as well as edited the question.
@JonathanDonell, you are free to accept any answer you think, but mine was first :P :)
4

Convert.ToInt32 actually calls int.Parse which will throw if the string is invalid (hence your unhandled exception).

Better would be to use int.TryParse:

int parsedTicket;
if (int.TryParse(listEvent[0].EventTicketID, out parsedTicket))
{
   //Value is held in parsedTicket
}
else
{
   //Ticket was not a number, parsedTicket is 0.
}

Comments

2

You have to understand the use of these functions.

Convert.toInt32 is used for conversion to integer type. Its not supposed to be used for checking if a value is integer or not.

Int.TryParse is a function specifically designed for that and that purpose only. To check if a given value is integer or not.

Similarly, there is a TryParse method for all built-in types in C#.

Hope this helps!!!

Comments

1

You can use the fact that numbers do not allow underscores to find out if this is a plain number of a ManagerShortName_TicketID:

bool isSubcontracted = ticketId.Contains("_");

If you want to parse the ticket identifier both for plain numbers and subcontracted items, you can do it like this:

int ticketNumber;
string subcontractor = null;
string[] tokens = ticketId.Split('_');
if (tokens.Length == 2) {
    subcontractor = tokens[0];
    ticketNumber = int.ParseInt(tokens[1]);
} else {
    ticketNumber = int.ParseInt(tokens[0]);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.