3 Replies - 749 Views - Last Post: 21 January 2016 - 07:55 AM

#1 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1726
  • View blog
  • Posts: 5,704
  • Joined: 25-September 09

using var in a Regex Matches method instead of being explicit.

Posted 21 January 2016 - 06:37 AM

I'm putting this here instead of in the regular forum as it's more of trying to understand how others appear to be able to do this:
In the process of trying to become more comfortable programming in C#, I've come upon the following scenario quite a few times.

Take the following snippet of code to find specific date formats. This is what I found
		static void Main(string[] args)
		{
			string pattern = "[0-1]?[0-9]/[0-9]{2}/[0-9]{4}";
			string input = "Due Date: 01/26/2016 Date: 01/25/2016";

			foreach (var m in Regex.Matches(input, pattern))
			{
				Console.WriteLine("'{0}' found at index {1}.",
                                  m.Value, m.Index);
			}
		}


I understand var is just a base object being used since we don't always know exactly what the Matches would return. I see this type of code a lot and I understand its use. What I don't understand is the last line of code. I cannot compile this because m.Value and m.Index are not defined in 'object' I can change var to Capture and everything is fine but I'm just wondering if there is something I don't have set up to use var and not be explicit and still have access to .Value and .Index?

exact error:
'object' does not contain a definition for 'Value' and no extension method 'Value' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)

using System;
using System.Text;
using System.Text.RegularExpressions;


Is This A Good Question/Topic? 0
  • +

Replies To: using var in a Regex Matches method instead of being explicit.

#2 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6379
  • View blog
  • Posts: 25,770
  • Joined: 12-December 12

Re: using var in a Regex Matches method instead of being explicit.

Posted 21 January 2016 - 06:51 AM

This discussion is relevant:

To 'Var' Or Not To 'Var'?

Personally, I only use var where I consider it necessary or useful, typically with LINQ and anonymous types.

Sometimes I'll cheat and just use var temporarily, until I determine the correct type.

[You could use casts ((Capture)m).Value but this defeats the purpose.]
Was This Post Helpful? 0
  • +
  • -

#3 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5101
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: using var in a Regex Matches method instead of being explicit.

Posted 21 January 2016 - 07:47 AM

@Charlie,

https://msdn.microso...(v=vs.110).aspx
alternative link (tiny url) MatchCollection Class

That is what Regex.Matches returns. Notice it implements IEnumerable and ICollection, but not the generic versions of either. It is unfortunately a collection of objects rather than a typed collection. By using var, you're saying to treat them like objects. By specifying the type, you're performing a cast.

This post has been edited by andrewsw: 21 January 2016 - 08:04 AM
Reason for edit:: added tiny url

Was This Post Helpful? 1
  • +
  • -

#4 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1726
  • View blog
  • Posts: 5,704
  • Joined: 25-September 09

Re: using var in a Regex Matches method instead of being explicit.

Posted 21 January 2016 - 07:55 AM

EDIT: was in the process of typing this up before Curtis responded:

So the above code would never compile the way it is written? Is that because m is being declared inside the foreach and is maintaining its type to be 'object' instead of Match?

rewriting the above code like this allows me access to .Value and .Index

			string pattern = "[0-1]?[0-9]/[0-9]{2}/[0-9]{4}";
			string input = "Due Date: 01/26/2016 Date: 01/25/2016";

			var m = Regex.Match(input, pattern);

			while (m.Success)
			{
				Console.WriteLine("{0} - {1}", m.Value, m.Index);
				m = m.NextMatch();
			}



Ah, I just reread the var keyword description and I think I see why. It states:

Quote

Beginning in Visual C# 3.0, variables that are declared at method scope can have an implicit type var.
since this is being declared in the foreach it is not implying the type and remains Object which has no definition of .Value and .Index

So apparently the author of that snippet of code wouldn't have been able to compile it either.

Unless I'm still missing something.

This post has been edited by CharlieMay: 21 January 2016 - 07:56 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1