我创建了一个名为“AddNumbers”的函数,但是当我调用它时,我收到一条错误,上面写着“需要一个对象引用”。我已将此函数放在Main()入口点之外,但我确信它与放置无关,因为我尝试移动它并且它仍然无效。
namespace FunctionPractice
{
class Program
{
public int AddNumbers(int number1, int number2)
{
int result = number1 + number2;
return result;
}
static void Main(string[] args)
{
Console.WriteLine("Enter 2 numbers: ");
int firstNumber = Convert.ToInt16(Console.ReadLine());
int secondNumber = Convert.ToInt16(Console.ReadLine());
int result = AddNumbers(firstNumber, secondNumber);
Console.WriteLine(result);
}
}
}
答案 0 :(得分:0)
Si static
为AddNumbers
,' Code Block to prevent pasting Images with PasteSpecial
If IsClipboardContentInExcelFormat Then
Selection.PasteSpecial Paste:=xlValues
Else
Selection.Value = GetClipBoardText
End If
Function GetClipBoardText()
' src: http://stackoverflow.com/questions/9022245/get-text-from-clipboard-using-gettext-avoid-error-on-empty-clipboard
Dim DataObj As MSForms.DataObject
Set DataObj = New MSForms.DataObject '<~~ Amended as per jp's suggestion
Dim myString As String
On Error GoTo ErrorHandling
DataObj.GetFromClipboard '~~> Get data from the clipboard.
myString = DataObj.GetText(1)
GetClipBoardText = myString
Exit Function
ErrorHandling:
If Err <> 0 Then MsgBox "Data on clipboard is not text or is empty"
End Function
' Will use MSForms.DataObject for geting clipboard content
' reference: http://stackoverflow.com/questions/9022245/get-text-from-clipboard-using-gettext-avoid-error-on-empty-clipboard
' reference: http://msdn.microsoft.com/en-us/library/office/ff839748%28v=office.14%29.aspx
Private Function IsClipboardContentInExcelFormat() As Boolean
IsClipboardContentInExcelFormat = False
On Error GoTo ErrorHandling
Dim aFmts, fmt
aFmts = Application.ClipboardFormats
For Each fmt In aFmts
If (fmt = xlClipboardFormatBIFF _
Or fmt = xlClipboardFormatBIFF12 _
Or fmt = xlClipboardFormatBIFF2 _
Or fmt = xlClipboardFormatBIFF3 _
Or fmt = xlClipboardFormatBIFF4 _
) Then
IsClipboardContentInExcelFormat = True
Exit Function
End If
Next
Exit Function
ErrorHandling:
If Err <> 0 Then MsgBox "Cannot Read Data in Clipboard"
End Function
也必须是静态的。
答案 1 :(得分:0)
嘿你的方法工作得很好你只需要让你的“AddNumbers”方法静态。您不能从静态方法中调用非静态方法。此外,您可能希望在末尾添加读取行,以便程序在写入屏幕后不会关闭。这将是固定代码。
class Program
{
public static int AddNumbers(int number1, int number2)
{
int result = number1 + number2;
return result;
}
static void Main(string[] args)
{
Console.WriteLine("Enter 2 numbers: ");
int firstNumber = Convert.ToInt16(Console.ReadLine());
int secondNumber = Convert.ToInt16(Console.ReadLine());
int result = AddNumbers(firstNumber, secondNumber);
Console.WriteLine(result);
Console.ReadLine();
}
}
答案 2 :(得分:0)
让你的功能成为
public static int AddNumbers(int number1, int number2)
。
这是因为,您正在从属于类本身的静态函数调用您的函数。您的函数属于该类的对象。因此,当您调用静态函数main()
时,它不知道要调用哪个实例AddNumbers
。因此错误。请详细了解static
关键字。
答案 3 :(得分:0)
将方法声明更改为
public static int AddNumbers(int number1, int number2)
或者在main方法中创建Program类的实例,然后访问add numbers方法。
namespace FunctionPractice
{
class Program
{
public int AddNumbers(int number1, int number2)
{
int result = number1 + number2;
return result;
}
static void Main(string[] args)
{
Console.WriteLine("Enter 2 numbers: ");
int firstNumber = Convert.ToInt16(Console.ReadLine());
int secondNumber = Convert.ToInt16(Console.ReadLine());
Program program = new Program();
int result = program.AddNumbers(firstNumber, secondNumber);
Console.WriteLine(result);
}
}
}