2

If you guys decide to put this code into your complier, you will notice that I get a Implicitly-typed local variables must be initlialized error on the var match. But i'm not too sure about how to go about doing this check (for text.count()) any other way. Sometimes my OCR will miss a letter or something and if it does, the number of characters will be less and that will make the match variable return a null...leaving me without a result.

Also, for bonus points, anyone that can help clean up my REGEX expression that'd be nice..i just started using it recently and i'm not tooo skilled in the semantics yet.

var match;
if (wordList[i].Text.Count() < 12)
{
    match = Regex.Match(wordList[i].Text, @"c[:;i]ty[!l1;:i/][5s][tuj!l/]z[i;:l]p[c1:;zs]", RegexOptions.IgnoreCase);
}
else
{
    match = Regex.Match(wordList[i].Text, @"c[:;i]ty[!l1;:i/][5s][tuj!l/][f!l1j;:i/]z[i;:l]p[c1:;zs]", RegexOptions.IgnoreCase);
}
if (match.Success)
{ ... }

5 Answers 5

3

You can't declare a var without initializing it. Use

Match match;

Or if you really must,

dynamic match;//I don't recommend this

Var is an implicitly typed variable, you must give it the value for it to infer the actual type. See here.

Sign up to request clarification or add additional context in comments.

4 Comments

well I understand that. But doing var match = null; doesn't work. I know there has to be a way to do this.
@MaylorTaylor by doing var match = null you're still not telling the compiler what type match is.
You can't give a variable of type var a null type. Declare match as type Match. Edit: Yuriy beat me D:
@MaylorTaylor There is a way to do it and it's to tell the compiler what type match is by doing Match match; as this answer says.
0

Please checkout the doc.

Beginning in Visual C# 3.0, variables that are declared at method scope can have an implicit type var. An implicitly typed local variable is strongly typed just as if you had declared the type yourself, but the compiler determines the type.

When you declare a variable, you need to use explicitly typed way, or do

var match = new Match(); 

Comments

0

You have to give the compiler something so it can infer what the type is. For example this will work:

var match = (Match)null;

Because the compiler knows var is supposed to be a Match.

Why you would want to do this over Match match; is beyond me, but it does work.

Comments

0

Without looking at your regex patterns, I'll suggest that you could break your code down by separating the "pattern decision logic" from the rest of your code.

Below I've provided 2 separate examples for how to break this down. The first defines a DeterminePattern method that just returns which string to use as the regex pattern. The second defines a FindMatch method that returns the appropriate Match object.

Technically, either of these allows you to continue to use the var keyword, but in my mind that's not really that important...the benefit here, as I see it, is making the code simpler to read and understand. You can decide for yourself whether breaking the code down like this achieves that.

Example 1

private static string DeterminePattern(string input)
{
    if (input.Count() < 12)
    {
        return @"...";
    }

    return @"...";
}


var match = Regex.Match(wordList[i].Text, DeterminePattern(wordList[i].Text), RegexOptions.IgnoreCase);
if (match.Success)
{ ... }

Example 2

private static Match FindMatch(string input)
{
    if (input.Count() < 12)
    {
        return Regex.Match(input, @"...", RegexOptions.IgnoreCase);
    }

    return Regex.Match(input, @"...", RegexOptions.IgnoreCase);
}

var match = FindMatch(wordList[i].Text);
if (match.Success)
{ ... }

Comments

0

Keep it DRY: Don't Repeat Yourself.

If you must use var (and even if you don't: what's the point of using var in this context especially?), this construct is better:

string text = wordList[i].Text ;
string pattern = text.Length < 12
               ? @"c[:;i]ty[!l1;:i/][5s][tuj!l/]z[i;:l]p[c1:;zs]"
               : @"c[:;i]ty[!l1;:i/][5s][tuj!l/][f!l1j;:i/]z[i;:l]p[c1:;zs]"
               ;
Match match = Regex.Match( text , pattern , RegexOptions.IgnoreCase ) ;

Or, better yet:

string text  = wordList[i].Text ;
Regex  rx    = text.Length < 12 ? Regex1 : Regex2 ;
Match  match = rx.Match( text , pattern , RegexOptions.IgnoreCase ) ;
.
.
.
private static readonly Regex1 = new Regex( @"c[:;i]ty[!l1;:i/][5s][tuj!l/]z[i;:l]p[c1:;zs]" , RegexOptions.IgnoreCase ) ;
private static readonly Regex2 = new Regex( @"c[:;i]ty[!l1;:i/][5s][tuj!l/][f!l1j;:i/]z[i;:l]p[c1:;zs]" , RegexOptions.IgnoreCase ) ;

And why on earth are you using Linq (Wordlist[i].Text.Count()) to get the length of a string? Strings already know length. There's no need to iterate over it to count characters: just ask the string how long it is by querying its Length property.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.