0

I am currently trying to convert some vb into c# and in the vb code I have the following line:

If DateTime.TryParse(txtExpirationDate.Text, Nothing) = False Then 
_isValid = False

Which, with my extremely limited knowledge of vb, I think should read in c# as:

if (DateTime.TryParse(txtExpirationDate.Text, null) == false)
        _isValid = false;

However visual studio is telling me that I have invalid arguments:

The best overloaded method match for 'System.DateTime.TryParse(string, out System.DateTime)' has some invalid arguments

I was wondering if anyone would be willing to help me out. What am I doing wrong? Is this even possible in c# and if so how would I do it?

klashar
  • 2,519
  • 2
  • 28
  • 38
M_Griffiths
  • 547
  • 1
  • 7
  • 26
  • Check [Documentation](https://msdn.microsoft.com/en-us/library/system.datetime.tryparse(v=vs.110).aspx). It'll help you. – Rahul Singh Feb 15 '17 at 13:27
  • 1
    I had already and it was little to no help to me. The documentation is the first place I look when I run into troubles. But thank you for the suggestion, your contribution is appreciated – M_Griffiths Feb 15 '17 at 14:26

1 Answers1

4

VB allows you to use either a variable or an arbitrary expression as an out/ref argument. In the latter case, the returned value is ignored. C#, on the other hand, requires a variable to which the returned value can be written.

Before C# 7, there's no way to ignore an out parameter. Thus, you need to declare a variable for this purpose (even if you don't use its value):

DateTime expirationDate;
if (DateTime.TryParse(txtExpirationDate.Text, out expirationDate) == false)
    _isValid = false;
Community
  • 1
  • 1
Heinzi
  • 167,459
  • 57
  • 363
  • 519