c#如何在其他方法中定义类对象

时间:2016-01-08 14:57:36

标签: c# class

在我的代码中,我有课程:

public class letterCount
        {
            public int count;
            public letter(int count) 
              {
               this.count = count;
              }
    }

然后我想做:

letterCount a = new letterCount(0);

在一个函数中,

a.count++;

在另一个,但它一直让我误以为:当前上下文中不存在名称'a'

这两个函数都是public void type 我能做什么? 我想在另一个函数中定义(或初始化?idk如何调用它,sry)的原因是,因为我将有40个类定义,所以我认为它会更清晰

1 个答案:

答案 0 :(得分:2)

private letterCount a = null;

你的方法,

a = new letterCount(0);
实际上是方法里面的

。 请注意,如果在第一个方法之前调用第二个方法,则会出现错误(因为未实例化)。为了避免这种情况(除非有特殊原因只在第一个方法被调用时才实例化),你可以做到

private letterCount a = new letterCount(0);

直接,两种方法。

修改

好的,然后在评论之后,我建议采用不同的方法,如下:

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication8
{
    class Program
    {


        static void Main(string[] args)
        {
            var a = letterCounter("The quick brown fox jumps over the lazy dog");
            Console.ReadKey();
        }

        private const string charsToBeCounted = "abcdefghikjlmnopqrstuvwxyz"; // ABCDEFGHIKJLMNOPQRSTUVWXYZ capital letters not needed converting the string to lowercase

        public static Dictionary<char, int> letterCounter(string word)
        {
            var ret = new Dictionary<char, int>();
            word = word.ToLower();
            foreach (char ch in word)
            {
                if (charsToBeCounted.Contains(ch))
                {
                    if (ret.ContainsKey(ch))
                        ret[ch]++;
                    else
                        ret[ch] = 1;
                }
            }
            return ret;
        }
    }
}

该函数返回一个字典,其中字母是键,计数器是值。不需要所有这些对象。

EDIT2:

要使用结果,请执行以下操作:

private void button1_Click(object sender, EventArgs e)
    {
        var d = letterCounter(textBox1.Text);
        textBox2.Text = string.Empty;
        foreach(char c in d.Keys)
        {
             textBox2.Text += d[c].ToString() + " ";
        }
    }

<强> EDIT3:

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication8
{
    class Program
    {


        static void Main(string[] args)
        {
            var a = letterCounter("The quick brown fox jumps over the lazy dog");
            Console.ReadKey();
        }

        public class letter
        {
            public int count;
            public string stringValue;
        }

        private const string charsToBeCounted = "abcdefghikjlmnopqrstuvwxyz"; // ABCDEFGHIKJLMNOPQRSTUVWXYZ capital letters not needed converting the string to lowercase

        public static Dictionary<char, letter> letterCounter(string word)
        {
            var ret = new Dictionary<char, letter>();
            word = word.ToLower();
            foreach (char ch in word)
            {
                if (charsToBeCounted.Contains(ch))
                {
                    letter l = null;
                    if (ret.ContainsKey(ch))
                        l = ret[ch];
                    else
                    {
                        l = new letter();
                        ret.Add(ch, l);
                    }
                    l.count++;
                    l.stringValue = "Any value you want";
                }
            }
            return ret;
        }
    }
}

然后您的按钮变为:

    private void button1_Click(object sender, EventArgs e)
    {
        var d = letterCounter(textBox1.Text);
        textBox2.Text = string.Empty;
        foreach(char c in d.Keys)
        {
             textBox2.Text += d[c].count.ToString() + " ";
        }
    }