Thread: New to C pound - a few questions.

  1. #1
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431

    New to C pound - a few questions.

    I have been casually messing around with VC# express for a couple weeks and while I find it mostly enjoyable, there are still a few things I'm having trouble with.

    It looks as though there is no c# equivilent to the std::vector. I just read another thread which mentioned the generics list, which I haven't tried yet. Is that was most people use? Is there a 3rd party vector class?

    Most of my frustration has been because when you try to write to a read-only value (which there are a lot of), it seems like the statement is just silently ignored. Is there any way to make the program throw, or for the compiler to warn?

    When does a class need to be in its own file? I added a very small class to the same file as my Form1 class and the IDE refused to show the form editor.
    ---
    One more: is there any difference between string and String? They seem to have all the same properties and methods, but the words turn different colors in the editor.
    Last edited by NeonBlack; 12-27-2008 at 07:54 PM.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    It's C Sharp, not C Pound.

    List<> is the functional equivalent of vector.

    string and String are synonymous.

    A Form has to be the first class in the file in order for the IDE's designer to function properly.

    Not sure what you mean about writing to a read-only value being silently ignored. You'll have to clarify. And exceptions are certainly supported.

  3. #3
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    First of all, thank you for your quick reply.

    Quote Originally Posted by rags_to_riches View Post
    It's C Sharp, not C Pound.
    Sorry, maybe my tongue was planted a little too firmly in my cheek! I thought that joke was as old as C# itself.

    Quote Originally Posted by rags_to_riches View Post
    Not sure what you mean about writing to a read-only value being silently ignored. You'll have to clarify. And exceptions are certainly supported.
    For example, the string array RichTextBox.Lines are writable, but the individual elements of the array are read-only.

    So for example, say I have the following:
    Code:
    my_rtb.Lines=some_string_array;
    I will see the text in my rich text box update. But if I do this:
    Code:
    my_rtb.Lines[0]=some_string;
    The text will not be updated because elements are read-only, but there is nothing to tell me I did something wrong. The program will compile and run as if that line were not in the source at all.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    if something is read-only then you cannot change its value at all.
    The .Lines is a property. That means it is not a variable that you just set. It is more a function. So I guess it doesn't change if you do it one way only the other. Don't know why... I also had the same question, but without knowing exactly how the property works is hard to answer. But in this kind of thing you should just see what works for you

    edit: The logic behind not getting an error is that you properties set the private variables only if you give a "good" value. If you do what you do then you might got a "bad" result so it prevents you from doing so. But the compiler doesn't know the logic of a RichTextBox do give you a warning and in some occasions you just want to ignore bad values.
    Last edited by C_ntua; 12-28-2008 at 07:20 AM.

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    When you have a readonly property (get, no set) it's the property itself that is readonly, meaning the data it refers to. In case of objects you may not assign a new object, however the object may have methods that can write to the object's state.

    In this code you're trying to reassign the property, but since it's readonly you're not allowed.
    Code:
    my_rtb.Lines=some_string_array;
    In this code you're using the index operator (a member of the lines object). You're not touching the property, but a method of the object the property refers to.
    Code:
    my_rtb.Lines[0]=some_string;
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    Thanks, C_ntua. I have also spent some time reading, and I think now the "properties" thing is making a little more sense. It still seems dumb to me that it doesn't throw or do anything when it gets a "bad value." I guess I will just have to spend a little more time on msdn when I use a new class (it's probably a good thing to do anyway).

    Magos, I think what you're saying makes sense, but I don't understand how it works. If it's not too much trouble, could you show me a small example of what that part of the class might look like?
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Out of my head, no guarantee of compilable code:
    Code:
    class CLines
    {
      //Index operator, both read and writeable
      public string this[int Line]
      {
        get
        {
          //Return the line here
        }
        set
        {
          //Set the line here
        }
      }
    }
    
    class CRichTextBox
    {
      //Readonly property, you cannot reassign the lines object
      public CLines Lines
      {
        get
        {
          return _Lines;
        }
      }
    
      private CLines _Lines = new CLines();
    }
    
    var MyRichTextBox = new CRichTextBox();
    
    //Accessing the readonly property, this is not allowed
    MyRichTextBox.Lines = ...
    
    //Accessing the read/write index operator, this is allowed
    MyRichTextBox.Lines[0] = ...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  8. #8
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    Magos, that makes a lot more sense. The way that the subscript operator is overloaded still seems goofy to me though. I was able to get a full example working, but I had to add this to the CRichTextBox class:
    Code:
    public string this[int Line]
    {
        get
        {
            return _Lines[Line];
        }
        set
        {
            _Lines[Line] = value;
        }
    }
    is it possible to have multiple get's or set's? Say one that returned CLines and another that returned string[] ?
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  9. #9
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Doubt it, but you could try. If you want both you could have 2 properties, one "Lines" (for CLine) and one "StringLines" (for string) or something.
    It'd probably be even better with just a CLine property which in turn has a text/string property, so you'd do:
    Code:
    class CLine
    {
      public string Text
      {
        get
        set
      }
    }
    
    //Assign a line object
    MyRichTextBox.Lines[0] = new CLine("Yatta");
    
    //Assign a line string
    MyRichTextBox.Lines[0].Text = "Yatta";
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  10. #10
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    Shoot, I was hoping to be able to "overload" the property something like this:
    Code:
    class MyClass
    {
        private string _text="";
        public string Text
        {
            get { return _text; }
            set { _text = value; }
        }
        public char[] Text
        {
            get { return _text.ToCharArray(); }
            set { _text = value.ToString(); }
        }
    }
    Aww well.
    Anyway Magos, I really appreciate your help. Thank you!
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 07:42 AM
  4. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM