views:

94

answers:

5

I am working on my first C# program and have run into a brick wall. I want to be able to set and get variables throughout diferent forms in the same application.

I created a class caleld "data" which contains the following:

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

namespace Application1
{
    public class data
    {

        public string SearchAirport
        {
            get 
            { 
                return searchairport; 
            }
            set 
            { 
                searchairport = value; 
            }


        }
    }
}

What do I need to put into my forms to be able to use this class??

Right now all I have is:

data.SearchAirport = commandAirport;
string working = data.SearchAirport;

I know I have to add something else to keep from getting the "Error 11 An object reference is required for the non-static field, method, or property 'Sector_Datastore_2._0.data.SearchAirport.get'..." error

A: 

data d = new data();

....before those lines

SP
+2  A: 

Well, you need to declare searchairport:

public class data
{
    private string searchairport;

    public string SearchAirport
    {
        get 
        { 
            return searchairport; 
        }
        set 
        { 
            searchairport = value; 
        }


    }
}

alternatively, you could let C# do that automatically by using the following code:

public class data
{
    public string SearchAirport
    {
        get;
        set;
    }
}
tster
If you're really lazy, you'll type `prop` followed by 2 tabs.
Charlie Salts
If you're really really lazy, you'll type prop followed by a *single* tab (thanks R#)
Martinho Fernandes
If you're really really really lazy, you use butterflies.
Charlie Salts
VS Studio 2010 beta 2 C# : "prop" followed by two tabs inserts template for Public Property type 'int with "automatic" get and set : inside methods, or inside class definitions, even right after the 'namespace defintion. Of course define a Property like that ... only inside the "namespace" ... you'll get a compile error.If dumb, type (in namespace scope : not inside class definition or method) "public string" followed by two tabs : get a generic "struct" definition :)Type only the letter "i" followed by two tabs : what did you expect ? Auto-completion is a strange and wondrous beast !
BillW
+1  A: 

You are accessing searchAirport statically, and the method itself is not static.

You can either add the static keyword to the SearchAirport method signature or create a data object and then call SearchAirport on that object.

Jacob Relkin
A: 

Thank you guys, that got it!

Brodie
Which answer? Please delete this post, and use a comment instead.
Charlie Salts
+1  A: 

I'd suggest a Service Locator pattern, but I'm afraid it's way too complicated for what the Question-poster wants to achieve.

Just in case it may be useful later on: Service Locator pattern

Webleeuw