Solved

call c# object

Posted on 2010-08-26
17
308 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
  • 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

775 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