Author Topic: C#: Throwing warnings?  (Read 7984 times)

0 Members and 2 Guests are viewing this topic.

Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
C#: Throwing warnings?
« on: November 19, 2007, 01:53:14 pm »
In C#, is there any way to throw a warning, like the ones the compiler gives you? Sort of like an exception, only not a fatal error, and the function can continue? For example, in my my connection sanity, I'd like to do something like this:

Code: [Select]
            // Check for an illegal client.
            if (!( m_sClient.Equals("RATS") || m_sClient.Equals("PXES") || m_sClient.Equals("NB2W") || m_sClient.Equals("VD2D") ||
                   m_sClient.Equals("PX2D") || m_sClient.Equals("3RAW") || m_sClient.Equals("PX3W")))
                throw new ArgumentOutOfRangeException(null, "You have not selected a valid client. See the readme for more information.");

            // LOOK AT ME
            if (m_sClient.Equals("RATS") || m_sClient.Equals("PXES"))
                throw new ConnectionWarning("StarCraft and Brood War are currently subject to checks by Blizzard's Wardent technology, "
                    + "causing possible disconnections after a few minutes.");
            // LOOK AT ME

            // Check if they're attempting to use an SPR-related client (unsupported).
            if (m_sClient.Equals("3RAW") || m_sClient.Equals("PX3W"))
                throw new ArgumentOutOfRangeException(null, "The product you have selected has not had it's logon method implemented.");

(See the LOOK AT ME part in the middle)

Does anyone know of a way to do this?
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline MyndFyre

  • Boticulator Extraordinaire
  • x86
  • Hero Member
  • *****
  • Posts: 4540
  • The wait is over.
    • View Profile
    • JinxBot :: the evolution in boticulation
Re: C#: Throwing warnings?
« Reply #1 on: November 19, 2007, 02:48:32 pm »
I think you really need to take a look at your design.  Should something so deep as your connection, which has several layers of abstraction, be communicating with your user interface in such a high level?  Probably not.

Exceptions are supposed to be raised in exceptional circumstances.  Clearly this isn't such a case.

An alternative might be to create a "Warning" event:

Code: [Select]
// outside of classes
public delegate void WarningEventHandler(object sender, WarningEventArgs e);
public class WarningEventArgs : EventArgs
{
    private string m_msg;

    public WarningEventArgs(string warning)
    {
        m_msg = warning;
    }

    public string Message { get { return m_msg; } }
}

// in the class
[field: NonSerialized]
public event WarningEventHandler Warning;
protected virtual void OnWarning(WarningEventArgs e)
{
    if (Warning != null)
        Warning(this, e);
}

// in your method
if (string.Compare(m_sClient, "RATS", true) == 0 || string.Compare(m_sClient, "PXES", true) == 0)
    OnWarning(new WarningEventArgs("Starcraft and Brood War..."));
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Our species really annoys me.

Offline Newby

  • Moderator
  • Hero Member
  • *****
  • Posts: 10877
  • Thrash!
    • View Profile
Re: C#: Throwing warnings?
« Reply #2 on: November 19, 2007, 07:46:24 pm »
Code: [Select]
            // Check for an illegal client.
            if (!( m_sClient.Equals("RATS") || m_sClient.Equals("PXES") || m_sClient.Equals("NB2W") || m_sClient.Equals("VD2D") ||
                   m_sClient.Equals("PX2D") || m_sClient.Equals("3RAW") || m_sClient.Equals("PX3W")))
                throw new ArgumentOutOfRangeException(null, "You have not selected a valid client. See the readme for more information.");

...

            // Check if they're attempting to use an SPR-related client (unsupported).
            if (m_sClient.Equals("3RAW") || m_sClient.Equals("PX3W"))
                throw new ArgumentOutOfRangeException(null, "The product you have selected has not had it's logon method implemented.");

Why is it that at first you call 3RAW and PX3W invalid... and then proceed to mention that you can't login with them? Wouldn't it make sense that since they don't exist, their login method doesn't exist either?

You should probably stop having all that sex, it's making you retarded.

A good program should simply pick the reversed 4-letter names for the products and... reverse them back on its own. Don't rely on telling the user to pick a 'real' product because as far as they're concerned, RATS could very well be what they used in another program, so they'll just go back to it.
- Newby
http://www.x86labs.org

Quote
[17:32:45] * xar sets mode: -oooooooooo algorithm ban chris cipher newby stdio TehUser tnarongi|away vursed warz
[17:32:54] * xar sets mode: +o newby
[17:32:58] <xar> new rule
[17:33:02] <xar> me and newby rule all

I'd bet that you're currently bloated like a water ballon on a hot summer's day.

That analogy doesn't even make sense.  Why would a water balloon be especially bloated on a hot summer's day? For your sake, I hope there wasn't too much logic testing on your LSAT. 

Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: C#: Throwing warnings?
« Reply #3 on: November 20, 2007, 01:59:54 am »
Why is it that at first you call 3RAW and PX3W invalid... and then proceed to mention that you can't login with them? Wouldn't it make sense that since they don't exist, their login method doesn't exist either?

You should probably stop having all that sex, it's making you retarded.

You should get laid. You missed the not operation.

A good program should simply pick the reversed 4-letter names for the products and... reverse them back on its own. Don't rely on telling the user to pick a 'real' product because as far as they're concerned, RATS could very well be what they used in another program, so they'll just go back to it.

I'm really touched that you approve of my coding.
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline Warrior

  • supreme mac daddy of trolls
  • Hero Member
  • *****
  • Posts: 7503
  • One for a Dime two for a Quarter!
    • View Profile
Re: C#: Throwing warnings?
« Reply #4 on: November 20, 2007, 09:49:37 am »
Why is it that at first you call 3RAW and PX3W invalid... and then proceed to mention that you can't login with them? Wouldn't it make sense that since they don't exist, their login method doesn't exist either?

You should probably stop having all that sex, it's making you retarded.

You should get laid. You missed the not operation.

A good program should simply pick the reversed 4-letter names for the products and... reverse them back on its own. Don't rely on telling the user to pick a 'real' product because as far as they're concerned, RATS could very well be what they used in another program, so they'll just go back to it.

I'm really touched that you approve of my coding.

You should be touched, because I don't approve of it.

Horrible. F
One must ask oneself: "do I will trolling to become a universal law?" And then when one realizes "yes, I do will it to be such," one feels completely justified.
-- from Groundwork for the Metaphysics of Trolling

Offline MyndFyre

  • Boticulator Extraordinaire
  • x86
  • Hero Member
  • *****
  • Posts: 4540
  • The wait is over.
    • View Profile
    • JinxBot :: the evolution in boticulation
Re: C#: Throwing warnings?
« Reply #5 on: November 20, 2007, 02:24:53 pm »
You should be touched, because I don't approve of it.
You're saying you touch Joe?  Homo pedophile.
Horrible. F
K maddox.
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Our species really annoys me.

Offline Warrior

  • supreme mac daddy of trolls
  • Hero Member
  • *****
  • Posts: 7503
  • One for a Dime two for a Quarter!
    • View Profile
Re: C#: Throwing warnings?
« Reply #6 on: November 20, 2007, 02:27:26 pm »
You should be touched, because I don't approve of it.
You're saying you touch Joe?  Homo pedophile.
Horrible. F
K maddox.

party poopers poop at parties.
One must ask oneself: "do I will trolling to become a universal law?" And then when one realizes "yes, I do will it to be such," one feels completely justified.
-- from Groundwork for the Metaphysics of Trolling

Offline Camel

  • Hero Member
  • *****
  • Posts: 1703
    • View Profile
    • BNU Bot
Re: C#: Throwing warnings?
« Reply #7 on: November 20, 2007, 08:06:40 pm »
I prefer to read and write the product id as a DWORD, so I can switch() on it.

<Camel> i said what what
<Blaze> in the butt
<Camel> you want to do it in my butt?
<Blaze> in my butt
<Camel> let's do it in the butt
<Blaze> Okay!

Offline MyndFyre

  • Boticulator Extraordinaire
  • x86
  • Hero Member
  • *****
  • Posts: 4540
  • The wait is over.
    • View Profile
    • JinxBot :: the evolution in boticulation
Re: C#: Throwing warnings?
« Reply #8 on: November 20, 2007, 10:37:34 pm »
I prefer to read and write the product id as a DWORD, so I can switch() on it.

Good thing that C# can switch on strings. :)
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Our species really annoys me.

Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: C#: Throwing warnings?
« Reply #9 on: November 20, 2007, 10:59:05 pm »
I prefer to read and write the product id as a DWORD, so I can switch() on it.

Good thing that C# can switch on strings. :)

Oh. Hehe. Thanks for telling me that a few months ago. :P
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline Camel

  • Hero Member
  • *****
  • Posts: 1703
    • View Profile
    • BNU Bot
Re: C#: Throwing warnings?
« Reply #10 on: November 20, 2007, 11:02:26 pm »
The purpose of a switch statement is to avoid giving bias to the options at the top of the if..else if.. tree. Allowing a switch statement to accept anything beyond constant integer base types for its cases is just silly.

<Camel> i said what what
<Blaze> in the butt
<Camel> you want to do it in my butt?
<Blaze> in my butt
<Camel> let's do it in the butt
<Blaze> Okay!

Offline Newby

  • Moderator
  • Hero Member
  • *****
  • Posts: 10877
  • Thrash!
    • View Profile
Re: C#: Throwing warnings?
« Reply #11 on: November 21, 2007, 12:21:02 am »
You should get laid. You missed the not operation.

So if they enter "STAR" or "SEXP" it'll say invalid product? That's just as retarded. :|
- Newby
http://www.x86labs.org

Quote
[17:32:45] * xar sets mode: -oooooooooo algorithm ban chris cipher newby stdio TehUser tnarongi|away vursed warz
[17:32:54] * xar sets mode: +o newby
[17:32:58] <xar> new rule
[17:33:02] <xar> me and newby rule all

I'd bet that you're currently bloated like a water ballon on a hot summer's day.

That analogy doesn't even make sense.  Why would a water balloon be especially bloated on a hot summer's day? For your sake, I hope there wasn't too much logic testing on your LSAT. 

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: C#: Throwing warnings?
« Reply #12 on: November 21, 2007, 08:32:13 am »
The purpose of a switch statement is to avoid giving bias to the options at the top of the if..else if.. tree. Allowing a switch statement to accept anything beyond constant integer base types for its cases is just silly.
Huh? That doesn't make any sense! An if/else tree with non-overlapping cases gives no bias, so that reason makes no sense.

The reason you use a switch/case is purely speed and style.

Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: C#: Throwing warnings?
« Reply #13 on: November 21, 2007, 02:44:26 pm »
You should get laid. You missed the not operation.

So if they enter "STAR" or "SEXP" it'll say invalid product? That's just as retarded. :|

See the link. They click StarCraft or StarCraft Expansion in the config dialog. You may notice that I switched the direction of all the product codes after posting this.
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline Camel

  • Hero Member
  • *****
  • Posts: 1703
    • View Profile
    • BNU Bot
Re: C#: Throwing warnings?
« Reply #14 on: November 21, 2007, 08:36:36 pm »
The purpose of a switch statement is to avoid giving bias to the options at the top of the if..else if.. tree. Allowing a switch statement to accept anything beyond constant integer base types for its cases is just silly.
Huh? That doesn't make any sense! An if/else tree with non-overlapping cases gives no bias, so that reason makes no sense.

The reason you use a switch/case is purely speed and style.

No, an if/else tree evaluates the conditions in the order they appear. A switch table with constant base types as its cases uses a jump table, which more appropriate for this case.

In any event, for this particular example, where this code is executed once per execution, it is hardly worth arguing over. As a matter of principle, however, you should at least be aware of how each one works.

<Camel> i said what what
<Blaze> in the butt
<Camel> you want to do it in my butt?
<Blaze> in my butt
<Camel> let's do it in the butt
<Blaze> Okay!