null for int value

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Barry400

    null for int value

    Hi

    i want to set the following
    int xxx = null; // this is not allowed

    i want to pass a null value to a method like this

    setvalue("Hello world ", null); // to the following, it does not work,

    private void setvalue(string s1, int iVal)
    {

    }

    Barry


  • BarryM

    #2
    Re: null for int value

    "Barry400" <someone@somewh ere.comwrote in message
    news:OjDTOkliHH A.4132@TK2MSFTN GP05.phx.gbl...
    Hi
    >
    i want to set the following
    int xxx = null; // this is not allowed
    >
    i want to pass a null value to a method like this
    >
    setvalue("Hello world ", null); // to the following, it does not work,
    >
    private void setvalue(string s1, int iVal)
    Hi Barry,

    you can't assign null to a "value" type such as int. You need to use the
    nullable version, ie:
    int? xxx = null;

    lookup "nullable types" in the help file. ... I am not sure, but I think
    that you would need to be on .Net v2.

    Barry M



    Comment

    • =?ISO-8859-1?Q?G=F6ran_Andersson?=

      #3
      Re: null for int value

      Barry400 wrote:
      Hi
      >
      i want to set the following
      int xxx = null; // this is not allowed
      >
      i want to pass a null value to a method like this
      >
      setvalue("Hello world ", null); // to the following, it does not work,
      >
      private void setvalue(string s1, int iVal)
      {
      >
      }
      >
      Barry
      >
      In C# 2.0 you can use a nullable int:

      private void setvalue(string s1, int? iVal)

      You can pass either an integer value or null as the second argument.

      In the method you use iVal.HasValue to determine if the nullable
      variable has a value, and iVal.Value to get the value.

      --
      Göran Andersson
      _____
      Göran Anderssons privata hemsida.

      Comment

      Working...