CSharp/C# >> Define String

by shapper » Thu, 04 Sep 2008 04:06:51 GMT

Hello,

I want to define a string value in a short way, if possible, as
follows:

Weight = "Light" if Count < 10, "Normal" if Count >= 10 and < 20,
"Heavy" if Count >= 20

How can I do this?

Thanks,
Miguel

CSharp/C# >> Define String

by Jon Skeet [C# MVP] » Thu, 04 Sep 2008 04:18:01 GMT



weight = count < 10 ? "Light" : (count < 20 ? "Normal" : "Heavy");

Personally I wouldn't do that - it's confusing to read. I'd use
if/else:

string weight;
if (count < 10)
{
weight = "Light";
}
else if (count < 20)
{
weight = "Normal";
}
else
{
weight = "Heavy";
}

More space, but easier to read. You could use a conditional for the
normal/heavy but if/else to differentiate between light and
(normal/heavy) though.

--
Jon Skeet - < XXXX@XXXXX.COM >
Web site: http://www.pobox.com/ ~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com

CSharp/C# >> Define String

by shapper » Thu, 04 Sep 2008 04:56:37 GMT


> Web site: http://www.pobox.com/ ~skeet? > Blog: http://www.msmvps.com/jon.skeet

Maybe use a switch/case?
In my example I used only three values but it will have around 8.

This is for a Tag Cloud ... the strings are the CSS Class of each tag
according to its frequency.
I am using ASP.NET MVC, so I am passing the count to my View and
building the CSS Class there ...

Thanks,
Miguel

CSharp/C# >> Define String

by Jon Skeet [C# MVP] » Thu, 04 Sep 2008 05:47:02 GMT


No, you can't define ranges in a switch/case.

--
Jon Skeet - < XXXX@XXXXX.COM >
Web site: http://www.pobox.com/ ~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com

CSharp/C# >> Define String

by Arne Vajh » Thu, 04 Sep 2008 08:13:15 GMT


The nested if else is very readable, so why not just stick with it.

With 8 ranges you will get more more lines. But what so ? You do not pay
per line of code (I hope) !

If it is <10, <20, ... , <70 then you could switch on count/10. But
the code would not be very flexible.

Arne

Similar Threads

1. Using a #define'd integer to create a #define'd string

2. [OT] User-Defined string Functions MS SQL Server 2005 T-SQL CLR C#

3. [OT] User-Defined string Functions MS SQL Server 2005 T-SQL C# CLR - CSharp/C#

4. Why is an exe-file include defined strings ?

Hi. Hooooooooo~

Listen...

I have a next source:

#define COMM_DATA  ":300:%s"

And next, I compiled the source.
And as result, I got 'comm.exe'

I opened the comm.exe.
And I saw next result.

------------------------------------------------
.....
....
abnormal program termination
   R6009
- not enough space for environment
Microsoft Visual C++ Runtime Library
         :300:%s

.....
....
------------------------------------------------

Why is comm.exe include defined string(COMM_DATA)?



In case I use 'const char', I can hide the COMM_DATA.
But another strings - exam(.Format("hi - %s"), hi2);...

I can see 'hi - %s' in comm.exe.

why? how?

Please tell me why?

5. define string class which implements linked list at backend

6. Index a #define string

Can I do this:

#define MYSTRING "ABC"
.
.
.
char mychar = MYSTRING[0];
.
.
.



-Thanks,
s

7. Defining String parameter max length?

8. User-Defined string Functions Transact-SQL