Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Have a look at the code

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Hello a = new Hello();
            a.aa = "string"; //correct
            a.bb = "String"; //correct
            a.cc = "SB"; //error
        }
    }

    public class Hello
    {
        public string aa;
        public String bb;
        StringBuilder cc = new StringBuilder();
    }
}



In this, I am able to access string and String values. What is the difference between the two ?

And, why cant i access the StringBuilder object ?
Posted

Access Modifers are the reason you can access your "String/string" fields and not the StringBuilder.

Read this article:
Access Modifiers @ MSDN[^]

This is a very basic concept and should be understood before you venture into any reasonable development as it provides the basis for how objects can interact.

As for your question regarding string and String, there isn't really a difference. string is an alias for String (Full name System.String) so when declaring objects you can use either. However if you wish to use static methods off of the String class you are better to use String rather than string.
 
Share this answer
 
The question about "difference" simply makes no sense. What do you mean by difference? Even the expression "to access string and String values" isn't correct. There is no such concept as "accessing values". You are accessing two members of the class, and they happen to have the said values. See the difference?

As to the second question, the answer is way to simple: you are trying to assign a.cc to "SB", but "SB" is a string object, and a.cc is not, this is the instance of a StringBuilder class. Those two types are not assignment-compatible. Besides, the member becomes accessible from some code outside class (and not from derived class) only if its access modifier is internal or more accessible. By default, it is private, which does not allow for outside access (unless you use reflection, but this is a separate topic).

What to do? Get back to some more basic topics of your study of programming. Pay attention for the concepts of type, variables and members of classes/structures, for reference types and their instances, understand the reference objects and referenced objects. Learn how to work with strings (the String class is a very unusual reference type).

—SA
 
Share this answer
 
v3
You cannot directly assign a value to StringBuilder objects.

You have to use member function Append() to do the same.

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            Hello a = new Hello();
            a.aa = "string"; //correct
            a.bb = "String"; //correct
            a.cc.Append("SB")//error
        }
    }
 
    public class Hello
    {
        public string aa;
        public String bb;
        StringBuilder cc = new StringBuilder();
    }
}


Maybe this will work
 
Share this answer
 
visit this link to get your answer for 1st question :

http://stackoverflow.com/questions/7074/whats-the-difference-between-string-and-string

Answer to the second question is your stringbuilder is not public...

check it..
hope it solves your query.
Thanks
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900