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);


Edited by carlitosway17: n/a

6 Years
Discussion Span
Last Post by Mitja Bonca

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.

Edited by Momerath: n/a


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!

Edited by Mitja Bonca: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.