我是c#的新手(之前有c ++背景).
我有类"MyClass",它在同一名称空间中创建另一个类"Symbols"的对象.我创建类"符号"的对象,以定义从"MyClass"调用的函数"add_symbol()".
在c ++中它很安静:
void NameOfTheClass::NameOfTheFunction()
{
}
Run Code Online (Sandbox Code Playgroud)
但我不知道怎么做c#.我尝试了下面的代码,并得到以下错误(见最后).
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace shekhar_final
{
public class Symbols
{
public void add_symbol(byte processingValue) //called from MyClass
{
Console.WriteLine(processingValue);
}
}
public class MyClass
{
public static void Main(string[] args)
{
Symbols ObjSym =new ObjSym(); //Here is the error
using (var stream = new BinaryReader(System.IO.File.OpenRead(args[0])))
{
while (stream.BaseStream.Position < stream.BaseStream.Length)
{
byte processingValue = stream.ReadByte();
ObjSym.add_symbol(processingValue); //This function is called which is defined in Symbol class
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
错误是(我在我的代码中指出了导致此错误的行):
error CS0246: The type or namespace name `ObjSym' could not be found. Are you missing a using directive or an assembly reference?
Run Code Online (Sandbox Code Playgroud)
请帮我删除此错误,任何额外的指导也很感激,因为我是c#的新手,谢谢
代替
Symbols ObjSym = new ObjSym();
你应该用类初始化:
Symbols ObjSym = new Symbols();.
现在你正在尝试初始化变量而不是类.