GPWiki.org
GPWiki.org
It is currently Sat May 25, 2013 5:32 pm

All times are UTC




Post new topic Reply to topic  [ 12 posts ] 
Author Message
 Post subject: C# - Simple scope issue
PostPosted: Wed Nov 10, 2010 1:41 am 
Grand Optimizer
User avatar

Joined: Mon Jan 17, 2005 6:01 pm
Posts: 352
Location: Canada
Scope was a simple thing in VB... apparently in .NET it's not so simple.

Heres my basic dillema:

Code:
namespace
{
     class
           {

             method1()
                   {
                      I declare an instance of a class here
                    }


             method2()
                     {
                       I cannot reference the instance here
                      }

             }


}

Any ideas? I know its a very simple scope error, I just don't know how to resolve it. :confused

Any help?

Thanks.

_________________
"None are more hopelessly enslaved than those who falsely believe they are free."
"It is no measure of health to be well adjusted to a profoundly sick society."
"Hope is the first step on the road to dissapointment." -Jonah Orion
http://tankzgame.blogspot.com


Top
 Profile  
 
PostPosted: Wed Nov 10, 2010 2:09 am 
Double Guru
User avatar

Joined: Fri Aug 12, 2005 8:58 am
Posts: 2009
Location: LA, CA
I'm unsure how VB does it but I believe what you are look for are class variables.

Code:
namespace
{
     class
           {
            someClass classVariable;

             method1()
                   {
                      classVariable = new someClass();
                    }


             method2()
                     {
                           classVariable.doSomething();
                      }

             }


}


Is that what you are looking for?

_________________
My Development Blog | My Website | My Current 3d Engine


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 10, 2010 2:39 am 
Grand Optimizer
User avatar

Joined: Mon Jan 17, 2005 6:01 pm
Posts: 352
Location: Canada
Yep, that's it..

Thought it might've been something like that, just wish i didn't have to do it that way.

Oh well, it's probably best in the end as it's alot more structured.

Thanks

_________________
"None are more hopelessly enslaved than those who falsely believe they are free."
"It is no measure of health to be well adjusted to a profoundly sick society."
"Hope is the first step on the road to dissapointment." -Jonah Orion
http://tankzgame.blogspot.com


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 13, 2010 10:14 pm 
King Code Monkey
User avatar

Joined: Wed Sep 01, 2004 3:05 pm
Posts: 11182
Location: Abingdon, MD
Jimbo wrote:
... just wish i didn't have to do it that way.

You would have to do it in VB.NET too.

_________________
Bored? Head on over to my blog and see what I'm up to.

Microsoft XNA MVP


Top
 Profile  
 
 Post subject: Re:
PostPosted: Wed Jan 05, 2011 2:19 pm 
Rookie

Joined: Wed Jan 05, 2011 1:49 pm
Posts: 1
Machaira wrote:
Jimbo wrote:
... just wish i didn't have to do it that way.

You would have to do it in VB.NET too.


Not necessarily. You could instantiate a class in method1 and pass it by reference to method2. This way you are only working with a single instance of the class and you don't have the possible issues and risks associated with global variables. i know this can be done in C# as well, but I don't know the syntax off-hand.

in VB it would be something like...
Code:
Sub Method1()
    Dim myClass As someClass = New someClass()
    Method2(myClass)
End Sub

Sub Method2(ByRef SC as someClass)
    SC.doSomething()
End Sub


Top
 Profile  
 
PostPosted: Wed Jan 05, 2011 3:01 pm 
King Code Monkey
User avatar

Joined: Wed Sep 01, 2004 3:05 pm
Posts: 11182
Location: Abingdon, MD
No parameters were specified in the code however. Obviously you could do the same thing in C# with passing a reference.

_________________
Bored? Head on over to my blog and see what I'm up to.

Microsoft XNA MVP


Top
 Profile  
 
PostPosted: Fri Jan 14, 2011 6:06 am 
P2k
User avatar

Joined: Tue Aug 23, 2005 5:11 am
Posts: 2145
If you are creating instances of the same class in the methods, you most likely mean for the methods to be static. If it isn't static you would have to create an instance of the class just to call the method to get another instance. Which I can't think of a good reason to be doing.

Code:
class SomeClass
{
    public static SomeClass GetInstance() //method 1
    {
        return new SomeClass();
    }
    public static void Process(SomeClass sc) //method 2
    {
        sc.DoSomething();
    }
    public void DoSomething() { }
}

You would call it like this...
Code:
SomeClass.Process(SomeClass.GetInstance());


Maybe that isn't what you mean at all, in which case you should ignore me. It would really help if you posted real code and not pseudo-code when you are asking about syntax.


Top
 Profile  
 
PostPosted: Sun Dec 04, 2011 4:40 pm 
Grand Optimizer
User avatar

Joined: Mon Jan 17, 2005 6:01 pm
Posts: 352
Location: Canada
Got a new similar problem, but slightly different.

Code:
namespace

{

Class
  {

    public void Method1()
     {
     }

     static void Main(string[] args)
     {

     //cant call Method1 here. IntelliSense cant see it, plus object reference errors. tried making method1 static but that caused even more errors.lol.
     }


    }
}


_________________
"None are more hopelessly enslaved than those who falsely believe they are free."
"It is no measure of health to be well adjusted to a profoundly sick society."
"Hope is the first step on the road to dissapointment." -Jonah Orion
http://tankzgame.blogspot.com


Top
 Profile  
 
PostPosted: Sun Dec 04, 2011 8:13 pm 
Rookie
User avatar

Joined: Sun Dec 04, 2011 7:59 pm
Posts: 3
Jimbo wrote:
Got a new similar problem, but slightly different.

I'm only a C++ person, but you can't call non-static methods (like Method1()) inside a static method. If you make Method1() static, it should solve the problem.
If you're getting errors when making Method1() static, that is probably because you are trying to access non-static member variables inside it, which static methods must not do.

So basically, what you could try to make the code work is to also make the member variables that Method1() accesses static. This might work for you if this class contains your whole project, but is horrible style ;)
Before you continue coding in a language that forces you to use object orientation you should maybe try to get a more thorough understanding of it though :)
It's boring in the beginning, but it will pay off once you grasped the concepts.


Top
 Profile  
 
PostPosted: Sun Dec 04, 2011 8:48 pm 
Grand Optimizer
User avatar

Joined: Mon Jan 17, 2005 6:01 pm
Posts: 352
Location: Canada
Thanks for the reply, you were 100% correct. Static has been applied, and the errors have been eliminated.

However, I did some reading on the meaning of "Static" and I think it may be a problem. One of the things I was forced to make static was my array of clsTank (to store player info). Naturally during gameplay, each instance of the Tank class in the players array will have vastly different info in each of it's member variables.. if the instances are all declared static, will that force them all to share the same data?(bad)

Thanks

_________________
"None are more hopelessly enslaved than those who falsely believe they are free."
"It is no measure of health to be well adjusted to a profoundly sick society."
"Hope is the first step on the road to dissapointment." -Jonah Orion
http://tankzgame.blogspot.com


Top
 Profile  
 
PostPosted: Sun Dec 04, 2011 9:43 pm 
Rookie
User avatar

Joined: Sun Dec 04, 2011 7:59 pm
Posts: 3
Jimbo wrote:
if the instances are all declared static, will that force them all to share the same data?(bad)
Thanks

You're welcome :)
Regarding your question, I'm not fully sure how your program is designed, but I don't think it would break your game at this point.
The individual instances inside the array of clsTanks would not share their data, which means you can have completely independent clsTank objects in that array.
The array itself would be static, which should only be a problem for you if you plan to create multiple instances of the class that contains your Main() function and depend on having multiple of these arrays available.

The best idea if you don't want to run into problems in the future is probably to create a class that only contains your static Main function, and not do anything else in that class (except maybe defining static data types that need to be globally available like enums). Then you can move the rest of your game logic out to other classes and safely create instances of them in your Main() function.


Top
 Profile  
 
PostPosted: Sun Dec 04, 2011 11:42 pm 
Grand Optimizer
User avatar

Joined: Mon Jan 17, 2005 6:01 pm
Posts: 352
Location: Canada
That's a really good idea. I might give that a try.

Thanks!

_________________
"None are more hopelessly enslaved than those who falsely believe they are free."
"It is no measure of health to be well adjusted to a profoundly sick society."
"Hope is the first step on the road to dissapointment." -Jonah Orion
http://tankzgame.blogspot.com


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group