Jump to content
Xtreme .Net Talk

c#: (simple question) calling the variables - please aid me...


Recommended Posts

Posted

Well, I have created 3 files on my project:

1 - f_Construction (form)

2 - t_Construction (class)

3 - GlobalVars (class)

 

I have declared some public vars in the GlobalVars class:

I use the: "GlobalVars myVars = new GlobalVars();" to gain access to those vars within the GlobalVars class throught both f_construction and t_construction.

The prob is that if I change a var: "f_Construction.myVars.varName += 1" trought the f_construction form the varName will be "0" everytime I call it throught: "t_construction.myVars"! Or it will just create another instance of those vars, I don't know.

 

QUESTION: How can I call the vars inside "GlobalVars" so that they will keep its modified values?

 

In VB6 this was just SOOOOO easy: "form / module Name.Varname = newValue", and, whenever I call it again, it would contain the last sent value.

 

To be honest, well, maybe I was just bad educated with VB6 but, I have seen more disavantages then advantages in .NET framework...

'It looks like a kind of primitive the way .NET works, having to create instances to almost everything! We can't just be direct in .NET, like, I can't just tell him to grad that candy. First I have to say: "Hey dude! Prepare to grab that candy" then "Ok, now u can grab that candy" (ok, I confess, am a bit frustrated with .NET at the moment.)

Well, maybe this is the right way to workout, but, for me, its just pain in the ***. Maybe I will change my mind after my .NET programing skills increase, who knows?

 

Hope I can hear from u soon... And give more atenttion to my question rather of what I think (at the moment) of .NET...

  • Administrators
Posted

If you declare the variables as static then you can access them using the classname.variablename syntax rather than creating an instance of the class.

However global variables can lead to hard to maintain code - they can be changed from anywhere, you may want to look at organising your code differently to reduce the need for globals.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted (edited)

PlausiblyDamp I have changed the my public var to static var inside my GlobalVars class... but still I can't call it if i type Globalvars.varname...

Wasn't the "varName" appear at the popup list the minute u type "globalVars."?

Well, inside globalVars class works.. but i want to do it outside that class...

 

Engine252 I want to know how can I access / change the vars inside my throught another classes / forms "GlobalVars class" rather creating instances of them, like: (vb6 example) "formName.varname = x" (can't explain better then this...)

 

Engine252 tks alot for the example u gave me in the other thread about transparent labels.. thet was truly helpfull

Edited by EFileTahi-A
Posted

LOL! Oh God... i had "public int varName" and then i put "static int varName"... now i realize that i need "public static int varName" ARGHHH!!

 

Thread solved!

 

tks for posting out!

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...