[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

call c# object

Posted on 2010-08-26
17
Medium Priority
?
313 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 1000 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 1000 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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

656 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