Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert custom Class to List<>

Tags:

c#

list

Scenario: i have a web form from where i m taking input for Item class now i want to assign values to feature that have return type of list how can i do that.

item value = new item(),
value.feature = serialtextbox.text; //error

foreach ( var item in value) //error 
{
 item.SerialNo= serialtextbox.text; 
}

Item and Item feature classes

Class Item
{
 list<Itemfeature> features;
 }

 class ItemFeature
  {  
    public int SerialNo
    {
        get { return serialno; }
        set { serialno = value; }
    }

    public int Weight
    {
        get { return weight; }
        set { weight = value; }
    }

}

Plz help me out

like image 512
Salman Avatar asked Dec 05 '25 18:12

Salman


1 Answers

Note: No language is specified, but it looks like C#. I'm assuming C# in this answer.

It's not really clear what you're trying to do here, but I'll give it a shot. First of all, you're going to want to post the actual code you're using. This code won't even compile, it's loaded with syntax errors.

Let's take a look at your objects first:

class Item
{
  List<ItemFeature> features;
}

class ItemFeature
{  
  public int SerialNo
  {
    get { return serialno; }
    set { serialno = value; }
  }

  public int Weight
  {
    get { return weight; }
    set { weight = value; }
  }
}

You have a custom class, ItemFeature, which consists of a serial number (integer) and a weight (integer). You then have another custom class, Item, which consists of a list of ItemFeatures.

Now it looks like you're trying to add a new ItemFeature to the Item and then loop through all of them and set them again?. Something like this, perhaps?:

Item value = new Item();
value.features.Add(new ItemFeature { SerialNo = int.Parse(serialtextbox.Text) } );

foreach (var item in value.features)
{
  item.SerialNo = int.Parse(serialtextbox.Text); 
}

(Note that this code is probably as free-hand as your code, so I haven't tested it or anything.)

What I've changed here is:

  1. Setting the SerialNo property, rather than trying to set the ItemFeature directly to a value. You need to dig into the object's property to set a value on that property, not just set it to the entire object.
  2. Converting the input (a string) into the property's type (an int).
  3. Looping through the list, not the Item object itself. The Item object contains a list as a property, but the object itself isn't a list. You can loop through the property, not through the parent object.

A few things to ask/note:

  1. What exactly are you trying to do? You have a list of objects, but you're only setting one and then looping through that one to set it again. Why?
  2. You may want to consider more apt class/property names. Things like "Item" can be a bit unclear.
  3. Your Item class has a public variable, features. This is generally frowned upon. It's better to use a property. That way if you ever have to add logic behind it you won't break compatibility outside of the object itself. The ItemFeature class has properties like this, which is good. They can be additionally shortened by using automatic properties if you'd like, just to keep things clean and simple.
  4. Note that my code isn't doing any input checking on the serialtextbox.Text value. It should be. I presented it in a simpler form as an introductory approach to something that will work under ideal conditions. But something like the following would be better:

var serialValue = 0;

if (!int.TryParse(serialtextbox.Text, out serialValue))
{
  // Here you would probably present an error to the user stating that the form field failed validation.
  // Maybe even throw an exception?  Depends on how you handle errors.
  // Mainly, exit the logic flow.
  return;
}

var value = new Item();
value.features.Add(new ItemFeature { SerialNo = serialValue } );

Edit: I just noticed that my call to .Add() will actually fail. You'll want to initialize the list before trying to use it. Consider changing the Item class to something like this:

class Item
{
  public List<ItemFeature> features { get; set; }

  public Item()
  {
    features = new List<ItemFeature>();
  }
}

Two things changed here:

  1. I converted the public member to a property, as previously mentioned.
  2. I added a constructor which initializes the list so that it can be used. Otherwise, being a reference type, it would default to null. So any call to .Add() or any other method on the list would throw a NullReferenceException because there's no object on which to call the method(s).
like image 113
David Avatar answered Dec 08 '25 09:12

David



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!