PDA

View Full Version : سوال: یک مشکل مبتدیانه در مورد متد ها



kingtak
شنبه 03 دی 1390, 23:05 عصر
سلام دوستان
من تازه دارم C#‎‎‎‎‎‎‎‎ یاد میگیرم.یه مشکل در مورد متد ها دارم.
کامپایلر ویژوال استادیو تو لاین 14 این پیغام رو میده:

An object reference is required for the nonstatic field, method, or property property 'consoleApplaction3.program1.zarbValu(int,int)'...


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

namespace ConsoleApplication3
{
class Program1
{
static void Main(string[] args)
{
int m = 5;
int n = 7;
zarbValu(m, n);
}
private void zarbValu(int m,int n)
{

int result=m * n;
Console.WriteLine("the result is:{0}", result);
return;
}


}
}

sayvan
شنبه 03 دی 1390, 23:14 عصر
متد main استاتیک پس تابع برنامه هم به صورت استاتیک تعریف کن
class Program
{

static void Main(string[] args)
{
int m = 5;
int n = 7;
zarbValu(m, n);
}
static void zarbValu(int m, int n)
{

int result = m * n;
Console.WriteLine("the result is:{0}", result);
Console.ReadLine();
return;
}

خط console.ReadLine() برای گرفتن مقداری از ورودی- در اینجا برای اینکه برنامه بعد از وارد کردن رشته ایی بسته شود و نتیجه نشان داده شود استفاده میکنیم