Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Accessing public variables through namespace?

namespace

  • Please log in to reply
3 replies to this topic

#1 tavichh

tavichh

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 399 posts
  • Location:Delaware
  • Programming Language:C#
  • Learning:C++, PHP, JavaScript

Posted 17 August 2012 - 09:05 PM

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
namespace TheZone
{
public static class unitTable : MonoBehaviour
{
	 public static string UnitName;

	 public static class Freedom
	 {
		 public static class Grunt
		 {
			 unitTable.UnitName;
		 }
	 }
}
}

The "UnitName" being called under the "Grunt" class is not valid. What am I doing wrong?
The last time I had a similar problem which was accessing public variables through library's and you guys said to add "Static" which worked, but its not working now that I am trying to access it through a namespace and not a library.

I know that this a newbie question but I forgot alot after being so empty with my programming.
  • 0
Send a like my way if I helped you or if you found my post useful or even just enjoyed reading my post.
Thank you :)

#2 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1360 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash

Posted 18 August 2012 - 01:44 AM

You are doing 2 things wrong:

1) not understanding that static variables belong to the class and have to be assigned before use. They are not a placeholder for an internal variable.

2) there are very few reasons to encapsulate a class within another class.
  • 0
My Blog: http://forum.codecal...699-blog-77241/
"Women and Music: I'm always amazed by other people's choices." - David Lee Roth

#3 tavichh

tavichh

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 399 posts
  • Location:Delaware
  • Programming Language:C#
  • Learning:C++, PHP, JavaScript

Posted 18 August 2012 - 04:42 AM

You are doing 2 things wrong:

1) not understanding that static variables belong to the class and have to be assigned before use. They are not a placeholder for an internal variable.

2) there are very few reasons to encapsulate a class within another class.


1) So I should learn about the Static Variable?
2) I did that for an ease of access for the other programmers. To keep it neater, not for any specific reason about programming.
  • 0
Send a like my way if I helped you or if you found my post useful or even just enjoyed reading my post.
Thank you :)

#4 Tonchi

Tonchi

    Helping the world with programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1249 posts
  • Location:Zagreb
  • Programming Language:C#, Others
  • Learning:C, C++, Python, JavaScript, Transact-SQL, Assembly

Posted 18 August 2012 - 05:41 AM

1) So I should learn about the Static Variable?
2) I did that for an ease of access for the other programmers. To keep it neater, not for any specific reason about programming.


For your first answer. You should learn everything. You don't know when you will need it.
  • 0

Microsoft Student Partner, Microsoft Certified Professional






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download