Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Mod funtion in c#?

Posted on 2004-11-18
12
Medium Priority
?
63,271 Views
Last Modified: 2011-08-18

I used to use the mod function in asp to figure out the following while looping through rows in a dataset to display a matrix style grid of products:

if n mod 3 = 0 then
   //start a new row
esle
  //continue adding table cells

How can I acheive this same kind of thing in c# for my asp.net page? I'm displaying a list of products 3 across and so I want to know when I'm on every third one in my for each...

does that make sense? thanks in advance!
0
Comment
Question by:animated405
  • 3
  • 3
  • 2
  • +3
12 Comments
 
LVL 10

Accepted Solution

by:
NetworkArchitek earned 1000 total points
ID: 12622395
Hi animated405,
Use the % character.

Cheers!
0
 

Author Comment

by:animated405
ID: 12622411

thats what I thought but it didn't seem to do anything, is this syntax correct?

if((n mode 3) == 0)
   //do this
else
  //do that

sometimes I ask first and look into later so maybe its obvious and I'm too all over the place, million things going on at once...

thanks!
0
 

Author Comment

by:animated405
ID: 12622414
oops, I accidentally put mod in the above, I meant:

if((n % 3) == 0)
  // do this
else
  //do that
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 33

Expert Comment

by:sajuks
ID: 12622450
//this should work
   if(n%3==0)
        {
            //do this
        }
        else
        {
            //do that          
        }
0
 
LVL 8

Expert Comment

by:tomvergote
ID: 12622657
Why would   if(n%3==0) work better than if((n % 3) == 0)?

I get this result from a command window in visual studio
>? (4%2==0)
true
>? ((4%2)==0)
true
>
0
 
LVL 33

Expert Comment

by:sajuks
ID: 12622695
Both are the same, Do you rememeber the BODMAS rule-  bracketed sub expressions first, then multiplication and division left to right and then addition and subtraction left to right.
Thats the difference between your code and mine
0
 
LVL 8

Expert Comment

by:tomvergote
ID: 12624303
yes, but I meant this:

Comment from animated405

if((n % 3) == 0)
  // do this
else
  //do that

Comment from sajuks

//this should work
   if(n%3==0)
        {
            //do this
        }
        else
        {
            //do that          
        }

there is no difference, if you put brackets, the % gets executed first and then compared to 0, if you don't put brackets, the leftmost thus % gets executed first and then compared to 0.

I'm not solving any problem here but I was just pointing out that it should work with the brackets too
0
 
LVL 9

Expert Comment

by:msdixon
ID: 12624940
// i don't like thie first one below. i always add {} to my code blocks even if it's only one line and i can get away without them. it's also easier if you have to go back and add a second or third line within the block.

// also, explicitly adding the parenthesis is nonsense. it makes the code unreadable. the modulus will be evaluated prior to comparing it to zero. it's uneccessary. it's the same as:
// if (((3*4) + (5 + (6*4))) < 2)
// i over exaggerated for effect, but you see what i mean. if you have such a complex statement that you need a ton of grouping, it's time to re-evaluate the statement and use a more variables.

if((n % 3) == 0)
  // do this
else
  //do that

// this is what i prefer
// it's clean and simple

        if (n % 3 == 0)
        {
            //do this
        }
        else
        {
            //do that          
        }
0
 
LVL 8

Expert Comment

by:tomvergote
ID: 12624958
I'm not debating that it was cleaner or not, I was simply questioning why it would work better
0
 
LVL 9

Expert Comment

by:msdixon
ID: 12625181
but it wouldn't work better
0
 
LVL 85

Expert Comment

by:ozo
ID: 12633478
% takes precedence over == regardless of whether it is on the left or the right
0
 

Author Comment

by:animated405
ID: 12646649
I'm sorry I forgot about this. I was actually doing something else wrong, points to the first person that answered me. thanks again...
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Loops Section Overview

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question