Answer the question
In order to leave comments, you need to log in
C#: Why does the function receive an empty variable value?
Hello, I'm starting to learn OOP and the C # language itself, and therefore the error is likely to be stupid, and I may not express myself quite correctly. There is a Container class that performs a number of functions depending on the user's choice, but it takes the variable data from the Data class. The error is as follows: when the Adding function is executed in the Container class, the Description (class Data) variable comes empty: i.e. the value is assigned to it at the very beginning of the program execution, but then for some reason it is empty when the Adding function is executed. On another Main function, the variable arrives filled and correctly displays its contents. What is the problem? Thank you.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CsharpLABA2
{
class Data
{
public string Description;
public int Num;
public void Filling()
{
Console.OutputEncoding = Encoding.UTF8;
Console.WriteLine("Введите описание: ");
Description = Console.ReadLine();
}
public void Output()
{
Console.WriteLine("Ваш текст: " + Description);
}
public void Input()
{
Console.WriteLine("Для добавления строки введите 1 ");
Console.WriteLine("Для удаления строки введите 2 ");
Console.WriteLine("Для очистки текста введите 3 ");
Console.WriteLine("Для определения колличества символов введите 4 ");
Console.WriteLine("Для поиска заданного текста который возвращает колличество совпадений введите 5 ");
Console.WriteLine("Для замены одного символа на остальные введите 6 ");
Num = Convert.ToInt32(Console.ReadLine());
}
}
class Container
{
public void Adding ()
{
Data Dataclass = new Data();
Console.WriteLine(Dataclass.Description);
Console.WriteLine("Введите строку, которую вы хотите добавить ");
Console.WriteLine("Было: " + Dataclass.Description);
Dataclass.Description = String.Concat(Dataclass.Description, Console.ReadLine());
Console.WriteLine("Результат: " + Dataclass.Description);
Console.ReadKey();
}
static void Main(string[] args)
{
Container Classcontainer = new Container();
Data Dataclass = new Data();
Dataclass.Filling();
Dataclass.Output();
Dataclass.Input();
switch (Dataclass.Num)
{
case 1:
Classcontainer.Adding();
Dataclass.Output();
Dataclass.Input();
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
}
}
}
}
Answer the question
In order to leave comments, you need to log in
you call the class constructor in both methods Data
, however, the field is filled Description
only in one of the methods (when called Dataclass.Filling()
)
most likely, you need to parameterize the method Classcontainer.Adding()
and pass a reference to the previously created instance into it as a parameterData Dataclass
At the beginning of main, you filled in one instance of Data, then in the Adding function you created another one, which is no longer filled naturally, and you take data from it. You have two different copies.
In the Adding( ) method, a new Dataclass object is created with an empty Description field. It's easier for you to declare a Dataclass object (the name is not very good) in namespace.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question