The type or namespace name "PeterO" could not be found

205 views Asked by At

I am just giving a try at utilizing https://github.com/peteroupc/CBOR repo with demo hello.cs program. But I end up with the following issues.

1) Attribute PeterO.Cbor is not reflecting as others in color. Please refer to the below code.

using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using PeterO;
using PeterO.Cbor;  // Why this attribute is in different color?

namespace Hello
{
    public class HelloWorld
    {
        public static void Main(string[] args)
        {
            Console.WriteLine ("Hello World!");
            var cbor1 = CBORObject.NewMap()
               .Add("item", "any string")
               .Add("number", 42)
               .Add("map", CBORObject.NewMap().Add("number", 42))
               .Add("array", CBORObject.NewArray().Add(999f).Add("xyz"))
               .Add("bytes", new byte[] { 0, 1, 2 });
            // The following converts the map to CBOR
            byte[] bytes = cbor1.EncodeToBytes();
            // The following converts the map to JSON
            string json = cbor1.ToJSONString();
            System.Console.WriteLine(json);
            Console.ReadKey();
        }
    }
}

2) Error during execution of hello.cs program. Please find the same from the below snapshot.

Command Used: mcs hello.cs -out:hello

Compilation Error Output Msg:

hello.cs(8,7): error CS0246: The type or namespace name `PeterO' could not be found. Are you missing an assembly reference?
Compilation failed: 1 error(s), 0 warnings

Additional Info:

1) Dotnet-sdk and Visual Studio Code installation steps are followed as below:

Dotnet-sdk installation steps:
    a) wget https://download.visualstudio.microsoft.com/download/pr/f01e3d97-c1c3-4635-bc77-0c893be36820/6ec6acabc22468c6cc68b61625b14a7d/dotnet-sdk-3.1.402-linux-x64.tar.gz
    b) mkdir -p $HOME/dotnet && tar zxf dotnet-sdk-3.1.402-linux-x64.tar.gz -C $HOME/dotnet
    c) export DOTNET_ROOT=$HOME/dotnet
    d) export PATH=$PATH:$HOME/dotnet

Visual Studio Code installation steps:
    a) wget https://az764295.vo.msecnd.net/stable/2af051012b66169dde0c4dfae3f5ef48f787ff69/code_1.49.3-1601661857_amd64.deb
    b) sudo dpkg -i code_1.49.3-1601661857_amd64.deb && sudo apt-get update

2) Ubuntu, Dotnet, and Visual Studio Code version details:

a) Ubuntu Command: lsb_release -a

Output:
    No LSB modules are available.
    Distributor ID: Ubuntu
    Description:    Ubuntu 18.04.5 LTS
    Release:    18.04
    Codename:   bionic
b) Dotnet Command: dotnet --info

Output:
    .NET Core SDK (reflecting any global.json):
     Version:   3.1.402
     Commit:    9b5de826fd

    Runtime Environment:
     OS Name:     ubuntu
     OS Version:  18.04
     OS Platform: Linux
     RID:         ubuntu.18.04-x64
     Base Path:   /home/$USER/dotnet/sdk/3.1.402/

    Host (useful for support):
      Version: 3.1.8
      Commit:  9c1330dedd

    .NET Core SDKs installed:
      3.1.402 [/home/$USER/dotnet/sdk]

    .NET Core runtimes installed:
      Microsoft.AspNetCore.App 3.1.8 [/home/$USER/dotnet/shared/Microsoft.AspNetCore.App]
      Microsoft.NETCore.App 3.1.8 [/home/$USER/dotnet/shared/Microsoft.NETCore.App]

    To install additional .NET Core runtimes or SDKs:
      https://aka.ms/dotnet-download
c) Visual Studio Code details:

    Version: 1.49.3
    Commit: 2af051012b66169dde0c4dfae3f5ef48f787ff69
    Date: 2020-10-02T17:56:11.027Z
    Electron: 9.2.1
    Chrome: 83.0.4103.122
    Node.js: 12.14.1
    V8: 8.3.110.13-electron.0
    OS: Linux x64 5.4.0-48-generic

3) Dotnet restore command output:

Path: /path/to/repo/CBOR/
Command: dotnet restore
Output: 
    Determining projects to restore...
    /path/to/repo/CBOR/CBOR.csproj : error NU1108: Cycle detected.  [/path/to/repo/CBOR.sln]
    /path/to/repo/CBOR/CBOR.csproj : error NU1108:   PeterO.Cbor -> PeterO.Cbor (>= 4.2.0). [/path/to/repo/CBOR.sln]

So, I replaced the PackageId PeterO.Cbor with PackageId Peter Cbor inside a file /path/to/repo/CBOR/CBOR.csproj. Then it gave the build output as below.

Path: /path/to/repo/CBOR/
Command: dotnet restore
Output:
    Determining projects to restore...  Restored 
    /path/to/repo/CBOR/CBOR.csproj (in 306 ms).
    Restored /path/to/repo/CBORTest/CBORTest.csproj (in 319 ms).
    6 of 8 projects are up-to-date for restore.

I am very new to the visual studio code and dotnet framework as well.

Could anyone please correct me on this issue and let me know where I went wrong.

1

There are 1 answers

4
MindSwipe On

1:
That's not an attribute, it's called a using directive and it's essential the same as import in Java, Python (and maybe more) and comparable to a #include in C/C++. Attributes are something entirely different in C#.

2:
mcs is the Mono C# compiler and has nothing to do with .NET. Instead use the dotnet cli to run, restore, build and whole lot more.


Now, your entire question is kind of confusing, so I'll go step by step showing you how to do what you want.

  1. Create a new project
dotnet new console --name YourProjectName

This will create a new directory YourProjectName and place everything you need inside it. Namely, a YourProjectName.csproj file and a Program.cs file. The Program.cs is simply a convention, your entry point (Main method) can lie anywhere.

  1. Add the CBOR package
    CBOR is available on NuGet, meaning we can simply run
dotnet add package PeterO.Cbor --version 4.2.0

And this will do everything we need it to do. Under the hood this will add

<ItemGroup>
  <PackageReference Include="PeterO.Cbor" Version="4.2.0" />
</ItemGroup>

to your YourProjectName.csproj file and restore (download/ build any dependencies) of of your project.

  1. Now edit Program.cs Now you can open VS Code (or any text editor) and edit Program.cs to do what you want. If you want to test out your program, simply switch to the terminal, cd to the directory of your project and run dotnet run. This will compile and run your project.

P.S: I highly recommend following the advice given in C# programming with Visual Studio Code guide