Jonathan Donell
Jonathan Donell

Reputation: 83

Check if value is string or only integer?

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?

Upvotes: 3

Views: 18621

Answers (4)

Satwik Nadkarny
Satwik Nadkarny

Reputation: 5135

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!!!

Upvotes: 2

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

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]);
}

Upvotes: 1

BradleyDotNET
BradleyDotNET

Reputation: 61339

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.
}

Upvotes: 4

Habib
Habib

Reputation: 223207

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
}

Upvotes: 9

Related Questions