PDA

View Full Version : اشکال در کد نویسی کلاس



voice.of.silence
چهارشنبه 28 تیر 1391, 11:14 صبح
سلام دوستان:
من یک کلاس از نوع Partial تعریف کرده ام .
نام کلاس Calculation . وظیفه محاسبه ی مساحت ، محیط و حجم اشکال هندسی.
برای محاسبه ی مساحت مستطیل باید یک کانستراتور تعریف کنم که با استفاده از آن بتوانم در محیط ویژوال مقدار دهی کنم .
مشکل : نمی دانم که چگونه به کانستراتور چند پارامتر بدهم .
من با استفاده از (ویلگول) چند پارامتر پاس دادم اما سیستم قبول نکرد .
دوستان عزیز امیدوارم که شما مرا در حل این مشکل راهنمایی نمایید .
با سپاس Voice of Silence

voice.of.silence
چهارشنبه 28 تیر 1391, 15:25 عصر
Code of Class Calculation


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

namespace Calculation
{
partial class Calculation
{
double r;
double r2;
double l;
double w;
double h;

public double csi()
{
return Math.PI * r * r;
}
public double ssi()
{
return w * w;
}
public double rsi()
{
return l * w;
}
}
}

*********************

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

namespace Calculation
{
partial class Calculation
{
public Calculation()
{
r = 1;
w = 5;
l = 10;

}
public Calculation(double initiall,double initialw,double initialx)
{
r = initialx;
l = initiall;
w = initialw;
}


}
}

*******************

Code of Form

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Calculation
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Calculation csi = new Calculation();
MessageBox.Show(csi.csi().ToString());
}

private void button2_Click(object sender, EventArgs e)
{
Calculation ssi = new Calculation();
MessageBox.Show(ssi.ssi().ToString());
}

private void button3_Click(object sender, EventArgs e)
{
Calculation rsi = new Calculation();
MessageBox.Show(rsi.rsi().ToString());
}

private void button1_Click_1(object sender, EventArgs e)
{
Calculation csi = new Calculation(double.Parse(textBox1.Text));
MessageBox.Show(csi.csi().ToString());
}

private void button2_Click_1(object sender, EventArgs e)
{
Calculation ssi = new Calculation(double.Parse(textBox2.Text));
MessageBox.Show(ssi.ssi().ToString());
}

private void button3_Click_1(object sender, EventArgs e)
{
Calculation rsi = new Calculation(initialw = double.Parse(textBox2.Text), initiall = double.Parse(textBox3.Text));
MessageBox.Show(rsi.rsi().ToString());
}
}
}

*********************

Eror

Error 3 The name 'initialw' does not exist in the current context C:\Users\IZUMI\Documents\Visual Studio 2010\Projects\Calculation\Calculation\Form1.cs 51 47 Calculation
Error 4 The name 'initiall' does not exist in the current context C:\Users\IZUMI\Documents\Visual Studio 2010\Projects\Calculation\Calculation\Form1.cs 51 87 Calculation
Error 1 'Calculation.Calculation' does not contain a constructor that takes 1 arguments C:\Users\IZUMI\Documents\Visual Studio 2010\Projects\Calculation\Calculation\Form1.cs 39 31 Calculation
Error 2 'Calculation.Calculation' does not contain a constructor that takes 1 arguments C:\Users\IZUMI\Documents\Visual Studio 2010\Projects\Calculation\Calculation\Form1.cs 45 31 Calculation

Saeed_m_Farid
چهارشنبه 28 تیر 1391, 16:07 عصر
فقط همین قسمت‌اش کافی بود:
namespace MyCalculation
{
public partial class Calculation
{
double r;
double r2;
double l;
double w;
double h;

public double csi() // Circle
{
return Math.PI * r * r;
}
public double ssi() // Square
{
return w * w;
}
public double rsi() // RecTangle
{
return l * w;
}
}
}

namespace MyCalculation
{
public partial class Calculation
{
public Calculation()
{
r = 1;
w = 5;
l = 10;
}

public Calculation(double initiall, double initialw, double initialx)
{
r = initialx;
l = initiall;
w = initialw;
}
}
}
این کد بی‌معنی هست! خوب شما حالا می‌خواین Polymorphism (http://msdn.microsoft.com/en-us/library/ms173152.aspx) رو یاد بگیرین، یا override (http://msdn.microsoft.com/en-us/library/ebca9ah3%28v=vs.71%29.aspx) کردن یا overload سازنده (http://msdn.microsoft.com/en-us/library/ms173152.aspx) یا مفهوم partial (http://msdn.microsoft.com/en-us/library/wa80x488%28v=vs.100%29.aspx) یا یه مثال از پیاده‌سازی درست ارث‌بری (+ (http://stackoverflow.com/questions/3314154/class-inheritance-and-polymophism)) یا ...؟
اگه هیچکدوم از اینها منظور شما نبوده! و فقط می‌خواین برحسب Shape مساحت بدست بیارید، لازم به اینهمه کار نبود، اینها رو بفرمائید و بی‌زحمت همه یونیت‌ها رو Copy/Paste نکنید و از تگ #C (برای قرار دادن کد در اینجا) استفاده کنید...