• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 314
  • Last Modified:

call c# object

Hi

Im a c# newbie, I found this code in the internet, and Im trying to call it, but doesnt work,  please see below:

Call code:
       string[] strArray = new string[] { "reponse", "Ronnie", "Jack", "ome", "Max" };
        SpellCorrect.Main(strArray);

Open in new window


Class:
using System;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using StrEnum = System.Collections.Generic.IEnumerable<string>;

public static class SpellCorrect
{
    const string Alphabet = "abcdefghijklmnopqrstuvwxyz";

    public static StrEnum Edits1(string w)
    {
        // Deletion
        return (from i in Enumerable.Range(0, w.Length)
                select w.Substring(0, i) + w.Substring(i + 1))
            // Transposition
         .Union(from i in Enumerable.Range(0, w.Length - 1)
                select w.Substring(0, i) + w.Substring(i + 1, 1) +
                       w.Substring(i, 1) + w.Substring(i + 2))
            // Alteration
         .Union(from i in Enumerable.Range(0, w.Length)
                from c in Alphabet
                select w.Substring(0, i) + c + w.Substring(i + 1))
            // Insertion
         .Union(from i in Enumerable.Range(0, w.Length + 1)
                from c in Alphabet
                select w.Substring(0, i) + c + w.Substring(i));
    }

    public static void Main(string[] args)
    {
        var nWords = (from Match m in Regex.Matches(File.ReadAllText("/big.txt").ToLower(), "[a-z]+")
                      group m.Value by m.Value)
                     .ToDictionary(gr => gr.Key, gr => gr.Count());

        Func<StrEnum, StrEnum> nullIfEmpty = c => c.Count() == 0 ? null : c;

        var candidates =
            nullIfEmpty(new[] { args[0] }.Where(nWords.ContainsKey))
            ?? nullIfEmpty(Edits1(args[0]).Where(nWords.ContainsKey))
            ?? nullIfEmpty((from e1 in Edits1(args[0])
                            from e2 in Edits1(e1)
                            where nWords.ContainsKey(e2)
                            select e2).Distinct());

        Console.WriteLine(
            candidates == null
                ? args[0]
                : (from cand in candidates
                   orderby (nWords.ContainsKey(cand) ? nWords[cand] : 1) descending
                   select cand).First());
    }
}

Open in new window


From what I understood that code must print the result.

Any advice would be helpful.

Thanks
0
mpvvee
Asked:
mpvvee
  • 6
  • 5
  • 3
  • +1
2 Solutions
 
nmarunCommented:
You're a newbie and LINQ and Lambda expression are your first step towards C# coding? You should try something simpler. I'll post back, trying to answer your question in a few.

Arun
0
 
nmarunCommented:
Where is the calling-code being executed from? Another application?

Arun
0
 
käµfm³d 👽Commented:
I agree... where are you calling this from? Static Main() is an entry point to run your application standalone. You can add a reference to any .NET application or DLL and call its methods (even Main). I assume that's what you did here. Please expand upon where you are calling the method from and what you mean by "but doesnt work".
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
nmarunCommented:
Just to add one more question to kaufmed's list... what is it that you're trying to achieve?

Arun
0
 
käµfm³d 👽Commented:
I do see that you are calling ContainsKey() in your Where conditions in lines 39 and 40. ContainsKey() takes a string parameter which refers to a string that will be sought of the Keys collection for your dictionary.
0
 
mpvveeAuthor Commented:
hi,

thanks to all for the responses, that code expect a string and gives a result of possible corrections ( the code is for spell checking ), so I got that code as is, without instructions of how to call it, so I added it in my asp.net app inside the app_code folder and I tried to call it from an aspx page, with a button. When I run the app and click the button everything runs fine, but Im not getting any result.

Thanks in advance.
0
 
wdfdo1986Commented:
do u really want a static class here
u cant remove the static key word from the class definition then this will work
if u want to keep it static then search on how u can call methods in static classes.
0
 
wdfdo1986Commented:
sorry correction
its not "u cant remove the static key word from the class definition then this will work"
its "u can remove the static key word from the class definition then this will work"
0
 
mpvveeAuthor Commented:
could you elaborate a bit? Im a newbe in c#, where I need to make the change?
0
 
wdfdo1986Commented:
public static class SpellCorrect
to
public class SpellCorrect
0
 
käµfm³d 👽Commented:
@wdfdo1986

The use of "static" here has no bearing on whether or not the code will work--only how you call it. By the first code window of the OP, the function is being called correctly:  ClassName.StaticMethod().
0
 
wdfdo1986Commented:
ok can u tell me what's happening
is it a compile time error?
So what does it says
Can u copy the error to a post
0
 
mpvveeAuthor Commented:
no error at all, the program runs, but Im not getting any result
0
 
wdfdo1986Commented:
ok then i need to see the whole class thats using those first two lines of code
0
 
mpvveeAuthor Commented:
the class is in the first comment
0
 
wdfdo1986Commented:
ok explain the output you had.
Is it a blank console?
Or dont u see a console at all
in that case u can try this line at the end of the method.
Console.ReadLine();
0
 
mpvveeAuthor Commented:
solveed
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 6
  • 5
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now