am trying to create a new class for a binary search program in c# I keep getting this compilation error A namespace cannot directly contain members such as fields or methods

here is my code

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

namespace ConsoleApplication1{

// Create a new binary tree
bt = new TBinarySTree();


// Insert data
bt.insert ("Canis Minoris", 5.37);
bt.insert ("Gamma Cancri", 4.66);
bt.insert ("Phi Centauri", 3.83);
bt.insert ("Kappa Tauri", 4.21);
 
// Retrieve data  
TTreeNode symbol = bt.findSymbol ("Phi Centauri");
if (symbol != null)
   Console.WriteLine ("Star {1} has magnitude = {0}", symbol.name, symbol.value);

}

You need a class to hold those:

namespace ConsoleApplication1 {
    class MyClass {
        // create a new binary tree
        bt = new TBinarySTree();
        // etc.

Statements (like 'if') need a method in the class.

And where do you instantiate the object, like:
TBinarySTree bt; ?

You can create a new instance of an object on two ways:

namespace ConsoleApplication1
{
    class MyClass
    {
        //1. in the class:
        BinarySTree bt = new TBinarySTree();
   
        //2.In the method
        BinarySTree bt;
        private void MyMethod()
        {
            bt = new BinarySTree(); //like momerath said!
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.