12 Replies - 479 Views - Last Post: 26 November 2013 - 05:59 PM Rate Topic: -----

#1 vpdic  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 32
  • Joined: 24-November 13

Creating an array variable accessible from anywhere in the project

Posted 24 November 2013 - 08:15 PM

I'm attempting to create an array using the proxyScanlist structure. This array should be readable anywhere in the program. In this example I named the array "myScanlist" The buildScanlist class is figurative but provides the general idea of what will happen.

The array will be written to once and then only read. As a side note: is string really the best way to store an IPAdress from a list/text format?

Thanks.

namespace ProxyScanner
{

    public struct proxyScanlist
    {
        public string IPAddress;
        public int Port;
        public byte Type;
        public bool Verified;
    }

    public class buildScanlist
    {    
        //streamreader open some file here
        proxyScanlist[] myScanlist = new proxyScanlist[lines in file];
        parsedText = DoSomeWorkHere();
        proxyScanlist[index] = parsedText;
    }

}



Is This A Good Question/Topic? 0
  • +

Replies To: Creating an array variable accessible from anywhere in the project

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3575
  • View blog
  • Posts: 11,116
  • Joined: 05-May 12

Re: Creating an array variable accessible from anywhere in the project

Posted 24 November 2013 - 08:32 PM

There is an IPAddress class: http://msdn.microsof...(v=vs.110).aspx

If it is a list, make it a List<>. To make it accessible to everybody without having to pass around an instance of the containing class, make the list a public static member of the class.

Note that in general having a global variable like this is typically a bad practice. You have to be very disciplined about using globals. In your case, if you do know that it will always be read-only after its been initialized, then I think that you'll be okay, but I always tend to look at globals with suspicion.
Was This Post Helpful? 0
  • +
  • -

#3 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 673
  • View blog
  • Posts: 2,963
  • Joined: 03-December 12

Re: Creating an array variable accessible from anywhere in the project

Posted 24 November 2013 - 08:33 PM

What's wrong with how you did it? Have the array stored privately in the class with a getter function and call the getter function when you need the array.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3575
  • View blog
  • Posts: 11,116
  • Joined: 05-May 12

Re: Creating an array variable accessible from anywhere in the project

Posted 24 November 2013 - 08:36 PM

With the way the OP did it, he'll need to pass around an instance of the buildScanList object to all the classes within his program.
Was This Post Helpful? 0
  • +
  • -

#5 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 673
  • View blog
  • Posts: 2,963
  • Joined: 03-December 12

Re: Creating an array variable accessible from anywhere in the project

Posted 24 November 2013 - 08:42 PM

I did not notice that. In that case, if it will not change then why not initiate it once into a Global Constant?
Was This Post Helpful? 0
  • +
  • -

#6 vpdic  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 32
  • Joined: 24-November 13

Re: Creating an array variable accessible from anywhere in the project

Posted 25 November 2013 - 04:41 PM

I'm new to C# and I did a lot of reading to determine the best way to go about things. That being said, my way isn't necessarily the best way.

Here's a better explanation as to why I want it the way I do...

1. The list will be generated all at once, and then read from later bits at a time, but never altered without creating an entire new list. With this, from what I've read, it seems that an array would be the most resource friendly as well the fastest way to go about this.

2. The list will have to be persistent because creating instances at the rate I would need them will damper performance, at least from what I've gathered.

I've watched at least 50 hours of tutorial videos by now as well did online research; but none of them hit this point specifically.
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3575
  • View blog
  • Posts: 11,116
  • Joined: 05-May 12

Re: Creating an array variable accessible from anywhere in the project

Posted 25 November 2013 - 05:33 PM

This is golden nugget I picked up from Extreme Programming and have take to applying to other endeavors: Implement the simplest thing that could possibly work. (It's a variation of the K.I.S.S principle.) The idea in XP was that you will always be free to refactor down the road if you need to. What I've found is that outside the context of XP is that getting something working as quickly as possible lets me get the entire project up and running first, and lets me figure out what the bottlenecks are and if need be focus on those. It lets me avoid premature optimization.
Was This Post Helpful? 2
  • +
  • -

#8 vpdic  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 32
  • Joined: 24-November 13

Re: Creating an array variable accessible from anywhere in the project

Posted 25 November 2013 - 05:51 PM

That actually makes sense, and I think answering this will resolve my problem down the road when I do optimize as well. Let's assume I go with List because those are easy to modify and build.

How would I go about pulling up a created list from anywhere in the project without having to instance the class each time?
Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5522
  • View blog
  • Posts: 11,830
  • Joined: 02-June 10

Re: Creating an array variable accessible from anywhere in the project

Posted 25 November 2013 - 07:20 PM

I'm going to guess this is a WinForms project.

May I point out that the Application class is available to all the other classes?
If you want something to be available to the entire scope of the application, then put it in the application class.

The you can access it as Application.IPAddress for example.

Or, since it is a setting... You could put it in the Application.Settings, that seems like a really good place for it. And then it can save/load with the program.

Quote

I've watched at least 50 hours of tutorial videos by now as well did online research; but none of them hit this point specifically.

Videos are really only a so-so way of learning programming. And after only 50 hours of video you really aren't ready to try designing and building an application. You simply don't know, how much you don't know. This is pretty much paramount to saying "I've watched 50 hours of Bruce Lee movies so I'm pretty good at Kung-Fu. Now tell me what I need to know to stop a mugger."

Get some "C# in 30 days" type books and work them cover to cover. Don't try to build anything of your own. Just work the books and learn the underlying concepts. The guys that try to start off building and just want to learn enough to get them through the next hurdle aren't worth the powder it would take to blow them up.

This post has been edited by tlhIn`toq: 25 November 2013 - 07:21 PM

Was This Post Helpful? 0
  • +
  • -

#10 vpdic  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 32
  • Joined: 24-November 13

Re: Creating an array variable accessible from anywhere in the project

Posted 25 November 2013 - 08:04 PM

View PosttlhIn`toq, on 25 November 2013 - 07:20 PM, said:

I'm going to guess this is a WinForms project.

May I point out that the Application class is available to all the other classes?
If you want something to be available to the entire scope of the application, then put it in the application class.

The you can access it as Application.IPAddress for example.

Or, since it is a setting... You could put it in the Application.Settings, that seems like a really good place for it. And then it can save/load with the program.


I don't think that would work for what I plan on doing. The scanlist, a proxy scanlist, will be loaded when the user selects the scanlist option. They will have the ability to choose other files, or whatever I think of down the road. It can't be a constant for this reason, and since the scanlist will be used through other methods in other .cs files I would like to find a way to make it stick. I'm sure I could make it work to a limited degree but now I'm wondering if this is even possible?

Any maybe I should of said, "I've watched over 40 hours of c# tutorials, and have many years of coding in visual basic 6." I've also messed around with assembly, php, and a few other smaller languages. I've been out of it for awhile though and I'm looking for something more modern to learn. I'm not actually planning on using this proxy scanner to be honest; it just has a variety of objectives that I want to overcome.

Quote

It can't be a constant for this reason, and since the scanlist will be used through other methods in other .cs files I would like to find a way to make it stick. I'm sure I could make it work to a limited degree but now I'm wondering if this is even possible?

Couldn't find the edit button...

but what I meant to say was that I would like to make the array which will contain the scanlist to persist through all aspects of the application without having to create a new instance of the class each time. I've tried declaring the array within the class, as I know I can make the class static - non instanced -, but the IDE simply won't let me as demonstrated in my first post.
Was This Post Helpful? 0
  • +
  • -

#11 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Creating an array variable accessible from anywhere in the project

Posted 25 November 2013 - 08:46 PM

Make like 15
public static List<proxyScanlist> myScanlist = new List<proxyScanlist>();

After that read, parse and create a new proxyScanlist object, then use the .Add() method to add it to the list. Repeat until file done.

You can now access it with buildScanlist.myScanlist
Was This Post Helpful? 1
  • +
  • -

#12 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4490
  • View blog
  • Posts: 7,822
  • Joined: 08-June 10

Re: Creating an array variable accessible from anywhere in the project

Posted 26 November 2013 - 09:43 AM

I'd also suggest that if all you're doing is adding/removing, you make the list readonly. That means that you can't re-initialize the list anywhere else in your code, and if you accidentally try, you'll get a compile error.
Was This Post Helpful? 1
  • +
  • -

#13 vpdic  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 32
  • Joined: 24-November 13

Re: Creating an array variable accessible from anywhere in the project

Posted 26 November 2013 - 05:59 PM

I ultimately achieved what I needed to using various post, thank you all.

   public class proxyScanlist
    {
        public string IPAddress { get; set; }
        public int Port { get; set; }
        public byte Type { get; set; }
        public bool Verified { get; set; }
    }


    public static class Setup_Proxies
    {
        public static List<proxyScanlist> myScanlist = new List<proxyScanlist>();

        public static void createScanlist()
        {
            string fileLine;
            int spaceLocation;
            byte proxyCode; //declare variables for reading file
            StreamReader fileReader = new StreamReader("List\\Scanlist_01.txt");
            proxyScanlist generatedList = new proxyScanlist(); //i'll be populating this object then adding it to my list
            while ((fileLine = fileReader.ReadLine()) != null)
            {

                if (fileLine != null)
                {
                    spaceLocation = fileLine.IndexOf(" ");
                    if (spaceLocation > 3) fileLine = fileLine.Substring(0, spaceLocation);

                    if (fileLine.Length > 6)
                    {
                        spaceLocation = fileLine.IndexOf("@");
                        if (spaceLocation > 3)
                        {
                            switch (fileLine.ToLower().Substring(fileLine.Length-2))
                            {
                                case "tp": //http
                                    proxyCode = 0;
                                    break;
                                case "s4": //socks5 or s4
                                    proxyCode = 1;
                                    break;
                                case "s5": //socks5 or s5
                                    proxyCode = 2;
                                    break;
                                default:
                                    proxyCode = 3;
                                    break;
                            }
                            fileLine = fileLine.Substring(0, spaceLocation);
                        }
                            //determine the proxy type. if none specified set a default value which will be a global setting
                        else
                        {
                            proxyCode = 3; //was never specified, use default
                        }
                        //determine proxy code/type such as socks 4 socks 5 or http
                        //MessageBox.Show(proxyCode.ToString());
                        generatedList.IPAddress = fileLine;
                        generatedList.Port = 80;
                        generatedList.Type = proxyCode;
                        generatedList.Verified = false;
                        //set variables to structure
                        myScanlist.Add(generatedList);
                        //add to list here     
                    }
                }
                
            }

            fileReader.Close();
            //close the reader
           
            MessageBox.Show(String.Format ("IPAddress {0}, Port {1}, Proxy Type {2}.", myScanlist[0].IPAddress, myScanlist[0].Port.ToString(), myScanlist[0].Type.ToString()));
            MessageBox.Show(String.Format("IPAddress {0}, Port {1}, Proxy Type {2}.", myScanlist[1].IPAddress, myScanlist[1].Port.ToString(), myScanlist[1].Type.ToString()));
            //I havent parsed the proxy port from the ip but that shouldnt be much more work
        }

    }



I can create the scanlist anytime using...
Setup_Proxies.createScanlist();



and as mentioned call it anytime using...
Setup_Proxies.myScanlist[]



I've stumbled upon another problem where the same data is referenced at every index but I suspect that's because of how I put it in. I'm not creating a new object every time so even though I change the variables it's still referencing the same object. Something I learned in those 50 hours of videos ;)

I'll have to optimize this code later as it's atrocious but hey it works at the moment. Thank you all for your help.

PS: not seeing a marked as solved option?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1