Как вызвать метод из конструктора в C#
Пытаюсь написать код, который бы перегружал конструкторы вместе с методами и, в зависимости от типа поступающей переменной mass_str реализовывал бы необходимый мне метод, но не могу нигде найти, как вызвать нужный мне метод. Пока код выдает ошибку:
Ошибка CS1061 "Set" не содержит определения "fillset", и не удалось найти доступный метод расширения "fillset", принимающий тип "Set" в качестве первого аргумента (возможно, пропущена директива using или ссылка на сборку).
Сама переменная mass_str получает на вход из текстового файла цифры 0 1 2 3 4 5 6 7 8 9
Вот сам код:
class Set
{
public Set(string[] mass_str)
{
void fillset(string[] mass_str)
{
SortedSet<char> s1 = new SortedSet<char>();
char x;
for (int i = 0; i < mass_str.Length; i++)
{
x = Convert.ToChar(mass_str[i]);
s1.Add(x);
}
}
}
public Set(double[] mass_str)
{
void fillset(double[] mass_str)
{
SortedSet<double> s1 = new SortedSet<double>();
double x;
for (int i = 0; i < mass_str.Length; i++)
{
x = Convert.ToDouble(mass_str[i]);
s1.Add(x);
}
}
}
public Set(int[] mass_str)
{
void fillset(int[] mass_str)
{
SortedSet<int> s1 = new SortedSet<int>();
int x;
for (int i = 0; i < mass_str.Length; i++)
{
x = Convert.ToInt32(mass_str[i]);
s1.Add(x);
}
}
}
}
class Program
{
static void Main()
{
StreamReader sr = new StreamReader("..\\..\\..\\Заданное.txt");
string[] delimeters_0 = { " ", "," };
string[] mass_str = sr.ReadLine().Split(delimeters_0, StringSplitOptions.RemoveEmptyEntries);
Set newset1 = new Set(mass_str);
newset1.fillset()
}
}
Код с int[]:
class Set
{
public Set(string[] mass_str)
{
fillset(mass_str);
}
public Set(double[] mass_str)
{
fillset(mass_str);
}
public Set(int[] mass_str)
{
fillset(mass_str);
}
public void fillset(string[] mass_str)
{
SortedSet<char> s1 = new SortedSet<char>();
char x;
for (int i = 0; i < 9; i++)
{
x = Convert.ToChar(mass_str[i]);
s1.Add(x);
}
}
void fillset(double[] mass_str)
{
SortedSet<double> s1 = new SortedSet<double>();
double x;
for (int i = 0; i < 9; i++)
{
x = Convert.ToDouble(mass_str[i]);
s1.Add(x);
}
}
void fillset(int[] mass_str)
{
SortedSet<int> s1 = new SortedSet<int>();
int x;
for (int i = 0; i <9; i++)
{
x = Convert.ToInt32(mass_str[i]);
s1.Add(x);
}
}
}
class Program
{
static void Main()
{
StreamReader sr = new StreamReader("..\\..\\..\\Заданное.txt");
string[] delimeters_0 = { " ", "," };
string[] stringmass = sr.ReadLine().Split(delimeters_0, StringSplitOptions.RemoveEmptyEntries);
int[] mass_str = new int[9];
for (int i=0; i < 9; i++)
{
mass_str[i] = Convert.ToInt32(stringmass[i]);
}
Set newset1 = new Set(mass_str);
newset1.fillset(mass_str);
}
}
Ответы (1 шт):
Как минимум, это должно выглядеть так:
class Set
{
public Set(string[] mass_str)
{
fillset(mass_str);
}
public Set(double[] mass_str)
{
fillset(mass_str);
}
public Set(int[] mass_str)
{
fillset(mass_str);
}
void fillset(string[] mass_str)
{
SortedSet<char> s1 = new SortedSet<char>();
char x;
for (int i = 0; i < mass_str.Length; i++)
{
x = Convert.ToChar(mass_str[i]);
s1.Add(x);
}
}
void fillset(double[] mass_str)
{
SortedSet<double> s1 = new SortedSet<double>();
double x;
for (int i = 0; i < mass_str.Length; i++)
{
x = Convert.ToDouble(mass_str[i]);
s1.Add(x);
}
}
void fillset(int[] mass_str)
{
SortedSet<int> s1 = new SortedSet<int>();
int x;
for (int i = 0; i < mass_str.Length; i++)
{
x = Convert.ToInt32(mass_str[i]);
s1.Add(x);
}
}
}
А дальше ещё... Можно обобщённый класс сделать class Set<T> или обобщённый метод void fillset<T>(T[] mass_str).
Работает:
using System;
static class Programm {
class Set {
public void fillset(string[] mass_str) {}
public void fillset(int[] mass_str) {}
}
static void Main() {
var a1 = new Set(); a1.fillset(new string[] {"a", "b"});
var a2 = new Set(); a1.fillset(new int[] {1, 2});
}
}