SunQuest
           C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old May 2nd, 2008, 12:51 PM
Agri Agri is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2007
Posts: 57 Agri User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 6 h 12 m 44 sec
Reputation Power: 1
Exclamation Fail to transfer variable

Hello,



I am having trouble with the following; I have made an application where the program uses data about a file specified when clicking a link on the context(right-click) menu. When the user clicks the link the program starts up. To get the data about the file i access it through the main args. Like so -



C# Code:
Original - C# Code
  1.  
  2. static void path(string filePath)
  3.  
  4. {
  5.  
  6.  
  7. // Add the file path to a global variable.
  8.  
  9. Global._fullpath = Path.Combine(
  10.  
  11. Path.GetDirectoryName(filePath),
  12.  
  13. string.Format("{0}{1}",
  14.  
  15. Path.GetFileNameWithoutExtension(filePath),
  16.  
  17. Path.GetExtension(filePath)));
  18.  
  19.  
  20. } 






This code is from Program.cs because that is where i can get the data from where the file was selected. However, i only need to use this data when a button is clicked, this button is on another file. When i try to access the data through the global variable it appears as nothing. So, the variable is not transferring across the different files (in this case - program.cs to form1.cs). I know this because if i add a MessageBox to program.cs the variable is shown, whereas, if i do the same on form1.cs its shows nothing. It is not a problem with the global variable set up code.



I have a hunch that this is because the above function is static and it can then only be used in the one file.



Please help,



Thanks,

Reply With Quote
  #2  
Old May 2nd, 2008, 02:10 PM
sizablegrin's Avatar
sizablegrin sizablegrin is offline
Stubborn ol' L'User
Click here for more information.
 
Join Date: Jun 2005
Posts: 3,062 sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 1 Day 9 h 53 m 2 sec
Reputation Power: 1441
Say what?
__________________
C/C++ pointers (Original in the "Commonly Asked Questions" thread).

Reply With Quote
  #3  
Old May 2nd, 2008, 02:19 PM
nattylife nattylife is offline
Closet coder
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2005
Location: Plantation, FL <---south florida
Posts: 1,214 nattylife User rank is Second Lieutenant (5000 - 10000 Reputation Level)nattylife User rank is Second Lieutenant (5000 - 10000 Reputation Level)nattylife User rank is Second Lieutenant (5000 - 10000 Reputation Level)nattylife User rank is Second Lieutenant (5000 - 10000 Reputation Level)nattylife User rank is Second Lieutenant (5000 - 10000 Reputation Level)nattylife User rank is Second Lieutenant (5000 - 10000 Reputation Level)nattylife User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Weeks 16 h 51 m 13 sec
Reputation Power: 70
Send a message via AIM to nattylife
Quote:
Originally Posted by sizablegrin
Say what?

lol yea i was the same way. but another read over and i think i know what hes asking. his c# app has a "global" declared in a file that gets a path but when he tries to access this var in code in another file it gets nothing and hes wondering how to accomplish this.
how exactly is Global._fullpath defined? is it a private/public member of the program class or some other class being used in the application?
what is the scope of the method that in form1.cs that tries to access this variable?
Comments on this post
sizablegrin agrees: OK. You take this one, I have a date at the watering hole. I'll take the next one. Well, maybe.
I can't promise.
__________________

Reply With Quote
  #4  
Old May 2nd, 2008, 03:13 PM
Agri Agri is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2007
Posts: 57 Agri User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 6 h 12 m 44 sec
Reputation Power: 1
Sorry, yes the global variable is defined like so -

c# Code:
Original - c# Code
  1.  
  2.  public class Global
  3.         {
  4.             public static string _fullpath = String.Empty;
  5.             public static string fullpath
  6.             {
  7.                 get
  8.                 {
  9.                     return _fullpath;
  10.                 }
  11.                 set
  12.                 {
  13.                     _fullpath = value;
  14.                 }
  15.             }


Sorry, i will explain it a bit better. I have made an application that interacts with the user by using the right click (context) menu. User right clicks a file > clicks the link to my app > my app loads. I then want to use the information about this file in my app. In this case, the path to the file. The path can be retrieved using the code i posted. I wanted to have the path stored in a "global variable" so i can use in multiple files.

But, for some reason the "global variable" is not being sent out of the file it was defined in. I get the file path through the main arguments. example -

c# Code:
Original - c# Code
  1.  
  2. public void file_path(string[] args)
  3. {
  4. MessageBox.Show(args[o]); // args[0] is the file path.
  5. }


Now, i have found a workaround for this, that's moving the main entry point to another file and defining the variable on that file. This way, it is still possible to use that information on that file. However, it still defeats the purpose of having the file path as a "global" variable. The question is - How do i let the global variable be used outside the file it was defined in? The global variable code does work perfectly well on other files, i think it is something to do with being defined in a static class etc.

Sorry for being unclear,

Reply With Quote
  #5  
Old May 2nd, 2008, 03:17 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,799 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 9 h 55 m 28 sec
Reputation Power: 437
Working solely from general principles (since I have no knowledge of nor experience with nor desire for C#), I would suspect that that global variable is being hidden.

If the global variable is not being hidden but rather is just not visible, then it shouldn't have compiled.

If a local or duplicate variable is hiding that global variable, then I would expect or assume that that local/duplicate had been initialized to zero or empty-string by the system and then never updated by the code and hence would output nothing, as reported.

Reply With Quote
  #6  
Old May 2nd, 2008, 03:25 PM
KitKat77's Avatar
KitKat77 KitKat77 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2005
Location: planet<earth_like_t>
Posts: 282 KitKat77 User rank is Sergeant Major (2000 - 5000 Reputation Level)KitKat77 User rank is Sergeant Major (2000 - 5000 Reputation Level)KitKat77 User rank is Sergeant Major (2000 - 5000 Reputation Level)KitKat77 User rank is Sergeant Major (2000 - 5000 Reputation Level)KitKat77 User rank is Sergeant Major (2000 - 5000 Reputation Level)KitKat77 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 17 h 16 m 30 sec
Reputation Power: 53
Agri, this is not related to your problem and I also am not a C# coder, but... why the heck are you deconstructing and reconstructing the string all in a single statement?

Can't you do just this:

c++ Code:
Original - c++ Code
  1. public void file_path(string[] args)
  2. {
  3.     Global._fullpath = argv[0];
  4. }

Reply With Quote
  #7  
Old May 2nd, 2008, 03:28 PM
Agri Agri is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2007
Posts: 57 Agri User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 6 h 12 m 44 sec
Reputation Power: 1
Good point, for some reason i thought the path was received in chunks, eh...yeah, i will change it, thanks.

Reply With Quote
  #8  
Old May 2nd, 2008, 03:59 PM
nattylife nattylife is offline
Closet coder
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2005
Location: Plantation, FL <---south florida
Posts: 1,214 nattylife User rank is Second Lieutenant (5000 - 10000 Reputation Level)nattylife User rank is Second Lieutenant (5000 - 10000 Reputation Level)nattylife User rank is Second Lieutenant (5000 - 10000 Reputation Level)nattylife User rank is Second Lieutenant (5000 - 10000 Reputation Level)nattylife User rank is Second Lieutenant (5000 - 10000 Reputation Level)nattylife User rank is Second Lieutenant (5000 - 10000 Reputation Level)nattylife User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Weeks 16 h 51 m 13 sec
Reputation Power: 70
Send a message via AIM to nattylife
ok, is Global defined inside the class that holds the program or outside of it?
also, is it defined in the same namespace as the application class?

Reply With Quote
  #9  
Old May 2nd, 2008, 04:01 PM
Agri Agri is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2007
Posts: 57 Agri User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 6 h 12 m 44 sec
Reputation Power: 1
Quote:
Originally Posted by nattylife
ok, is Global defined inside the class that holds the program or outside of it?
also, is it defined in the same namespace as the application class?


Global is defined in a seperate file, in the same namespace.

Reply With Quote
  #10  
Old May 5th, 2008, 08:08 AM
nattylife nattylife is offline
Closet coder
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2005
Location: Plantation, FL <---south florida
Posts: 1,214 nattylife User rank is Second Lieutenant (5000 - 10000 Reputation Level)nattylife User rank is Second Lieutenant (5000 - 10000 Reputation Level)nattylife User rank is Second Lieutenant (5000 - 10000 Reputation Level)nattylife User rank is Second Lieutenant (5000 - 10000 Reputation Level)nattylife User rank is Second Lieutenant (5000 - 10000 Reputation Level)nattylife User rank is Second Lieutenant (5000 - 10000 Reputation Level)nattylife User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Weeks 16 h 51 m 13 sec
Reputation Power: 70
Send a message via AIM to nattylife
Quote:
Originally Posted by Agri
Global is defined in a seperate file, in the same namespace.

try making Global a public static class

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Fail to transfer variable


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump