One Question

This is a discussion on One Question within the C# Programming forums, part of the General Programming Boards category; hello, guys: this is the code: // created on 5.7.2002 at 10:47 using System; public class Test { public struct ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    173

    One Question

    hello, guys:
    this is the code:

    // created on 5.7.2002 at 10:47
    using System;
    public class Test
    {

    public struct Car
    {
    public int year;

    }


    Car mycar = new Car();

    public static void Main()
    {

    Console.WriteLine(mycar.year);

    }

    public void mytest()
    {
    Console.WriteLine(mycar.year);

    }


    }

    and I got such error: An object reference is required for the nonstatic field, method, or property 'Test.mycar'

    So I have to declare another Car mycar = new Car();
    in static void Main() function to solve this problem,

    so is there any way so that I declare Car mycar = new Car();
    once and use it in both functions: void main() and void mytest().

    Thank you!!
    Don't laugh at me,I am just a SuperNewbie.

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,138
    Your Main is a static method. It can therefore only access other static data. your Car variable is a member of your test class. No instance of your class is created, so no Car variable exists.

    If you need only this one car variable, make it static, too. If you really need one car variable per test class, you have to find some other solution.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,138
    btw.: Who will call the testfunction ? If Main does it, it has to be static, too.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    173
    I just made a the class Test reference in Main() like

    public static void Main()
    {
    Test test2 = new Test();
    //then I call the testfunction:
    test2.mytest();



    }

    so mytest() function is not static,but it wroks.
    Don't laugh at me,I am just a SuperNewbie.

  5. #5
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,138
    ok, now you have an instance of your class and you can access it's methods and members
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21