0

I have a class Category with two a property name as string.

and have a other class that use this class.

Public Class Myclass
{
      public virtual Category Category
    {
        get;
        set;
    }
}

I have a object of Myclass. I want set string value for Category Property of this object .

Myclass cls=new Myclass();
cls.Category=// a string value

How to set this ?

2
  • I suggest you get a better clarification of the basics of class properties, methods, constructors etc. :) If you are interested, hope this web post , links helps going forward. You can search something which could give better understanding too. Commented Nov 17, 2012 at 7:46
  • The property called Category is of a type also called Category. But we don't know what this Category is? Is it a class or an enum, perhaps? We need to know that. Why do you think a string type should convert to Category type? Since System.String does not derive from (or implement) anything called Category, you will need an implicit conversion operator for it to work. Or simply call some method explicitly which converts your string into Category. Commented Nov 17, 2012 at 8:22

4 Answers 4

2

For example, if one of those string properties is "Name" then you just set it like this:

Myclass cls=new Myclass();
cls.Category = new Category();
cls.Category.Name = "CategoryName1";

Note that the Name property must be accessible to the calling code (e.g. public).

Or you can write it like this:

Myclass cls=new Myclass();
cls.Category = new Category {Name = "CategoryName1"};
Sign up to request clarification or add additional context in comments.

2 Comments

thanks a lot, if this Category with name "CategoryName1" is exist in Category list, it add again to Category list?
No, this will create a new Category object but will not add it to any lists. In other words, the list has no way to know that a new Category object has been created somewhere else in the program.
2

From your example:

Public Class Myclass
{
      public virtual Category Category
    {
        get;
        set;
    }
}

I am thinking you mean this:

Public Class Myclass
{
      public string Category { get; set; } // variable
      public MyClass() {}  //<- empty constructor-might be created by default .. but I like to put them in
}

then call:

Myclass cls=new Myclass();
cls.Category= "text here";

It looks like your type for the Category variable is Category instead of string?

Unless you already have a Category type somewhere that you want to use.

Hope this helps!

2 Comments

Thanks, but i want to have a category property to myclass.
This gives you a property called Category, but it needs a type of string.
1

You cannot set string value to non-string field. But if you need that string inside class instance, you can write like this:

public class MyClass
{
    public Category Category { get; set; }
}

public class Category
{
    public Category(string name)
    {
        Name = name;
    }

    public string Name { get; set; }
}

And then declare Category field of MyClass like this:

var cls = new MyClass();
cls.Category = new Category("hello");

Comments

1

Is this what you meant?

public class Category
{
   public string Name { get; set; }
   public string Description { get; set; }
}


Category myCategory = new Category();
myCategory.Category.Name = "Basketball";
myCategory.Category.Description = "Played Indoors";


Tutorial Reference: Below are some examples to help you understand properties and class's

Tutorial 1

Tutorial 2

1 Comment

Thanks, but i want add property of category type to myclass.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.