Разбил код на классы, и один класс не находит другой - C#
Формулировка задачи:
когда в одном файле все работает
а когда в разных то
article
Material
и мэйн
почемуто articlle не может найти Material класс когда объявляю его полем
строка
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace пример_2ого
- {
- class Material
- {
- private string name;
- private double d;
- public Material() { }
- public Material(string newName, double newV)
- {
- D = newV;
- Name = newName;
- }
- public string Name
- {
- get
- {
- return name;
- }
- set
- {
- name = value;
- }
- }
- public double D
- {
- get
- {
- return d;
- }
- set
- {
- d = value;
- }
- }
- public override string ToString()
- {
- return string.Format("{0};{1}", Name, D);
- }
- }
- class Article
- {
- private Material material;
- private double vol;
- private string name;
- public Article() { }
- public Article(string articleName, double articleVolume, Material articleMaterial)
- {
- Mat = articleMaterial;
- Name = articleName;
- Vol = articleVolume;
- }
- public double Vol
- {
- get
- {
- return vol;
- }
- set
- {
- vol = value;
- }
- }
- public string Name
- {
- get
- {
- return name;
- }
- set
- {
- name = value;
- }
- }
- public Material Mat
- {
- get
- {
- return material;
- }
- set
- {
- material = value;
- }
- }
- public double GetMass()
- {
- return material.D * Vol;
- }
- public override string ToString()
- {
- return string.Format("{0};{1};{2};{3}", Name, Mat.ToString(), Vol, this.GetMass());
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Article art = new Article("Wire", 0.03, new Material("Steel", 7850));
- Console.WriteLine(art.ToString());
- Material cu = new Material("Cuprum", 8500);
- art.Mat=cu;
- //art.Mat.Name = "Cuprum";
- // art.Mat.D = 8500;// можно и так
- Console.WriteLine(art.ToString());
- Console.ReadKey();
- }
- }
- }
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace пример_2ого
- {
- class Article
- {
- private Material material;
- private double vol;
- private string name;
- public Article() { }
- public Article(string articleName, double articleVolume, Material articleMaterial)
- {
- Mat = articleMaterial;
- Name = articleName;
- Vol = articleVolume;
- }
- public double Vol
- {
- get
- {
- return vol;
- }
- set
- {
- vol = value;
- }
- }
- public string Name
- {
- get
- {
- return name;
- }
- set
- {
- name = value;
- }
- }
- public Material Mat
- {
- get
- {
- return material;
- }
- set
- {
- material = value;
- }
- }
- public double GetMass()
- {
- return (material.D * Vol);
- }
- public override string ToString()
- {
- return string.Format("{0};{1};{2};{3}", Name, Mat.ToString(), Vol, this.GetMass());
- }
- }
- }
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace пример_2ого
- {
- class Material
- {
- private string name;
- private double d;
- public Material() { }
- public Material(string newName, double newV)
- {
- D = newV;
- Name = newName;
- }
- public string Name
- {
- get
- {
- return name;
- }
- set
- {
- name = value;
- }
- }
- public double D
- {
- get
- {
- return d;
- }
- set
- {
- d = value;
- }
- }
- public override string ToString()
- {
- return string.Format("{0};{1}", Name, D);
- }
- }
- }
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace пример_2ого
- {
- class Program
- {
- static void Main(string[] args)
- {
- Article art = new Article("Wire", 0.03, new Material("Steel", 7850));
- Console.WriteLine(art.ToString());
- Material cu = new Material("Cuprum", 8500);
- art.Mat=cu;
- //art.Mat.Name = "Cuprum";
- // art.Mat.D = 8500;// можно и так
- Console.WriteLine("Новая масса провода = ", art.GetMass());
- Console.ReadKey();
- }
- }
- }
вот эту строку в одном варианте Material подсвечивает синим а в другом нет
private Material material;
private Material material;
Решение задачи: «Разбил код на классы, и один класс не находит другой»
textual
Листинг программы
- namespace пример_2ого
- {
- class Material
- {
- private string name {get;set;}
- private double d { get; set; }
- public Material() { }
- public Material(string newName, double newV)
- {
- d = newV;
- name = newName;
- }
- public double D() {return d; }
- public string Name() { return name; }
- public override string ToString()
- {
- return string.Format("{0};{1}", name, d);
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д