Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. how to check typ in string

how to check typ in string

Scheduled Pinned Locked Moved C#
helptutorialquestion
9 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    stephan_007
    wrote on last edited by
    #1

    hy everyon! i do have a little problem. i do have a function which has a parameter input as string. the input could be a number (double) or a date (datetime). in this function i have to check, which input it contains. because depending on the input i have to do different things. so i have to realize an if-then statement like if (isdateformat) { // do something with date } if (isdoubleformat) { // do something with a double } when using the convert function then the result is of this type (but i guess, if it is a double, then it throws an exception). when casting, then it is always of this type (always true). could someone tell me please how to check the input of the string, if it is a date or a double? thanks. stephan.

    G D B G 4 Replies Last reply
    0
    • S stephan_007

      hy everyon! i do have a little problem. i do have a function which has a parameter input as string. the input could be a number (double) or a date (datetime). in this function i have to check, which input it contains. because depending on the input i have to do different things. so i have to realize an if-then statement like if (isdateformat) { // do something with date } if (isdoubleformat) { // do something with a double } when using the convert function then the result is of this type (but i guess, if it is a double, then it throws an exception). when casting, then it is always of this type (always true). could someone tell me please how to check the input of the string, if it is a date or a double? thanks. stephan.

      G Offline
      G Offline
      Gareth H
      wrote on last edited by
      #2

      stephan_007, You can use RegEx or DateTime.TryParse / Double.TryParse. Regards, Gareth.

      S 1 Reply Last reply
      0
      • S stephan_007

        hy everyon! i do have a little problem. i do have a function which has a parameter input as string. the input could be a number (double) or a date (datetime). in this function i have to check, which input it contains. because depending on the input i have to do different things. so i have to realize an if-then statement like if (isdateformat) { // do something with date } if (isdoubleformat) { // do something with a double } when using the convert function then the result is of this type (but i guess, if it is a double, then it throws an exception). when casting, then it is always of this type (always true). could someone tell me please how to check the input of the string, if it is a date or a double? thanks. stephan.

        D Offline
        D Offline
        dan sh
        wrote on last edited by
        #3

        Use DateTime.TryParse()

        My idea of ideal life : Eat, Sleep, Repeat

        1 Reply Last reply
        0
        • G Gareth H

          stephan_007, You can use RegEx or DateTime.TryParse / Double.TryParse. Regards, Gareth.

          S Offline
          S Offline
          stephan_007
          wrote on last edited by
          #4

          what does the statement look like? because DateTime dt; datetime.tryparse(input, dt); returns an error, it does not like the dt statement

          J 1 Reply Last reply
          0
          • S stephan_007

            hy everyon! i do have a little problem. i do have a function which has a parameter input as string. the input could be a number (double) or a date (datetime). in this function i have to check, which input it contains. because depending on the input i have to do different things. so i have to realize an if-then statement like if (isdateformat) { // do something with date } if (isdoubleformat) { // do something with a double } when using the convert function then the result is of this type (but i guess, if it is a double, then it throws an exception). when casting, then it is always of this type (always true). could someone tell me please how to check the input of the string, if it is a date or a double? thanks. stephan.

            B Offline
            B Offline
            Bhim Prakash Singh
            wrote on last edited by
            #5

            hi, you want to check data type of parameter. solution: string sf = "hello"; double d = 34.3; if (sf is string) MessageBox.Show("String"); else MessageBox.Show("SF not string"); if (sf is double) MessageBox.Show("double"); else MessageBox.Show("SF not double"); if (d is string) MessageBox.Show("string"); else MessageBox.Show("dnot string"); if (d is double) MessageBox.Show("double"); else MessageBox.Show("d not double"); :rose:

            1 Reply Last reply
            0
            • S stephan_007

              what does the statement look like? because DateTime dt; datetime.tryparse(input, dt); returns an error, it does not like the dt statement

              J Offline
              J Offline
              J4amieC
              wrote on last edited by
              #6

              stephan_007 wrote:

              returns an error, it does not like the dt statement

              Funnily enough, compilers don't have emotions. It neither "likes" nor "dislikes" an argument! Do you mean it throws an exception? do you think this exception might be usful to us in trying to answer your question? Have you tried the documentation for DateTime.TryParse? If you did you'd see the second parameter is defined as an out parameter. DateTime dt; datetime.tryparse(input, out dt);

              S 1 Reply Last reply
              0
              • J J4amieC

                stephan_007 wrote:

                returns an error, it does not like the dt statement

                Funnily enough, compilers don't have emotions. It neither "likes" nor "dislikes" an argument! Do you mean it throws an exception? do you think this exception might be usful to us in trying to answer your question? Have you tried the documentation for DateTime.TryParse? If you did you'd see the second parameter is defined as an out parameter. DateTime dt; datetime.tryparse(input, out dt);

                S Offline
                S Offline
                stephan_007
                wrote on last edited by
                #7

                ups stupid me :) yes you are right, now it works ;)

                1 Reply Last reply
                0
                • S stephan_007

                  hy everyon! i do have a little problem. i do have a function which has a parameter input as string. the input could be a number (double) or a date (datetime). in this function i have to check, which input it contains. because depending on the input i have to do different things. so i have to realize an if-then statement like if (isdateformat) { // do something with date } if (isdoubleformat) { // do something with a double } when using the convert function then the result is of this type (but i guess, if it is a double, then it throws an exception). when casting, then it is always of this type (always true). could someone tell me please how to check the input of the string, if it is a date or a double? thanks. stephan.

                  G Offline
                  G Offline
                  Guffa
                  wrote on last edited by
                  #8

                  Why is the function using a string as parameter? That means that you have to convert all values to strings, then parse them back to the values again. Make the parameter an object instead, then you can easily check the actual type of the value:

                  public int SomeFunction(object value) {
                  if (value is DateTime) {
                  DateTime d = (DateTime)value;
                  ...
                  } else if (value is double) {
                  double d = (double)value;
                  ...
                  } else {
                  throw new ArgumentException("Type "+value.GetType().Name+" is not accepted as parameter.");
                  }
                  }

                  Using an object parameter to accept value types means that the values will be boxed. Boxing is something that you generally want to avoid if possible, but in this case there is no common base type between DateTime and Double that you could use instead. Also, converting to a string and back is far worse than boxing.

                  Despite everything, the person most likely to be fooling you next is yourself.

                  modified on Friday, May 2, 2008 9:04 AM

                  P 1 Reply Last reply
                  0
                  • G Guffa

                    Why is the function using a string as parameter? That means that you have to convert all values to strings, then parse them back to the values again. Make the parameter an object instead, then you can easily check the actual type of the value:

                    public int SomeFunction(object value) {
                    if (value is DateTime) {
                    DateTime d = (DateTime)value;
                    ...
                    } else if (value is double) {
                    double d = (double)value;
                    ...
                    } else {
                    throw new ArgumentException("Type "+value.GetType().Name+" is not accepted as parameter.");
                    }
                    }

                    Using an object parameter to accept value types means that the values will be boxed. Boxing is something that you generally want to avoid if possible, but in this case there is no common base type between DateTime and Double that you could use instead. Also, converting to a string and back is far worse than boxing.

                    Despite everything, the person most likely to be fooling you next is yourself.

                    modified on Friday, May 2, 2008 9:04 AM

                    P Offline
                    P Offline
                    PIEBALDconsult
                    wrote on last edited by
                    #9

                    I think I asked him that same thing a few days ago.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups