Solved

call c# object

Posted on 2010-08-26
17
306 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
Comment Utility
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
Comment Utility
Where is the calling-code being executed from? Another application?

Arun
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
 
LVL 27

Expert Comment

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

Arun
0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 3

Author Comment

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

Expert Comment

by:wdfdo1986
Comment Utility
public static class SpellCorrect
to
public class SpellCorrect
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
@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
Comment Utility
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
Comment Utility
no error at all, the program runs, but Im not getting any result
0
 
LVL 7

Expert Comment

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

Author Comment

by:mpvvee
Comment Utility
the class is in the first comment
0
 
LVL 7

Assisted Solution

by:wdfdo1986
wdfdo1986 earned 250 total points
Comment Utility
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
Comment Utility
solveed
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now