Game Development Community

inheriting varibles that are being written to

by Anthony Ratcliffe · in General Discussion · 05/12/2009 (2:10 pm) · 3 replies

the old programming part of the forum went so i posted here.
basically using c# i need to write to a varible then out put it from diffrent classes
public class profile
    {
        public string postcode;

        public void profileupdate()
        {
            
            Console.WriteLine("please enter postcode");
            postcode = Console.ReadLine();
        }
    }

im trying to inherit in this class
public class print
    {
        public void printseat()
        {
            profile profileupdate = new profile();
            
            Console.WriteLine("" + profileupdate.postcode + "");
            System.Threading.Thread.Sleep(1000000);
        }
    }

i have a feeling it is because im inheriting a new instance of the varible but im not sure how to overcome it?

#1
05/12/2009 (9:22 pm)
Not quite sure what you're talking about here.

Inheriting something implies that you have a base class and a derived class, which you don't have.

You are creating a profile object named profileupdate, but you're not calling the profileupdate() method, so your postcode is not going to be set.

Maybe provide some more information about what you're trying to accomplish?
#2
05/12/2009 (9:27 pm)
ive uploaded the file so u can see what im trying to do, basically im trying to take varible postcode from profile and display it in print class

http://www.mediafire.com/?sharekey=959536181be78135391d7d881749d3a7bcd3397c05f028475be6ba49b5870170
#3
05/12/2009 (9:39 pm)
I don't see how to download the file from there, it keeps wanting me to take a family guy quiz... I'm sleep deprived so maybe in the morning.

But from what I think you're trying to do, there are a few ways to do it.

If you're calling profileupdate() from somewhere else, then you'll probably want your printseat method to take a profile object as an input parameter. i.e.

public class print
{
    public void printseat( profile profileupdate )
    {
        Console.WriteLine("" + profileupdate.postcode + "");
        System.Threading.Thread.Sleep(1000000);
    }
}

and then when you call printseat, pass the profile object that you initialized into it, i.e.:

profile prof = new profile();
prof.profileupdate();

print printobject = new print();
printobject.printseat( prof );

Or something to that effect.

You could make "postcode" a static member variable, and not have to pass the object, but that would be a very bad practice for something like that.