Solved

call c# object

Posted on 2010-08-26
17
311 Views
Last Modified: 2013-12-17
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
Comment
Question by:mpvvee
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 3
  • +1
17 Comments
 
LVL 27

Expert Comment

by:nmarun
ID: 33538899
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
 
LVL 27

Expert Comment

by:nmarun
ID: 33538915
Where is the calling-code being executed from? Another application?

Arun
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 33538927
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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
LVL 27

Expert Comment

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

Arun
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 250 total points
ID: 33538950
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
 
LVL 3

Author Comment

by:mpvvee
ID: 33543456
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
 
LVL 7

Expert Comment

by:wdfdo1986
ID: 33553565
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
 
LVL 7

Expert Comment

by:wdfdo1986
ID: 33553573
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
 
LVL 3

Author Comment

by:mpvvee
ID: 33553721
could you elaborate a bit? Im a newbe in c#, where I need to make the change?
0
 
LVL 7

Expert Comment

by:wdfdo1986
ID: 33555326
public static class SpellCorrect
to
public class SpellCorrect
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 33556321
@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
 
LVL 7

Expert Comment

by:wdfdo1986
ID: 33556595
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
 
LVL 3

Author Comment

by:mpvvee
ID: 33558282
no error at all, the program runs, but Im not getting any result
0
 
LVL 7

Expert Comment

by:wdfdo1986
ID: 33561932
ok then i need to see the whole class thats using those first two lines of code
0
 
LVL 3

Author Comment

by:mpvvee
ID: 33573847
the class is in the first comment
0
 
LVL 7

Assisted Solution

by:wdfdo1986
wdfdo1986 earned 250 total points
ID: 33576955
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
 
LVL 3

Author Closing Comment

by:mpvvee
ID: 33780324
solveed
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question