Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. how to set the value to Exception.Message or this.message properties?

how to set the value to Exception.Message or this.message properties?

Scheduled Pinned Locked Moved C#
helptutorialquestion
15 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    babutkchn
    wrote on last edited by
    #1

    Hi all, how to set a value to custom exception message? I tried to set the value, since its a read only property couldnt able to set it...i tried it as follows public class Customexp:ApplicationException { public void frm(String mess) { this.Message="test"; this.Source = "kjashdfkj"; } } for this code, error is as follows Property or indexer 'System.Exception.Message' cannot be assigned to -- it is read only can anyone tell me how to solve this problem. the clear point is, i've to create an exception object and have to set the value for it. Regards babu

    M G P C 4 Replies Last reply
    0
    • B babutkchn

      Hi all, how to set a value to custom exception message? I tried to set the value, since its a read only property couldnt able to set it...i tried it as follows public class Customexp:ApplicationException { public void frm(String mess) { this.Message="test"; this.Source = "kjashdfkj"; } } for this code, error is as follows Property or indexer 'System.Exception.Message' cannot be assigned to -- it is read only can anyone tell me how to solve this problem. the clear point is, i've to create an exception object and have to set the value for it. Regards babu

      M Offline
      M Offline
      Martin 0
      wrote on last edited by
      #2

      Hello babu, This properties are virtual, which you can override in youre class.

      public override string Message
      {
      get "test";
      }

      public override string Source
      {
      get "test";
      }

      Hope that helps! All the best, Martin

      C 1 Reply Last reply
      0
      • B babutkchn

        Hi all, how to set a value to custom exception message? I tried to set the value, since its a read only property couldnt able to set it...i tried it as follows public class Customexp:ApplicationException { public void frm(String mess) { this.Message="test"; this.Source = "kjashdfkj"; } } for this code, error is as follows Property or indexer 'System.Exception.Message' cannot be assigned to -- it is read only can anyone tell me how to solve this problem. the clear point is, i've to create an exception object and have to set the value for it. Regards babu

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        You set the message in the constructor by using the message in the constructor for the base class:

        public CustomExp() : base("The message.") {
        this.Source = "The source.";
        }

        --- It's amazing to see how much work some people will go through just to avoid a little bit of work.

        M B 2 Replies Last reply
        0
        • B babutkchn

          Hi all, how to set a value to custom exception message? I tried to set the value, since its a read only property couldnt able to set it...i tried it as follows public class Customexp:ApplicationException { public void frm(String mess) { this.Message="test"; this.Source = "kjashdfkj"; } } for this code, error is as follows Property or indexer 'System.Exception.Message' cannot be assigned to -- it is read only can anyone tell me how to solve this problem. the clear point is, i've to create an exception object and have to set the value for it. Regards babu

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          If you don't need to do any manipulation of the message (and this is generally not a good idea), you should set Message by calling the base constructor:

          public class Customexp: ApplicationException
          {
            public Customexp(string message) : base (message) {}
          }
          

          the last thing I want to see is some pasty-faced geek with skin so pale that it's almost translucent trying to bump parts with a partner - John Simmons / outlaw programmer
          Deja View - the feeling that you've seen this post before.

          1 Reply Last reply
          0
          • G Guffa

            You set the message in the constructor by using the message in the constructor for the base class:

            public CustomExp() : base("The message.") {
            this.Source = "The source.";
            }

            --- It's amazing to see how much work some people will go through just to avoid a little bit of work.

            M Offline
            M Offline
            Martin 0
            wrote on last edited by
            #5

            Hello Guffa, Isn't Source also ReadOnly (Only get accessor)? All the best, Martin

            G 1 Reply Last reply
            0
            • M Martin 0

              Hello babu, This properties are virtual, which you can override in youre class.

              public override string Message
              {
              get "test";
              }

              public override string Source
              {
              get "test";
              }

              Hope that helps! All the best, Martin

              C Offline
              C Offline
              Colin Angus Mackay
              wrote on last edited by
              #6

              :confused: That won't even compile.


              Upcoming Scottish Developers events: * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos

              M 1 Reply Last reply
              0
              • B babutkchn

                Hi all, how to set a value to custom exception message? I tried to set the value, since its a read only property couldnt able to set it...i tried it as follows public class Customexp:ApplicationException { public void frm(String mess) { this.Message="test"; this.Source = "kjashdfkj"; } } for this code, error is as follows Property or indexer 'System.Exception.Message' cannot be assigned to -- it is read only can anyone tell me how to solve this problem. the clear point is, i've to create an exception object and have to set the value for it. Regards babu

                C Offline
                C Offline
                Colin Angus Mackay
                wrote on last edited by
                #7

                babutkchn wrote:

                can anyone tell me how to solve this problem.

                You set it in the constructor by calling the base constructor.


                Upcoming Scottish Developers events: * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos

                P 1 Reply Last reply
                0
                • G Guffa

                  You set the message in the constructor by using the message in the constructor for the base class:

                  public CustomExp() : base("The message.") {
                  this.Source = "The source.";
                  }

                  --- It's amazing to see how much work some people will go through just to avoid a little bit of work.

                  B Offline
                  B Offline
                  babutkchn
                  wrote on last edited by
                  #8

                  thank you for your kind reply.The this.source is working. i need to set the value to this.message. can you tell me for that babu

                  G 1 Reply Last reply
                  0
                  • B babutkchn

                    thank you for your kind reply.The this.source is working. i need to set the value to this.message. can you tell me for that babu

                    G Offline
                    G Offline
                    Guffa
                    wrote on last edited by
                    #9

                    I already told you how to set the message. Read my previous post. If you don't understand it, explain what it is that you don't understand.

                    --- It's amazing to see how much work some people will go through just to avoid a little bit of work.

                    1 Reply Last reply
                    0
                    • C Colin Angus Mackay

                      :confused: That won't even compile.


                      Upcoming Scottish Developers events: * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos

                      M Offline
                      M Offline
                      Martin 0
                      wrote on last edited by
                      #10

                      :sigh: Sorry,

                      	public override string Message
                      	{
                      		get
                      		{
                      			return "test";
                      		}
                      	}
                      
                      	public override string Source
                      	{
                      		get
                      		{
                      			return "test";
                      		}
                      	}
                      

                      Martin

                      1 Reply Last reply
                      0
                      • C Colin Angus Mackay

                        babutkchn wrote:

                        can anyone tell me how to solve this problem.

                        You set it in the constructor by calling the base constructor.


                        Upcoming Scottish Developers events: * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos

                        P Offline
                        P Offline
                        Pete OHanlon
                        wrote on last edited by
                        #11

                        Don't you just get sick of saying the same thing over and over again? It's bad enough when it's to different people, but when it's to the same person, arggghhhhh!!!!!:mad:

                        the last thing I want to see is some pasty-faced geek with skin so pale that it's almost translucent trying to bump parts with a partner - John Simmons / outlaw programmer
                        Deja View - the feeling that you've seen this post before.

                        C 1 Reply Last reply
                        0
                        • P Pete OHanlon

                          Don't you just get sick of saying the same thing over and over again? It's bad enough when it's to different people, but when it's to the same person, arggghhhhh!!!!!:mad:

                          the last thing I want to see is some pasty-faced geek with skin so pale that it's almost translucent trying to bump parts with a partner - John Simmons / outlaw programmer
                          Deja View - the feeling that you've seen this post before.

                          C Offline
                          C Offline
                          Colin Angus Mackay
                          wrote on last edited by
                          #12

                          Pete O`Hanlon wrote:

                          Don't you just get sick of saying the same thing over and over again? It's bad enough when it's to different people, but when it's to the same person, arggghhhhh!!!!!

                          Add to that the fact that You, me and Guffa have all told him what to do.


                          Upcoming Scottish Developers events: * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos

                          P 1 Reply Last reply
                          0
                          • C Colin Angus Mackay

                            Pete O`Hanlon wrote:

                            Don't you just get sick of saying the same thing over and over again? It's bad enough when it's to different people, but when it's to the same person, arggghhhhh!!!!!

                            Add to that the fact that You, me and Guffa have all told him what to do.


                            Upcoming Scottish Developers events: * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos

                            P Offline
                            P Offline
                            Pete OHanlon
                            wrote on last edited by
                            #13

                            What do we know? We're only professional developers:-D

                            the last thing I want to see is some pasty-faced geek with skin so pale that it's almost translucent trying to bump parts with a partner - John Simmons / outlaw programmer
                            Deja View - the feeling that you've seen this post before.

                            1 Reply Last reply
                            0
                            • M Martin 0

                              Hello Guffa, Isn't Source also ReadOnly (Only get accessor)? All the best, Martin

                              G Offline
                              G Offline
                              Guffa
                              wrote on last edited by
                              #14

                              Martin# wrote:

                              Isn't Source also ReadOnly (Only get accessor)?

                              Nope. :) MSDN Library: Exception.Source property[^]

                              --- It's amazing to see how much work some people will go through just to avoid a little bit of work.

                              M 1 Reply Last reply
                              0
                              • G Guffa

                                Martin# wrote:

                                Isn't Source also ReadOnly (Only get accessor)?

                                Nope. :) MSDN Library: Exception.Source property[^]

                                --- It's amazing to see how much work some people will go through just to avoid a little bit of work.

                                M Offline
                                M Offline
                                Martin 0
                                wrote on last edited by
                                #15

                                :doh::sigh: Ok, Thats really not my day. Sorry for asking!

                                1 Reply Last reply
                                0
                                Reply
                                • Reply as topic
                                Log in to reply
                                • Oldest to Newest
                                • Newest to Oldest
                                • Most Votes


                                • Login

                                • Don't have an account? Register

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • World
                                • Users
                                • Groups