Jump to content

C# - NullReferenceException was unhandled

AlTech
Go to solution Solved by squirrl,
Just now, AluminiumTech said:

That's not what fixed the issue though.........

 

I tried playing around and changed a few lines of code and I had no more issues. So idk what happened.

 

Ah well, If the issue wasn't in the code that you posted then there's no way we would have found it then. :P

For the life of me, I don't know what i'm doing wrong.

 

It's telling me that I've got a NullReferenceException. Which would mean that one of my variables has no value (is null) but that doesn't make any sense since all of mine have values.

 

 class OnTask
    {
        Dialog DialogBox = new Dialog();

        public string URL;
        public bool IsOnTask;


        public string[] OnTask_AllowedWebsites = new string[14];
        public string[] OnTask_NotAllowedWebsites = new string[22];
        public string[] OnTask_AllowedExtension = new string[9];

        //Check if the website is in the allowed list
        public void AllowedCheck()
        {
            OnTask_AllowedWebsites[0] = "onedrive.com";
            OnTask_AllowedWebsites[1] = "google.com";
            OnTask_AllowedWebsites[2] = "icloud.com";
            OnTask_AllowedWebsites[3] = ".edu";
            OnTask_AllowedWebsites[4] = ".org";
            OnTask_AllowedWebsites[5] = "youtube.com";
            OnTask_AllowedWebsites[6] = "dropbox.com";
            OnTask_AllowedWebsites[7] = "microsoft.com";
            OnTask_AllowedWebsites[8] = "bing.com";
            OnTask_AllowedWebsites[9] = "gmail.com";
            OnTask_AllowedWebsites[10] = "linkedin.com";
            OnTask_AllowedWebsites[11] = "stackoverflow.com";
            OnTask_AllowedWebsites[12] = "slack.com";

            if (URL == OnTask_AllowedWebsites[0])
            {
                IsOnTask = true;
            }
            if (URL == OnTask_AllowedWebsites[1])
            {
                IsOnTask = true;
            }
            if (URL == OnTask_AllowedWebsites[2])
            {
                IsOnTask = true;

            }
            if (URL == OnTask_AllowedWebsites[3])
            {
                IsOnTask = true;
            }
            if (URL == OnTask_AllowedWebsites[4])
            {
                IsOnTask = true;
            }
            if (URL == OnTask_AllowedWebsites[5])
            {
                IsOnTask = true;

            }
            if (URL == OnTask_AllowedWebsites[6])
            {
                IsOnTask = true;
            }
            if (URL == OnTask_AllowedWebsites[7])
            {
                IsOnTask = true;
            }
            if (URL == OnTask_AllowedWebsites[8])
            {
                IsOnTask = true;

            }
            if (URL == OnTask_AllowedWebsites[9])
            {
                IsOnTask = true;
            }
            if (URL == OnTask_AllowedWebsites[10])
            {
                IsOnTask = true;
            }
            if (URL == OnTask_AllowedWebsites[11])
            {
                IsOnTask = true;
            }
            if (URL == OnTask_AllowedWebsites[12])
            {
                IsOnTask = true;
            }
            if (URL == OnTask_AllowedWebsites[13])
            {
                IsOnTask = true;
            }
        }
        public void NotAllowedCheck()
        {
            OnTask_NotAllowedWebsites[0] = "steampowered.com";
            OnTask_NotAllowedWebsites[1] = "hub";
            OnTask_NotAllowedWebsites[2] = "amazon.com";
            OnTask_NotAllowedWebsites[3] = "ebay.com";
            OnTask_NotAllowedWebsites[4] = "spotify.com";
            OnTask_NotAllowedWebsites[5] = "linustechtips.com";
            OnTask_NotAllowedWebsites[6] = "apple.com";
            OnTask_NotAllowedWebsites[7] = "xbox.com";
            OnTask_NotAllowedWebsites[8] = "wccftech.com";
            OnTask_NotAllowedWebsites[9] = "fake";
            OnTask_NotAllowedWebsites[10] = ".con";
            OnTask_NotAllowedWebsites[11] = ".cm";
            OnTask_NotAllowedWebsites[12] = "skype.com";
            OnTask_NotAllowedWebsites[13] = "ad";
            OnTask_NotAllowedWebsites[14] = "game";
            OnTask_NotAllowedWebsites[15] = "facebook.com";
            OnTask_NotAllowedWebsites[16] = "instagram.com";
            OnTask_NotAllowedWebsites[17] = "twitter.com";
            OnTask_NotAllowedWebsites[18] = "wikipedia.org";
            OnTask_NotAllowedWebsites[19] = "yahoo.com";
            OnTask_NotAllowedWebsites[20] = "tube";
            OnTask_NotAllowedWebsites[21] = "red";

            if (URL == OnTask_NotAllowedWebsites[0])
            {
                IsOnTask = false;
            }
            else if (URL == OnTask_NotAllowedWebsites[1])
            {
                IsOnTask = false;
            }
            else if (URL == OnTask_NotAllowedWebsites[2])
            {
                IsOnTask = false;
            }
           else if (URL == OnTask_NotAllowedWebsites[3])
            {
                IsOnTask = false;

            }
            else if (URL == OnTask_NotAllowedWebsites[4])
            {
                IsOnTask = false;
            }
            else if (URL == OnTask_NotAllowedWebsites[5])
            {
                IsOnTask = false;
            }
           else if (URL == OnTask_NotAllowedWebsites[6])
            {
                IsOnTask = false;
            }
           else if (URL == OnTask_NotAllowedWebsites[7])
            {
                IsOnTask = false;
            }
           else if (URL == OnTask_NotAllowedWebsites[8])
            {
                IsOnTask = false;
            }
           else if (URL == OnTask_NotAllowedWebsites[9])
            {
                IsOnTask = false;
            }
           else if (URL == OnTask_NotAllowedWebsites[10])
            {
                IsOnTask = false;
            }
           else if (URL == OnTask_NotAllowedWebsites[11])
            {
                IsOnTask = false;
            }
           else if (URL == OnTask_NotAllowedWebsites[12])
            {
                IsOnTask = false;
            }
           else if (URL == OnTask_NotAllowedWebsites[13])
            {
                IsOnTask = false;
            }
           else if (URL == OnTask_NotAllowedWebsites[14])
            {
                IsOnTask = false;
            }
           else if (URL == OnTask_NotAllowedWebsites[15])
            {
                IsOnTask = false;

            }
           else if (URL == OnTask_NotAllowedWebsites[16])
            {
                IsOnTask = false;
            }
           else if (URL == OnTask_NotAllowedWebsites[17])
            {
                IsOnTask = false;

            }
           else if (URL == OnTask_NotAllowedWebsites[18])
            {
                IsOnTask = false;
            }
           else if (URL == OnTask_NotAllowedWebsites[19])
            {
                IsOnTask = false;
            }
            else if (URL == OnTask_NotAllowedWebsites[20])
            {
                IsOnTask = false;
            }
            else if (URL == OnTask_NotAllowedWebsites[21])
            {
                IsOnTask = false;
            }
        } 
        public void AllowedExtension()
        {
            OnTask_AllowedExtension[0] = ".edu";
            OnTask_AllowedExtension[1] = ".org";
            OnTask_AllowedExtension[2] = ".com";
            OnTask_AllowedExtension[3] = ".lithium";
            OnTask_AllowedExtension[4] = ".html";
            OnTask_AllowedExtension[5] = ".com";
            OnTask_AllowedExtension[6] = ".info";
            OnTask_AllowedExtension[7] = ".net";
            OnTask_AllowedExtension[8] = ".aspx";

            if (URL.EndsWith(OnTask_AllowedExtension[0]))
            {
                IsOnTask = true;
            }
            else if (URL.EndsWith(OnTask_AllowedExtension[1]))
            {
                IsOnTask = true;
            }
            else if (URL.EndsWith(OnTask_AllowedExtension[2]))
            {
                IsOnTask = true;
            }
            else if (URL.EndsWith(OnTask_AllowedExtension[3]))
            {
                IsOnTask = true;
            }
            else if (URL.EndsWith(OnTask_AllowedExtension[4]))
            {
                IsOnTask = true;
            }
            else if (URL.EndsWith(OnTask_AllowedExtension[5]))
            {
                IsOnTask = true;
            }
            else if (URL.EndsWith(OnTask_AllowedExtension[6]))
            {
                IsOnTask = true;
            }
            else if (URL.EndsWith(OnTask_AllowedExtension[7]))
            {
                IsOnTask = true;
            }
            else if (URL.EndsWith(OnTask_AllowedExtension[8]))
            {
                IsOnTask = true;
            }
            else
            {
                //Do nothing if we haven't picked up any bad URL exxtensions
            }
        }
        public void OnTask_TempNavigate()
        {
            
            //Check to see if the website we are visiting is Not Allowed
            NotAllowedCheck();

            //Check to see if the website we are visiting is Allowed
            AllowedCheck();

            //Check to see if the extension is allowed
            AllowedExtension();
        }
    }

Once this is run from an external class it returns a value in the form of a bool "IsOnTask".

 

Thanks guys :).

Judge a product on its own merits AND the company that made it.

How to setup MSI Afterburner OSD | How to make your AMD Radeon GPU more efficient with Radeon Chill | (Probably) Why LMG Merch shipping to the EU is expensive

Oneplus 6 (Early 2023 to present) | HP Envy 15" x360 R7 5700U (Mid 2021 to present) | Steam Deck (Late 2022 to present)

 

Mid 2023 AlTech Desktop Refresh - AMD R7 5800X (Mid 2023), XFX Radeon RX 6700XT MBA (Mid 2021), MSI X370 Gaming Pro Carbon (Early 2018), 32GB DDR4-3200 (16GB x2) (Mid 2022

Noctua NH-D15 (Early 2021), Corsair MP510 1.92TB NVMe SSD (Mid 2020), beQuiet Pure Wings 2 140mm x2 & 120mm x1 (Mid 2023),

Link to comment
Share on other sites

Link to post
Share on other sites

Sorry but I have to ask; it's really been bothering me - why do you use a ton of if statements instead of a loop to check the allowed websites array?

Solve your own audio issues  |  First Steps with RPi 3  |  Humidity & Condensation  |  Sleep & Hibernation  |  Overclocking RAM  |  Making Backups  |  Displays  |  4K / 8K / 16K / etc.  |  Do I need 80+ Platinum?

If you can read this you're using the wrong theme.  You can change it at the bottom.

Link to comment
Share on other sites

Link to post
Share on other sites

I suggest optimizing the code first like Ryan said, you're just making it harder for yourself if you ask me.

 

I'd help you but I'm at work and coding on phone.. please no

 

Link to comment
Share on other sites

Link to post
Share on other sites

8 minutes ago, Ryan_Vickers said:

Sorry but I have to ask; it's really been bothering me - why do you use a ton of if statements instead of a loop to check the allowed websites array?

 
 

Even a loop is more than you need. indexOf will work just fine for this.

 

I'm also pretty confused the way this is going about things. Usually, you have a whitelist, and only allow things on the whitelist, a blacklist and allow everything except things from the blacklist. here you have both, but also 2 whitelists. 

From the look of things, the last check renders the first 2 useless as well. You say facebook.com is off task, then say anything that ends with .com is on task. 

Aragorn (WS): 250D | 6800k | 840 Pro 512GB | Intel 530 480GB  | Asus X99-M WS | 64GB DDR4 | Corsair HX720i | GTX 1070 | Corsair H115i | Philips BDM4350UC 43" 3840x2160 IPS

Gimli (server):  Node 304 | G4560 | ADATA XPG SX8000 128GB | 2x 5TB WD Red | ASROCK H270M-ITX/AC  | 8GB DDR4 | Seasonic 400FL

 Omega (server):                 Fractal Arc Mini R2 | i3 4130 | 500GB Maxtor | 2TB WD Red : Raid 1 | 3TB Seagate Barracuda | 16GB RAM | Seasonic G-450w
Alpha (WS): 900D | 4770k | GTX 780  | 840 Pro 512GB  | GA-Z87X-OC | Corsair RM 850 | 24GB 2400mhz | Samsung S27B970D 2560x1440

                              ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, squirrl said:

Even a loop is more than you need. indexOf will work just fine for this.

True, although I think Contains would be better in this use case.

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, madknight3 said:

-

8 minutes ago, squirrl said:

-

10 minutes ago, ModuleLFS said:

-

 

oh no I knew this would happen... we've gone completely off topic xD 

Do any of you know how to solve the actual problem?

 

(I'm sorry I brought up the loop thing, I knew it was a bad idea xD full copy.png)

Solve your own audio issues  |  First Steps with RPi 3  |  Humidity & Condensation  |  Sleep & Hibernation  |  Overclocking RAM  |  Making Backups  |  Displays  |  4K / 8K / 16K / etc.  |  Do I need 80+ Platinum?

If you can read this you're using the wrong theme.  You can change it at the bottom.

Link to comment
Share on other sites

Link to post
Share on other sites

Anyway, as to @AluminiumTech's original question

 

if (URL == OnTask_AllowedWebsites[13])
            {
                IsOnTask = true;
            }

You never assign anything to [13] so you do have null variables.

Aragorn (WS): 250D | 6800k | 840 Pro 512GB | Intel 530 480GB  | Asus X99-M WS | 64GB DDR4 | Corsair HX720i | GTX 1070 | Corsair H115i | Philips BDM4350UC 43" 3840x2160 IPS

Gimli (server):  Node 304 | G4560 | ADATA XPG SX8000 128GB | 2x 5TB WD Red | ASROCK H270M-ITX/AC  | 8GB DDR4 | Seasonic 400FL

 Omega (server):                 Fractal Arc Mini R2 | i3 4130 | 500GB Maxtor | 2TB WD Red : Raid 1 | 3TB Seagate Barracuda | 16GB RAM | Seasonic G-450w
Alpha (WS): 900D | 4770k | GTX 780  | 840 Pro 512GB  | GA-Z87X-OC | Corsair RM 850 | 24GB 2400mhz | Samsung S27B970D 2560x1440

                              ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

Link to comment
Share on other sites

Link to post
Share on other sites

30 minutes ago, Ryan_Vickers said:

 

oh no I knew this would happen... we've gone completely off topic xD 

Do any of you know how to solve the actual problem?

 

(I'm sorry I brought up the loop thing, I knew it was a bad idea xD full copy.png)

Not completely true. Changing how he did the lookup would have also fixed the original issue that squirrl just pointed out.

 

edit: My mistake, it's fine for string[] to contain null and it's also fine to compare strings against null so this isn't the issue. It's possible the OP could use OnTask_AllowedWebsites[13] in another place and trigger a NullReferenceException though.

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, madknight3 said:

Not completely true. Changing how he did the lookup would have also fixed the original issue that @squirrl just pointed out.

hm, I suppose that's true if that really is the issue...

@AluminiumTech did he solve it? :D

Solve your own audio issues  |  First Steps with RPi 3  |  Humidity & Condensation  |  Sleep & Hibernation  |  Overclocking RAM  |  Making Backups  |  Displays  |  4K / 8K / 16K / etc.  |  Do I need 80+ Platinum?

If you can read this you're using the wrong theme.  You can change it at the bottom.

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, madknight3 said:

True, although I think Contains would be better in this use case.

 

 

 
 
 
 

There is no Array.Contains method. (You're probably thinking of string.contains)

IndexOf would just be 

if  (OnTaskNotAllowedWebsites.IndexOf(URL) != -1) {
// URL matches one of the websites
}

 

Aragorn (WS): 250D | 6800k | 840 Pro 512GB | Intel 530 480GB  | Asus X99-M WS | 64GB DDR4 | Corsair HX720i | GTX 1070 | Corsair H115i | Philips BDM4350UC 43" 3840x2160 IPS

Gimli (server):  Node 304 | G4560 | ADATA XPG SX8000 128GB | 2x 5TB WD Red | ASROCK H270M-ITX/AC  | 8GB DDR4 | Seasonic 400FL

 Omega (server):                 Fractal Arc Mini R2 | i3 4130 | 500GB Maxtor | 2TB WD Red : Raid 1 | 3TB Seagate Barracuda | 16GB RAM | Seasonic G-450w
Alpha (WS): 900D | 4770k | GTX 780  | 840 Pro 512GB  | GA-Z87X-OC | Corsair RM 850 | 24GB 2400mhz | Samsung S27B970D 2560x1440

                              ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

Link to comment
Share on other sites

Link to post
Share on other sites

10 minutes ago, squirrl said:

Anyway, as to @AluminiumTech's original question

 


if (URL == OnTask_AllowedWebsites[13])
            {
                IsOnTask = true;
            }

You never assign anything to [13] so you do have null variables.

That's not what fixed the issue though.........

 

I tried playing around and changed a few lines of code and I had no more issues. So idk what happened.

Judge a product on its own merits AND the company that made it.

How to setup MSI Afterburner OSD | How to make your AMD Radeon GPU more efficient with Radeon Chill | (Probably) Why LMG Merch shipping to the EU is expensive

Oneplus 6 (Early 2023 to present) | HP Envy 15" x360 R7 5700U (Mid 2021 to present) | Steam Deck (Late 2022 to present)

 

Mid 2023 AlTech Desktop Refresh - AMD R7 5800X (Mid 2023), XFX Radeon RX 6700XT MBA (Mid 2021), MSI X370 Gaming Pro Carbon (Early 2018), 32GB DDR4-3200 (16GB x2) (Mid 2022

Noctua NH-D15 (Early 2021), Corsair MP510 1.92TB NVMe SSD (Mid 2020), beQuiet Pure Wings 2 140mm x2 & 120mm x1 (Mid 2023),

Link to comment
Share on other sites

Link to post
Share on other sites

7 minutes ago, Ryan_Vickers said:

hm, I suppose that's true if that really is the issue...

@AluminiumTech did he solve it? :D

IDK.

 

 

I changed something else and it worked.

 

I changed this and it worked.

     ontask.URL = text.Text;

                    CheckSecurity();

                    ///Check to see if it is allowed or not.
                    ontask.OnTask_TempNavigate();

                    if(ontask.IsOnTask == true)
                    {
                        TempNavigate();
                    }
                    else if(ontask.IsOnTask == false)
                    {
                        //Tell the user to stay on task if they attempt to navigate to a URL which is not allowed.
                        dialog.StayOnTask();
                    }

 

I also removed the unused value just now and I will report back on results.

 

14 minutes ago, Ryan_Vickers said:

 

oh no I knew this would happen... we've gone completely off topic xD 

Do any of you know how to solve the actual problem?

 

(I'm sorry I brought up the loop thing, I knew it was a bad idea xD full copy.png)

No no. i'd like suggestions on how to do it as a loop since the problem may have been fixed.

Judge a product on its own merits AND the company that made it.

How to setup MSI Afterburner OSD | How to make your AMD Radeon GPU more efficient with Radeon Chill | (Probably) Why LMG Merch shipping to the EU is expensive

Oneplus 6 (Early 2023 to present) | HP Envy 15" x360 R7 5700U (Mid 2021 to present) | Steam Deck (Late 2022 to present)

 

Mid 2023 AlTech Desktop Refresh - AMD R7 5800X (Mid 2023), XFX Radeon RX 6700XT MBA (Mid 2021), MSI X370 Gaming Pro Carbon (Early 2018), 32GB DDR4-3200 (16GB x2) (Mid 2022

Noctua NH-D15 (Early 2021), Corsair MP510 1.92TB NVMe SSD (Mid 2020), beQuiet Pure Wings 2 140mm x2 & 120mm x1 (Mid 2023),

Link to comment
Share on other sites

Link to post
Share on other sites

13 minutes ago, squirrl said:

There is no Array.Contains method. (You're probably thinking of string.contains)

IndexOf would just be 


if  (OnTaskNotAllowedWebsites.IndexOf(URL) != -1) {
// URL matches one of the websites
}

 

You can use Contains on arrays as well. I tried it before posting to make sure.

string[] s = {"hello", "there"};
s.Contains("hello"); // returns true
s.Contains("he"); // returns false

Note this works for all primitive types, not just string.

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, AluminiumTech said:

That's not what fixed the issue though.........

 

I tried playing around and changed a few lines of code and I had no more issues. So idk what happened.

 

Ah well, If the issue wasn't in the code that you posted then there's no way we would have found it then. :P

Aragorn (WS): 250D | 6800k | 840 Pro 512GB | Intel 530 480GB  | Asus X99-M WS | 64GB DDR4 | Corsair HX720i | GTX 1070 | Corsair H115i | Philips BDM4350UC 43" 3840x2160 IPS

Gimli (server):  Node 304 | G4560 | ADATA XPG SX8000 128GB | 2x 5TB WD Red | ASROCK H270M-ITX/AC  | 8GB DDR4 | Seasonic 400FL

 Omega (server):                 Fractal Arc Mini R2 | i3 4130 | 500GB Maxtor | 2TB WD Red : Raid 1 | 3TB Seagate Barracuda | 16GB RAM | Seasonic G-450w
Alpha (WS): 900D | 4770k | GTX 780  | 840 Pro 512GB  | GA-Z87X-OC | Corsair RM 850 | 24GB 2400mhz | Samsung S27B970D 2560x1440

                              ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, squirrl said:

Ah well, If the issue wasn't in the code that you posted then there's no way we would have found it then. :P

No no. It was in the code I posted.

 

It just wasn't what I expected it to be. But how can I make a more efficient process of checking against the URL?

 

Judge a product on its own merits AND the company that made it.

How to setup MSI Afterburner OSD | How to make your AMD Radeon GPU more efficient with Radeon Chill | (Probably) Why LMG Merch shipping to the EU is expensive

Oneplus 6 (Early 2023 to present) | HP Envy 15" x360 R7 5700U (Mid 2021 to present) | Steam Deck (Late 2022 to present)

 

Mid 2023 AlTech Desktop Refresh - AMD R7 5800X (Mid 2023), XFX Radeon RX 6700XT MBA (Mid 2021), MSI X370 Gaming Pro Carbon (Early 2018), 32GB DDR4-3200 (16GB x2) (Mid 2022

Noctua NH-D15 (Early 2021), Corsair MP510 1.92TB NVMe SSD (Mid 2020), beQuiet Pure Wings 2 140mm x2 & 120mm x1 (Mid 2023),

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, AluminiumTech said:

No no. It was in the code I posted.

 

It just wasn't what I expected it to be. But how can I make this a more efficient process of checking?

 

 

Look above. You can use array.indexOf or array.contains

Aragorn (WS): 250D | 6800k | 840 Pro 512GB | Intel 530 480GB  | Asus X99-M WS | 64GB DDR4 | Corsair HX720i | GTX 1070 | Corsair H115i | Philips BDM4350UC 43" 3840x2160 IPS

Gimli (server):  Node 304 | G4560 | ADATA XPG SX8000 128GB | 2x 5TB WD Red | ASROCK H270M-ITX/AC  | 8GB DDR4 | Seasonic 400FL

 Omega (server):                 Fractal Arc Mini R2 | i3 4130 | 500GB Maxtor | 2TB WD Red : Raid 1 | 3TB Seagate Barracuda | 16GB RAM | Seasonic G-450w
Alpha (WS): 900D | 4770k | GTX 780  | 840 Pro 512GB  | GA-Z87X-OC | Corsair RM 850 | 24GB 2400mhz | Samsung S27B970D 2560x1440

                              ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, AluminiumTech said:

No no. It was in the code I posted.

 

It just wasn't what I expected it to be. But how can I make this a more efficient process of checking?

 

 

The code you posted just then isn't in the OP. :P

Aragorn (WS): 250D | 6800k | 840 Pro 512GB | Intel 530 480GB  | Asus X99-M WS | 64GB DDR4 | Corsair HX720i | GTX 1070 | Corsair H115i | Philips BDM4350UC 43" 3840x2160 IPS

Gimli (server):  Node 304 | G4560 | ADATA XPG SX8000 128GB | 2x 5TB WD Red | ASROCK H270M-ITX/AC  | 8GB DDR4 | Seasonic 400FL

 Omega (server):                 Fractal Arc Mini R2 | i3 4130 | 500GB Maxtor | 2TB WD Red : Raid 1 | 3TB Seagate Barracuda | 16GB RAM | Seasonic G-450w
Alpha (WS): 900D | 4770k | GTX 780  | 840 Pro 512GB  | GA-Z87X-OC | Corsair RM 850 | 24GB 2400mhz | Samsung S27B970D 2560x1440

                              ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, squirrl said:

The code you posted just then isn't in the OP. :P

Ohhhhhhhhhhhhhh. My bad xD.

 

It was part of the main section of the program.

 

1 minute ago, squirrl said:

Look above. You can use array.indexOf or array.contains

But wouldn't it mean it would use as many if statements as right now?

Judge a product on its own merits AND the company that made it.

How to setup MSI Afterburner OSD | How to make your AMD Radeon GPU more efficient with Radeon Chill | (Probably) Why LMG Merch shipping to the EU is expensive

Oneplus 6 (Early 2023 to present) | HP Envy 15" x360 R7 5700U (Mid 2021 to present) | Steam Deck (Late 2022 to present)

 

Mid 2023 AlTech Desktop Refresh - AMD R7 5800X (Mid 2023), XFX Radeon RX 6700XT MBA (Mid 2021), MSI X370 Gaming Pro Carbon (Early 2018), 32GB DDR4-3200 (16GB x2) (Mid 2022

Noctua NH-D15 (Early 2021), Corsair MP510 1.92TB NVMe SSD (Mid 2020), beQuiet Pure Wings 2 140mm x2 & 120mm x1 (Mid 2023),

Link to comment
Share on other sites

Link to post
Share on other sites

9 minutes ago, AluminiumTech said:

No no. i'd like suggestions on how to do it as a loop since the problem may have been fixed.

Oh good I'm glad this turned out well in the end :P 

Solve your own audio issues  |  First Steps with RPi 3  |  Humidity & Condensation  |  Sleep & Hibernation  |  Overclocking RAM  |  Making Backups  |  Displays  |  4K / 8K / 16K / etc.  |  Do I need 80+ Platinum?

If you can read this you're using the wrong theme.  You can change it at the bottom.

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Ryan_Vickers said:

Oh good I'm glad this turned out well in the end :P 

What was your loop idea or whatever to make it more clean and less if statements?

Judge a product on its own merits AND the company that made it.

How to setup MSI Afterburner OSD | How to make your AMD Radeon GPU more efficient with Radeon Chill | (Probably) Why LMG Merch shipping to the EU is expensive

Oneplus 6 (Early 2023 to present) | HP Envy 15" x360 R7 5700U (Mid 2021 to present) | Steam Deck (Late 2022 to present)

 

Mid 2023 AlTech Desktop Refresh - AMD R7 5800X (Mid 2023), XFX Radeon RX 6700XT MBA (Mid 2021), MSI X370 Gaming Pro Carbon (Early 2018), 32GB DDR4-3200 (16GB x2) (Mid 2022

Noctua NH-D15 (Early 2021), Corsair MP510 1.92TB NVMe SSD (Mid 2020), beQuiet Pure Wings 2 140mm x2 & 120mm x1 (Mid 2023),

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, AluminiumTech said:

 

But wouldn't it mean it would use as many if statements as right now?

 

Nope, since you're checking if the whole array contains the URL rather than checking if the URL exists in each individual position in the array.

You will end up with 3 if statements using the same logic as you have now.

I do think the logic needs modifying, though. If a website isn't on either list, is it supposed to be on task or not? If it's not on task then you don't need the blacklist, if it's on task then you don't need the whitelist.

And like i mentioned, the whole blacklist becomes useless when you overwrite the onTask variable when you have an allowed extenstion like.com which also appears on sites in the blacklist.

Aragorn (WS): 250D | 6800k | 840 Pro 512GB | Intel 530 480GB  | Asus X99-M WS | 64GB DDR4 | Corsair HX720i | GTX 1070 | Corsair H115i | Philips BDM4350UC 43" 3840x2160 IPS

Gimli (server):  Node 304 | G4560 | ADATA XPG SX8000 128GB | 2x 5TB WD Red | ASROCK H270M-ITX/AC  | 8GB DDR4 | Seasonic 400FL

 Omega (server):                 Fractal Arc Mini R2 | i3 4130 | 500GB Maxtor | 2TB WD Red : Raid 1 | 3TB Seagate Barracuda | 16GB RAM | Seasonic G-450w
Alpha (WS): 900D | 4770k | GTX 780  | 840 Pro 512GB  | GA-Z87X-OC | Corsair RM 850 | 24GB 2400mhz | Samsung S27B970D 2560x1440

                              ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, AluminiumTech said:

What was your loop idea or whatever to make it more clean and less if statements?

I would do something like this: (keep in mind I know nothing of C#)

 

for (int i = 0; i < 12; i++) {

if (URL == OnTask_AllowedWebsites[ i ]) {

IsOnTask = true;

}

}

 

I apologize for the formatting but it's currently taking every ounce of self control to not go off on a mad rant about the code editor

Solve your own audio issues  |  First Steps with RPi 3  |  Humidity & Condensation  |  Sleep & Hibernation  |  Overclocking RAM  |  Making Backups  |  Displays  |  4K / 8K / 16K / etc.  |  Do I need 80+ Platinum?

If you can read this you're using the wrong theme.  You can change it at the bottom.

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, squirrl said:

Nope, since you're checking if the whole array contains the URL rather than checking if the URL exists in each individual position in the array.

You will end up with 3 if statements using the same logic as you have now.

I do think the logic needs modifying, though. If a website isn't on either list, is it supposed to be on task or not? If it's not on task then you don't need the blacklist, if it's on task then you don't need the whitelist.

And like i mentioned, the whole blacklist becomes useless when you overwrite the onTask variable when you have an allowed extenstion like.com which also appears on sites in the blacklist.

So it would look something like this?

if(OnTask_AllowedExtension.Contains(URL))
   {
//Do something here
   }
else if(OnTask_NotAllowedWebsites.Contains(URL))
{
  //Do something here
}
else if(OnTask_AllowedWebsites.Contains(URL))
{
  //Do something here
}

 

Judge a product on its own merits AND the company that made it.

How to setup MSI Afterburner OSD | How to make your AMD Radeon GPU more efficient with Radeon Chill | (Probably) Why LMG Merch shipping to the EU is expensive

Oneplus 6 (Early 2023 to present) | HP Envy 15" x360 R7 5700U (Mid 2021 to present) | Steam Deck (Late 2022 to present)

 

Mid 2023 AlTech Desktop Refresh - AMD R7 5800X (Mid 2023), XFX Radeon RX 6700XT MBA (Mid 2021), MSI X370 Gaming Pro Carbon (Early 2018), 32GB DDR4-3200 (16GB x2) (Mid 2022

Noctua NH-D15 (Early 2021), Corsair MP510 1.92TB NVMe SSD (Mid 2020), beQuiet Pure Wings 2 140mm x2 & 120mm x1 (Mid 2023),

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, AluminiumTech said:

So it would look something like this?

 

 
 
 

Yeah, pretty much. Now that it's clearer i can see you're using elseif so forget what I said before about the variable getting overwritten. The order still matters though. I'm sure the way you have it there it will allow every website with a .com extension. Surely you will check allowed Websites first, then Not Allowed, then Extensions?  

Aragorn (WS): 250D | 6800k | 840 Pro 512GB | Intel 530 480GB  | Asus X99-M WS | 64GB DDR4 | Corsair HX720i | GTX 1070 | Corsair H115i | Philips BDM4350UC 43" 3840x2160 IPS

Gimli (server):  Node 304 | G4560 | ADATA XPG SX8000 128GB | 2x 5TB WD Red | ASROCK H270M-ITX/AC  | 8GB DDR4 | Seasonic 400FL

 Omega (server):                 Fractal Arc Mini R2 | i3 4130 | 500GB Maxtor | 2TB WD Red : Raid 1 | 3TB Seagate Barracuda | 16GB RAM | Seasonic G-450w
Alpha (WS): 900D | 4770k | GTX 780  | 840 Pro 512GB  | GA-Z87X-OC | Corsair RM 850 | 24GB 2400mhz | Samsung S27B970D 2560x1440

                              ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, AluminiumTech said:

So it would look something like this?


if(OnTask_AllowedExtension.Contains(URL))
   {
//Do something here
   }
else if(OnTask_NotAllowedWebsites.Contains(URL))
{
  //Do something here
}
else if(OnTask_AllowedWebsites.Contains(URL))
{
  //Do something here
}

 

or actually I think I can simplify that further.

 

if(OnTask_AllowedExtension.Contains(URL) & OnTask_AllowedWebsites.Contains(URL))
   {
//Do something here
   }
else if(OnTask_NotAllowedWebsites.Contains(URL))
{
  //Do something here
}

 

Judge a product on its own merits AND the company that made it.

How to setup MSI Afterburner OSD | How to make your AMD Radeon GPU more efficient with Radeon Chill | (Probably) Why LMG Merch shipping to the EU is expensive

Oneplus 6 (Early 2023 to present) | HP Envy 15" x360 R7 5700U (Mid 2021 to present) | Steam Deck (Late 2022 to present)

 

Mid 2023 AlTech Desktop Refresh - AMD R7 5800X (Mid 2023), XFX Radeon RX 6700XT MBA (Mid 2021), MSI X370 Gaming Pro Carbon (Early 2018), 32GB DDR4-3200 (16GB x2) (Mid 2022

Noctua NH-D15 (Early 2021), Corsair MP510 1.92TB NVMe SSD (Mid 2020), beQuiet Pure Wings 2 140mm x2 & 120mm x1 (Mid 2023),

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×