Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

if (x ever equals y) C#

grimreeper132
 Share

Pretty simple to explain I use the same boolean value, ERROR4 in multiple places in the same function and if it ever changed from false to true I want the entire function to return false. I can obviously just check in several places to see if it is true then return true, but I will be using it in at least 15 diffrent places in the code. So is adding in at least 30 diffrent lines all doing the same check, so I'm looking for a way to do this which means I can get the entire function to return false.

 

Summary without waffle:

 

do this code

during the code if (x ever equals y)

   immediately return false

The owner of "too many" computers, called

The Lord of all Toasters (1920X 1080ti 32GB)

The Toasted Controller (i5 4670, R9 380, 24GB)

The Semi Portable Toastie machine (i7 3612QM (was an i3) intel HD 4000 16GB)'

Bread and Butter Pudding (i7 7700HQ, 1050ti, 16GB)

Pinoutbutter Sandwhich (raspberry pi 3 B)

The Portable Slice of Bread (N270, HAHAHA, 2GB)

Muffinator (C2D E6600, Geforce 8400, 6GB, 8X2TB HDD)

Toastbuster (WIP, should be cool)

loaf and let dough (A printer that doesn't print black ink)

The Cheese Toastie (C2D (of some sort), GTX 760, 3GB, win XP gaming machine)

The Toaster (C2D, intel HD, 4GB, 2X1TB NAS)

Matter of Loaf and death (some old shitty AMD laptop)

windybread (4X E5470, intel HD, 32GB ECC) (use coming soon, maybe)

And more, several more

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, grimreeper132 said:

Pretty simple to explain I use the same boolean value, ERROR4 in multiple places in the same function and if it ever changed from false to true I want the entire function to return false. I can obviously just check in several places to see if it is true then return true, but I will be using it in at least 15 diffrent places in the code. So is adding in at least 30 diffrent lines all doing the same check, so I'm looking for a way to do this which means I can get the entire function to return false.

 

Summary without waffle:

 

do this code

during the code if (x ever equals y)

   immediately return false

public bool Method1(string data)
{
    // this automatically exit the method with a false and doesn't execute the rest of the code in the method
	if(data == "something") return false;   
  
    // this automatically exit the method with a true and doesn't execute the rest of the code in the method
    if(data == "another thing") return true; 
    
}
    
   
      

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Franck said:

public bool Method1(string data)
{
    // this automatically exit the method with a false and doesn't execute the rest of the code in the method
	if(data == "something") return false;   
  
    // this automatically exit the method with a true and doesn't execute the rest of the code in the method
    if(data == "another thing") return true; 
    
}
    
   
      

 

The problem with that is I still need 15 lines to check the same thing so it's halved the lines, but as all the lines are doing the same thing I was wondering if theres a way to get it down further so it just returns false whenever ERROR4 = true

The owner of "too many" computers, called

The Lord of all Toasters (1920X 1080ti 32GB)

The Toasted Controller (i5 4670, R9 380, 24GB)

The Semi Portable Toastie machine (i7 3612QM (was an i3) intel HD 4000 16GB)'

Bread and Butter Pudding (i7 7700HQ, 1050ti, 16GB)

Pinoutbutter Sandwhich (raspberry pi 3 B)

The Portable Slice of Bread (N270, HAHAHA, 2GB)

Muffinator (C2D E6600, Geforce 8400, 6GB, 8X2TB HDD)

Toastbuster (WIP, should be cool)

loaf and let dough (A printer that doesn't print black ink)

The Cheese Toastie (C2D (of some sort), GTX 760, 3GB, win XP gaming machine)

The Toaster (C2D, intel HD, 4GB, 2X1TB NAS)

Matter of Loaf and death (some old shitty AMD laptop)

windybread (4X E5470, intel HD, 32GB ECC) (use coming soon, maybe)

And more, several more

Link to comment
Share on other sites

Link to post
Share on other sites

7 minutes ago, grimreeper132 said:

I was wondering if theres a way to get it down further so it just returns false whenever ERROR4 = true

I rarely use C#, but AFAIK there isn't a syntax that allows injecting some kind of boolean status listener into a function. You could set up some observer pattern thing to do that, but that seems a bit over-engineered.


One thing I wonder is maybe the answer is to simplify the logic in the function and break it up such that you don't need to evaluate a condition 15 separate times outside of a loop. It's hard to say though without seeing some code.

 

EDIT: and honestly you'd probably have to use exceptions to get the observer pattern idea to work, and that just makes it so much more jank. Though at the same time, what does ERROR4 represent? Maybe an exception is the correct course of action in the code, if the ERROR4 case is truly exceptional. In other words, don't use exceptions for control flow, but if it's something that you don't expect to happen then it might be a solution

Gaming build:

CPU: i7-7700k (5.0ghz, 1.312v)

GPU(s): Asus Strix 1080ti OC (~2063mhz)

Memory: 32GB (4x8) DDR4 G.Skill TridentZ RGB 3000mhz

Motherboard: Asus Prime z270-AR

PSU: Seasonic Prime Titanium 850W

Cooler: Custom water loop (420mm rad + 360mm rad)

Case: Be quiet! Dark base pro 900 (silver)
Primary storage: Samsung 960 evo m.2 SSD (500gb)

Secondary storage: Samsung 850 evo SSD (250gb)

 

Server build:

OS: Ubuntu server 16.04 LTS (though will probably upgrade to 17.04 for better ryzen support)

CPU: Ryzen R7 1700x

Memory: Ballistix Sport LT 16GB

Motherboard: Asrock B350 m4 pro

PSU: Corsair CX550M

Cooler: Cooler master hyper 212 evo

Storage: 2TB WD Red x1, 128gb OCZ SSD for OS

Case: HAF 932 adv

 

Link to comment
Share on other sites

Link to post
Share on other sites

Maybe make a separate function and then just call it every time you want to check.

Like "if check() return;"

I don't think you can do what you're trying to do with ever equals.

Maybe change your code so you don't have to check in 15 places.

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
Share on other sites

Link to post
Share on other sites

Why not initialize the return variable as false and only set it to true if one of the conditions is met? If it's a loop, just make it into a DO WHILE statement.

 

 

 

Gaming - AMD TR 3970X | ASUS ROG Zenith Extreme II | G.SKILL Neo 3600 64GB | Zotac Nvidia 2080 Ti AMP | 2x Sabarent 1TB NVMe | Samsung 860 EVO 1TB SSD | Phanteks Enthoo 719 | Seasonic Prime Ultra Platinum 1000w | Corsair K70 RGB Lux | Corsiar M65 | 2x ASUS Rog PG279Q | BenQ EW3270U | Windows 10 Pro | EKWB Custom loop

ITX - Intel i7-10700k | Asus ROG Z490-I Gaming | G.SKILL TridentZ RGB 3200 32GB | EVGA 2080 Super| Samsung 970 Evo 1TB | Samsung 860 Evo 1TB SSD | NZXT H1| Windows 10 Pro

HTPC - Intel i9-9900k | Asus ROG Maximus XI Code | G.SKILL TridentZ RGB 3200 32GB | EVGA 1070 | Samsung 970 1TB | WD Blue 1TB SSD | NZXT H700  | EVGA G3 1000W | Corsair H150i | Windows 10 Pro

Servers - SuperMicro 846 | 2x 2695L V2 | 128GB | Chelsio 10Gbe | Chelsio 40Gbe | 24 x 6TB | FreeNas - SuperMicro 826 | 2 x 2695L | 128GB | Chelsio 10Gbe | Chelsio 40Gbe | 8 x 10TB | 847 24 x 1TB SSD | Windows Server 2019

Work - Dell XPS 15 9560 | i7-7700HQ | 32 GB RAM | 1TB NVMe | 4k dsiplay

Link to comment
Share on other sites

Link to post
Share on other sites

Not sure on syntax but in the places where you change the value use a method..

public bool SetBool(bool myBool, bool newValue) {
    myBool = newValue;
    if(myBool == true) {
    	// event code
    }
}

 

If you're interested in a product please download and read the manual first.

Don't forget to tag or quote in your reply if you want me to know you've answered or have another question.

Link to comment
Share on other sites

Link to post
Share on other sites

32 minutes ago, grimreeper132 said:

The problem with that is I still need 15 lines to check the same thing so it's halved the lines, but as all the lines are doing the same thing I was wondering if theres a way to get it down further so it just returns false whenever ERROR4 = true

You cannot do it without increasing the complexity by ALOT. You need to create a task list that check the return and if the return is not what you expect then it cancel the subsequent execution and return right away. I do not have sample for that because it is very inefficient and bad coding and nothing will beat a simple if statement check.

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

23 minutes ago, Enderman said:

Maybe make a separate function and then just call it every time you want to check.

Like "if check() return;"

I don't think you can do what you're trying to do with ever equals.

Maybe change your code so you don't have to check in 15 places.

Thew code is doing 15 different things but the errors for the 15 things are all the same so I can't

 

19 minutes ago, Xplo1t said:

Why not initialize the return variable as false and only set it to true if one of the conditions is met? If it's a loop, just make it into a DO WHILE statement.

 

 

 

I need to get the code to exit as soon as possible as it is likely to get the code to crash if I don't so yea.

 

1 minute ago, Franck said:

You cannot do it without increasing the complexity by ALOT. You need to create a task list that check the return and if the return is not what you expect then it cancel the subsequent execution and return right away. I do not have sample for that because it is very inefficient and bad coding and nothing will beat a simple if statement check.

 

 

more and more I hear about this the more I think I agree with you of this just not working easily so yea

The owner of "too many" computers, called

The Lord of all Toasters (1920X 1080ti 32GB)

The Toasted Controller (i5 4670, R9 380, 24GB)

The Semi Portable Toastie machine (i7 3612QM (was an i3) intel HD 4000 16GB)'

Bread and Butter Pudding (i7 7700HQ, 1050ti, 16GB)

Pinoutbutter Sandwhich (raspberry pi 3 B)

The Portable Slice of Bread (N270, HAHAHA, 2GB)

Muffinator (C2D E6600, Geforce 8400, 6GB, 8X2TB HDD)

Toastbuster (WIP, should be cool)

loaf and let dough (A printer that doesn't print black ink)

The Cheese Toastie (C2D (of some sort), GTX 760, 3GB, win XP gaming machine)

The Toaster (C2D, intel HD, 4GB, 2X1TB NAS)

Matter of Loaf and death (some old shitty AMD laptop)

windybread (4X E5470, intel HD, 32GB ECC) (use coming soon, maybe)

And more, several more

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, grimreeper132 said:

Thew code is doing 15 different things

why though? This just sounds like a bloated method. More smaller simple methods > fewer bigger complex methods

Gaming build:

CPU: i7-7700k (5.0ghz, 1.312v)

GPU(s): Asus Strix 1080ti OC (~2063mhz)

Memory: 32GB (4x8) DDR4 G.Skill TridentZ RGB 3000mhz

Motherboard: Asus Prime z270-AR

PSU: Seasonic Prime Titanium 850W

Cooler: Custom water loop (420mm rad + 360mm rad)

Case: Be quiet! Dark base pro 900 (silver)
Primary storage: Samsung 960 evo m.2 SSD (500gb)

Secondary storage: Samsung 850 evo SSD (250gb)

 

Server build:

OS: Ubuntu server 16.04 LTS (though will probably upgrade to 17.04 for better ryzen support)

CPU: Ryzen R7 1700x

Memory: Ballistix Sport LT 16GB

Motherboard: Asrock B350 m4 pro

PSU: Corsair CX550M

Cooler: Cooler master hyper 212 evo

Storage: 2TB WD Red x1, 128gb OCZ SSD for OS

Case: HAF 932 adv

 

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, reniat said:

why though? This just sounds like a bloated method. Look up the single responsibility principle.

It might be bloated but this is the most efficient way of doing this

The owner of "too many" computers, called

The Lord of all Toasters (1920X 1080ti 32GB)

The Toasted Controller (i5 4670, R9 380, 24GB)

The Semi Portable Toastie machine (i7 3612QM (was an i3) intel HD 4000 16GB)'

Bread and Butter Pudding (i7 7700HQ, 1050ti, 16GB)

Pinoutbutter Sandwhich (raspberry pi 3 B)

The Portable Slice of Bread (N270, HAHAHA, 2GB)

Muffinator (C2D E6600, Geforce 8400, 6GB, 8X2TB HDD)

Toastbuster (WIP, should be cool)

loaf and let dough (A printer that doesn't print black ink)

The Cheese Toastie (C2D (of some sort), GTX 760, 3GB, win XP gaming machine)

The Toaster (C2D, intel HD, 4GB, 2X1TB NAS)

Matter of Loaf and death (some old shitty AMD laptop)

windybread (4X E5470, intel HD, 32GB ECC) (use coming soon, maybe)

And more, several more

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, grimreeper132 said:

It might be bloated but this is the most efficient way of doing this

I'm dubious about that lol, but it's hard to say without looking at the source.

Gaming build:

CPU: i7-7700k (5.0ghz, 1.312v)

GPU(s): Asus Strix 1080ti OC (~2063mhz)

Memory: 32GB (4x8) DDR4 G.Skill TridentZ RGB 3000mhz

Motherboard: Asus Prime z270-AR

PSU: Seasonic Prime Titanium 850W

Cooler: Custom water loop (420mm rad + 360mm rad)

Case: Be quiet! Dark base pro 900 (silver)
Primary storage: Samsung 960 evo m.2 SSD (500gb)

Secondary storage: Samsung 850 evo SSD (250gb)

 

Server build:

OS: Ubuntu server 16.04 LTS (though will probably upgrade to 17.04 for better ryzen support)

CPU: Ryzen R7 1700x

Memory: Ballistix Sport LT 16GB

Motherboard: Asrock B350 m4 pro

PSU: Corsair CX550M

Cooler: Cooler master hyper 212 evo

Storage: 2TB WD Red x1, 128gb OCZ SSD for OS

Case: HAF 932 adv

 

Link to comment
Share on other sites

Link to post
Share on other sites

9 minutes ago, grimreeper132 said:

Thew code is doing 15 different things but the errors for the 15 things are all the same so I can't

 

I need to get the code to exit as soon as possible as it is likely to get the code to crash if I don't so yea.

 

more and more I hear about this the more I think I agree with you of this just not working easily so yea

If your tests or whatever generate the error are method with the same return type and parameter then it would easily be possible with a loop on function object type and that would change everything to a literally 4 code lines (excluding brackets)

 

private bool ERROR4 = false;
private bool RepeatingMethod(string data)
{
	var functions = new List<Func<string, bool>>() { Test1, Test2, Test3, Test4 };

	foreach (var func in functions)
	{
		func(data);
		if (ERROR4) return true;
	}

	return false;
}

private bool Test1(string data) { ERROR4 = false; return false; }
private bool Test2(string data) { ERROR4 = false; return false; }
private bool Test3(string data) { ERROR4 = true; return false; }
private bool Test4(string data) { ERROR4 = false; return false; }

 

Link to comment
Share on other sites

Link to post
Share on other sites

7 minutes ago, grimreeper132 said:

Thew code is doing 15 different things but the errors for the 15 things are all the same so I can't

 

I need to get the code to exit as soon as possible as it is likely to get the code to crash if I don't so yea.

 

more and more I hear about this the more I think I agree with you of this just not working easily so yea

Then you're programming things badly.

Instead of checking repeatedly you should find the place where your variables can get modified to result in x==y and make it so that it won't happen or just return there.

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
Share on other sites

Link to post
Share on other sites

29 minutes ago, grimreeper132 said:

It might be bloated but this is the most efficient way of doing this

For writing down code, it isn't. The solution I saw there were you have loop checking some variable that gets set every time one of the functions gets called is cleaner code than just writing an if statement checking the variable every time you run one of the functions.

 

If anything, assuming the functions take no arguments, you could gather them up in an array and for-loop through them that way. Otherwise, a switch-case statement within a while-loop exiting whenever the "default" case hits  would do here.

 

But overall, it sounds to me more like there's an issue with the design of the application itself.

Link to comment
Share on other sites

Link to post
Share on other sites

6 hours ago, grimreeper132 said:

Pretty simple to explain I use the same boolean value, ERROR4 in multiple places in the same function and if it ever changed from false to true I want the entire function to return false. I can obviously just check in several places to see if it is true then return true, but I will be using it in at least 15 diffrent places in the code. So is adding in at least 30 diffrent lines all doing the same check, so I'm looking for a way to do this which means I can get the entire function to return false.

 

Summary without waffle:

 

do this code

during the code if (x ever equals y)

   immediately return false

Sounds like a perfect case to use exceptions.

Wrap the bool in a class of it's own so you have to go trough the class's interface to assign a value to it (in C++ you can overload the assignment operator to add some syntax sugar). Then have the interface function throw an exception whenever the wrapped bool is to be set.

Link to comment
Share on other sites

Link to post
Share on other sites

I mostly agree with what Unimportant said; use exceptions.

 

It is hard to tell whether or not it is the best use of exceptions, given that it would greatly depend on your code and the concept you are going for.  Given you are saying it is error4 as the variable, my guess is that exceptions would be more well suited.

 

Example:

//Error4 is defined somewhere in the progam
private bool TestMethod()
{
	try {
    	//Your code here
        
        //Instead of setting Error4 = true at this stage, just go
        throw new Error4Exception("Hello World");
        //Or better yet, replace all of Error4 in your code with throw new Error4Expection...that way you get a nice way to debug as well
    } catch (Error4Exception ex) {
    	Error4 = true;
    }
	if(Error4)
		return false;
        
    return true;
}

public class Error4Exception : Exception
{
   public Error4Exception(string message)
      : base(message)
   {
   }
}

 

3735928559 - Beware of the dead beef

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
 Share


×