dcsimg
Results 1 to 12 of 12

Thread: What is the purpose of the base "object" class?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2015
    Posts
    71

    What is the purpose of the base "object" class?

    I'm confused about what the object class actually is.

    I had a field in a datarow that I was trying to check to see if its value was 0.

    I tried a few thing like:

    if (dr["f1"] == 0) //type mismatch error

    if ((int)dr["f1"] == 0) //casting error

    if ((double)dr["f1"] == 0) //casting error

    Then I ran the debugger and noticed that the type was object {decimal}.

    So I tried the following and it worked:

    if ((decimal)dr["f1"] == 0) //success!

    I also figured out that Convert.ToWhatever worked where casting would not.

    This is all very confusing to me. It seems very random. I would've thought that two objects of the same type would be the same structurally.

    Does that happen with all objects? Are they wrapped in the same class but their underlying structure can be fundamentally different? Can you have an object of type Listbox {int} and another of type Listbox {string}?

    It just seems like a flaw to me somehow.

    Anyway at the very least can someone show me what would be considered best practice for the scenario I described where you're trying to compare an object against another type?

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,410

    Re: What is the purpose of the base "object" class?

    .NET is based on object-oriented principles, including encapsulation and inheritance. The Object class encapsulates the common functionality that every object in a .NET application has. Every other type in the .NET Framework inherits the Object class, either directly or indirectly, and then adds its own specific functionality on top. For instance, the TextBox control in a Windows Forms project has the following inheritance hierarchy:

    Object
    MarshalByRefObject
    Component
    Control
    TextBoxBase
    TextBox

    Each class adds functionality on top of that of its direct parent.

    In cases where you want to be able to use various different types in .NET code, you would generally use a reference of type Object. In your case, a field in a DataRow has to be able to contain a String, an Int32, a Double, a DataTime, a DBNull and various others. The only way you can store objects/values of all those different types in the same place is if that place is type Object, because all those types inherit Object and thus instances of those types are all instances of Object. For that reason, the indexer of the DataRow class (the Item property in VB) is type Object. That means that, any time you get an object/value from a DataRow, you get it as an Object reference. In order to use it as its actual type, you need to cast it as that type.

    Casting only works if the object/value actually is the type you cast it as. That's why, if you have a Decimal value, you can't cast it as Double, Int32 or anything else that it is not. A cast is NOT a conversion of the object/value. It is a conversion of the type by which you refer to that object/value. The word "cast" is not just programming jargon. Have you ever heard the expression "to cast something in a different light"? That means to provide information that makes a particular situation appear to have a different character. It doesn't change the situation, only the way you view the situation. For example, if I told you that I hit my mother, you might think that I'm a terrible person. If I then told you that it was because she was trying to drown my father, that would cast my actions in a different light. My actions didn't change; only the way you viewed them changed. Casting in programming is the same, i.e. the object/value doesn't change; only the way you view it. By the way, that story about my parents was made up.

    So, when you get an object/value from a DataRow, you needs to cast it as the type it is to use it as that type. It's your data so you should know what type it is and thus what type to cast it as. Don't use the Convert class because you're not actually converting anything. If you had a String and you wanted to convert that to an Int32, Convert.ToInt32 would be appropriate. If you have an Object reference containing an Int32 though, there's no converting to be done, so use a cast.

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,410

    Re: What is the purpose of the base "object" class?

    Note that there are lots of other places where the Object type is used specifically because you need to be able to refer to any type object object. For instance, the SelectedItem property of a ListBox or ComboBox control is type Object because those controls can contain any type of Object. The Text property and SelectedIndex properties are type String and Int32 respectively because their values will always be those types. The SelectedItem property's value will be different types at different times, depending on what items are added/bound, so the property type must be able to handle all those different types, i.e. it must be Object.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,410

    Re: What is the purpose of the base "object" class?

    It's also worth noting that typed DataSets alleviate the "problem" you describe. When you create a typed DataSet, the system generates new classes that inherit DataSrt, DataTable and DataRow and add new members that are specific to your data. In your specific case, the standard DataRow class has an indexer to which you pass a String containing a column name and it returns an Object reference to the value in that row for that column, which you then have to cast. A typed DataRow based on that data would have a property named 'f1' of type Decimal, so you'd get full Intellisense and compiler support, ensuring that you didn't accidentally get the name wrong, and no casting would be required.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2015
    Posts
    71

    Re: What is the purpose of the base "object" class?

    Quote Originally Posted by jmcilhinney View Post
    It's also worth noting that typed DataSets alleviate the "problem" you describe. When you create a typed DataSet, the system generates new classes that inherit DataSrt, DataTable and DataRow and add new members that are specific to your data. In your specific case, the standard DataRow class has an indexer to which you pass a String containing a column name and it returns an Object reference to the value in that row for that column, which you then have to cast. A typed DataRow based on that data would have a property named 'f1' of type Decimal, so you'd get full Intellisense and compiler support, ensuring that you didn't accidentally get the name wrong, and no casting would be required.
    That was a good explanation.

    On a slightly different topic I keep wondering if C# is killing itself with bloat. It seems to me they start with a nice pure elegant language like C with only a few reserved words and expressions and then they junk it up with too many "nice" features. I reminds my of the english alphabet (C) vs the chinese alphabet (C#). Maybe that's why Python is so popular.

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    33,890

    Re: What is the purpose of the base "object" class?

    Quote Originally Posted by jmcilhinney View Post
    By the way, that story about my parents was made up.
    To quote NoteMe: BWAHAHAHAHAHAHA!

    C# is not based on a "nice clean language like C". It is based on C++, and "clean" is not a word that would describe THAT language. "Nice" might be used to describe C++, but not "clean". Even MS doesn't use all of C++, but uses what they call a rational subset. C++ has the whole meta-programming with the STL, plus multiple inheritance and the nightmares that can create, and so forth.

    You might say that C# is less bloated than C++, though it is certainly bloating up a bit, but the bloat all grew from the same seed. Things like LINQ and Lambdas are not necessary, but if you use them enough, you find scenarios where a single line of LINQ can replace dozens of lines of code. That LINQ may run slightly slower, or it may not (it usually is a bit slower), but that almost never matters because computers are mostly waiting around for us to fire a neuron, so if they take a hundredth of a millisecond longer on some task, that's fine, because we can't respond that fast anyways. Furthermore, there are people who don't want to read LINQ. I find that it is possible to create Write-Only statements in LINQ, where you have to be a total expert to be able to decipher what the darn line is saying, but those tend to be the lines that are replacing the most code with a single line. Another example would be the fact that multiple inheritance in C++ is very rarely used for the simple fact that a scenario where it is necessary is usually a bad idea already. Also, the STL, which is as complicated as you want it to be from 0 to infinity, has the same qualities as LINQ in that you don't HAVE to use it, using it can be horribly complicated if you make it so, and some people use it for everything.

    So, bloat is really derived from adding features for a minority of users. Most people find it a nuisance, but for those who really like it, the language would be decidedly less appealing without the features. Add that to the fact that you can ignore it if you choose, and MS is better off adding the feature. Therefore, bloat is the result of responsiveness.
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Mar 2015
    Posts
    71

    Re: What is the purpose of the base "object" class?

    Quote Originally Posted by Shaggy Hiker View Post
    To quote NoteMe: BWAHAHAHAHAHAHA!

    C# is not based on a "nice clean language like C". It is based on C++, and "clean" is not a word that would describe THAT language. "Nice" might be used to describe C++, but not "clean". Even MS doesn't use all of C++, but uses what they call a rational subset. C++ has the whole meta-programming with the STL, plus multiple inheritance and the nightmares that can create, and so forth.

    You might say that C# is less bloated than C++, though it is certainly bloating up a bit, but the bloat all grew from the same seed. Things like LINQ and Lambdas are not necessary, but if you use them enough, you find scenarios where a single line of LINQ can replace dozens of lines of code. That LINQ may run slightly slower, or it may not (it usually is a bit slower), but that almost never matters because computers are mostly waiting around for us to fire a neuron, so if they take a hundredth of a millisecond longer on some task, that's fine, because we can't respond that fast anyways. Furthermore, there are people who don't want to read LINQ. I find that it is possible to create Write-Only statements in LINQ, where you have to be a total expert to be able to decipher what the darn line is saying, but those tend to be the lines that are replacing the most code with a single line. Another example would be the fact that multiple inheritance in C++ is very rarely used for the simple fact that a scenario where it is necessary is usually a bad idea already. Also, the STL, which is as complicated as you want it to be from 0 to infinity, has the same qualities as LINQ in that you don't HAVE to use it, using it can be horribly complicated if you make it so, and some people use it for everything.

    So, bloat is really derived from adding features for a minority of users. Most people find it a nuisance, but for those who really like it, the language would be decidedly less appealing without the features. Add that to the fact that you can ignore it if you choose, and MS is better off adding the feature. Therefore, bloat is the result of responsiveness.
    Yeah, it's a trade off when you're adding features. At first you're adding stuff that's critical but at some point it becomes over complicated. It's true that you don't have to use the extra stuff, but the problem is when you have to understand someone else's code. And if I want to use an array what do I pick? List? Array? Enumerable? Ienumerable? Ilist? Collection? Ahhhh!!!

    I checked indeed.com for my state. C# 150 jobs. Python - 600. I just wonder if it has anything to do with the complexity of C#.

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,410

    Re: What is the purpose of the base "object" class?

    Quote Originally Posted by madison320 View Post
    And if I want to use an array what do I pick? List? Array? Enumerable? Ienumerable? Ilist? Collection? Ahhhh!!!
    That question is not making the point you think it is. Only the first two are actually options and there is never an instance where the choice between them is a difficult one. The fact that you don't understand a language and it's class library doesn't make it bloated.

  9. #9
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,538

    Re: What is the purpose of the base "object" class?

    And if I want to use an array what do I pick? List? Array? Enumerable? Ienumerable? Ilist? Collection? Ahhhh!!!

    That question is not making the point you think it is. Only the first two are actually options and there is never an instance where the choice between them is a difficult one. The fact that you don't understand a language and it's class library doesn't make it bloated.
    I second what JMC is saying here, those options you have listed are not all comparable things, that fact you dont understand that is not really the fault of the language.

    As far as Arrays and Lists go, in most instances i use Lists as they have many more features and they work well with what i am trying to do which is passing list of objects back and forth and manipulating them in various ways, but for instance if you want to upload a file to a website or web service often the site will turn it in to an array of bytes and send that over http(s), so they both have there uses it just about using the right tool for the job.

    I checked indeed.com for my state. C# 150 jobs. Python - 600. I just wonder if it has anything to do with the complexity of C#.
    I just checked where i live and its 571 jobs for C# and 440 for Python, so what does that tell you? probably very little much like your search
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  10. #10

    Thread Starter
    Lively Member
    Join Date
    Mar 2015
    Posts
    71

    Re: What is the purpose of the base "object" class?

    Quote Originally Posted by jmcilhinney View Post
    That question is not making the point you think it is. Only the first two are actually options and there is never an instance where the choice between them is a difficult one. The fact that you don't understand a language and it's class library doesn't make it bloated.
    True. That was a bad example. Actually what make sense to me is to have limited syntax and an unlimited library.

    I think what bothers me is all the syntax exceptions.

    For example I just hit this:

    Code:
    public static async Task Method1()
    {
        await Task.Run(() =>
        {
            for (int i = 0; i < 1000; i++)
            {
                Console.WriteLine(" Method 1");
             }
         });
    }
    Why doesn't Method1 need to return a Task Object?

  11. #11
    Frenzied Member PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Newport, UK
    Posts
    1,048

    Re: What is the purpose of the base "object" class?

    The async / await thing is getting the compiler to do a lot of the heavy lifting involved in writing asynchronous code. A method that is async Task rather than async Task<T> is pretty much the async equivalent of a void method.

  12. #12
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,410

    Re: What is the purpose of the base "object" class?

    I think what bothers you is having to read. If you had read about the async and await keywords, you'd already know why that method doesn't return a Task.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width