Returning <List>

C

Cralis

I am trying to populate a ListView with a list of 'Models' of cars. I
have a data object class for my models, which has a function,
'getListOfModels', which I want to retuyrn a <List> of models.

In my data object, I have also created a class to hold each model, as
shown below.

No database work has been done. Just adding a model, and trying to
return it.

class clsDOModels
{
public static List<Model> getListOfModels()
{
SqlConnection sConn = clsDatabase.GetConnection();
List<Model> models = new List<Model>();
Model mod = new Model();
mod.id = 1;
mod.description = "Raptor";
models.Add( mod );

return models;
}

}

class Model
{
public int id;
public string description;
}


My calling form does this:
ModelParts.classes.clsDOModels.getListOfModels();

No runtime error, so it looks like it's working. My problem is, I'm
not sure how to get what ever is returned. That is, the <List>Models.
My calling form knows nothing about my 'Model' class from the data
object. So I am fundimentally doing something wrong, I think. How do I
declare what ever it is I am returning?
 
R

Roman Wagner

Hi,

try the following.

Write an interface (for example IModel) that defines everything about
a Model you need.
In your case it should look like

interface IModel
{
int ID { get; set; }
string Description { get; set; }
}

Your model class should implement the interface --> class Model :
IModel

and you should return a list of IModel --> public static List<IModel>
GetListOfModels()
 
C

Cralis

Thanks Roman.

I'm pretty new to .Net and OOP, so excuse me here. Should this IModel
be in it's own class file?
At the moment, I have my class Model inside my clsDOModel (Model data
object class file). So should I have a separate file for Interfaces
maybe?

Also, once I have created the IModel interface, will it be visible to
my calling form?
 
L

Ludwig

Thanks Roman.

I'm pretty new to .Net and OOP, so excuse me here. Should this IModel
be in it's own class file?
At the moment, I have my class Model inside my clsDOModel (Model data
object class file). So should I have a separate file for Interfaces
maybe?

Also, once I have created the IModel interface, will it be visible to
my calling form?

I mostly put interfaces even in their own project...
 
C

Cralis

Thanks guys. This sounds like a better plan, and I am learning
something new. :)

I guess you don't have to 'new' an interface, or do you?

I am getting a compiler error now. It says I can't create an instance
of an abstract class when I try "List<IModel> models = new
List<IModel>();"

namespace ModelParts.classes
{
class clsDOModels
{
public static List<IModel> getListOfModels()
{
SqlConnection sConn = clsDatabase.GetConnection();
List<IModel> models = new List<IModel>();
IModel mod = new IModel();
mod.id = 1;
mod.description = "Raptor";
models.Add( mod );

return models;
}

}

interface IModel
{
int id { get; set;}
string description { get; set;}
bool deleted { get; set;}
}
}
 
M

Marc Gravell

If you form has acces to clsDOModels, then it has access to the Model
class (assuming that it is public) since they are side-by-side. You
should be able to use
List<Model> models =
ModelParts.classes.clsDOModels.getListOfModels();
Does this not work? I can't see how the compiler would let you near
getListOfModels() otherwise...?

Additionally - depending on your UI, it might not even be *necessary*
for the UI to know about the model (in the MVP sense, not the Model
class, although they amount to the same thing) - but it makes life a
lot easier if it does ;-p

Marc
 
J

Jon Skeet [C# MVP]

Thanks guys. This sounds like a better plan, and I am learning
something new. :)

I guess you don't have to 'new' an interface, or do you?

It's more that you *can't* create an instance of an interface
directly.
I am getting a compiler error now. It says I can't create an instance
of an abstract class when I try "List<IModel> models = new
List<IModel>();"

No, you're getting the compiler error on:
IModel mod = new IModel();

You should make your Model class implement IModel, then you can do:

IModel mod = new Model();

Jon
 
C

Cralis

Thanks Marc. Yes, I had to add:

using ModelParts.classes;

to my calling forms uses clauses.

However, I am still unable to compile. I keep getting the error:

Cannot create an instance of the abstract class or interface
'ModelParts.interfaces.IModel'

This is happening in my GetListOfModels function, when I try the 'new'
bit.

Why's this happening? Do you have to create new instances of IModel?
 
L

Ludwig

Thanks guys. This sounds like a better plan, and I am learning
something new. :)

I guess you don't have to 'new' an interface, or do you?

I am getting a compiler error now. It says I can't create an instance
of an abstract class when I try "List<IModel> models = new
List<IModel>();"

namespace ModelParts.classes
{
class clsDOModels
{
public static List<IModel> getListOfModels()
{
SqlConnection sConn = clsDatabase.GetConnection();
List<IModel> models = new List<IModel>();
IModel mod = new IModel();
mod.id = 1;
mod.description = "Raptor";
models.Add( mod );

return models;
}

}

interface IModel
{
int id { get; set;}
string description { get; set;}
bool deleted { get; set;}
}
}

First, you have the interface:

interface IModel
{
int id { get; set;}
string description { get; set;}
bool deleted { get; set;}
}

Then you have an implementation of you interface:

public class SomeModel : IModel
{
public string Description
{
get {return description;}
set {description = value;}
}

// same for id and deleted
}

Then you can:

SqlConnection sConn = clsDatabase.GetConnection();
List<IModel> models = new List<IModel>();
IModel mod = new Model(); <<-- the class, not interface
mod.id = 1;
mod.description = "Raptor";
models.Add( mod );

Note: properties start with capital letter (Pascal notation), private
variables with small letter (Camel notation)
 
R

Roman Wagner

Hmm, this will not work.

Since you can't instanciate an interface you should get an error from
your compiler.

In one File of your project (name it IModel.cs) write the following:
using System;
using System.Collections.Generic;
using System.Text;

namespace Tests
{
interface IModel
{
int Id { get; set;}
}
}


in an other file of your project (should be named clsDOModels.cs)
write something like

class clsDOModels
{
public static List<IModel> getListOfModels()
{
List<IModel> models = new List<IModel>();
models.Add(new Model()); // <-- look crete a new instance of Model
//...
return models;
}

class Model : IModel
{
private int _Id;
#region IModel Member

public int Id
{
get
{
return _Id;
}
set
{
_Id = value;
}
}

#endregion
}
}

inside your form you can write something like
foreach(IModel model in clsDOModels.getListOfModels())
{
Console.WriteLine("ID = " + model.Id);
}
 
C

Cralis

Oh!

I went and removed the class Model, and replaced it with interface!
This is where I am falling flat. So, I need to keep 'class Model' and
add : IModel to that.

I understand (I think)
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Cralis said:
I am trying to populate a ListView with a list of 'Models' of cars. I
have a data object class for my models, which has a function,
'getListOfModels', which I want to retuyrn a <List> of models.

In my data object, I have also created a class to hold each model, as
shown below.

No database work has been done. Just adding a model, and trying to
return it.

class clsDOModels
{
public static List<Model> getListOfModels()
{
SqlConnection sConn = clsDatabase.GetConnection();
List<Model> models = new List<Model>();
Model mod = new Model();
mod.id = 1;
mod.description = "Raptor";
models.Add( mod );

return models;
}

}

class Model
{
public int id;
public string description;
}


My calling form does this:
ModelParts.classes.clsDOModels.getListOfModels();

No runtime error, so it looks like it's working. My problem is, I'm
not sure how to get what ever is returned. That is, the <List>Models.
My calling form knows nothing about my 'Model' class from the data
object. So I am fundimentally doing something wrong, I think. How do I
declare what ever it is I am returning?

I would skip the ClassOfDataObjectCreationClassForClassModels class ;)
and just use a Model class, containing private member variables, public
properties and static methods to fetch the data:

public class Model {

private int _id;
private string _description;

public int Id { get { return _id; } }
public string Description { get { return _description; } }

public static List<Model> GetModels() {
List<Model> models;
using (SqlConnection sConn = clsDatabase.GetConnection()) {
models = new List<Model>();
Model mod = new Model();
mod._id = 1;
mod._description = "Raptor";
models.Add( mod );
}
return models;
}

}

To get the return value of the method, just assign it's value to a variable:

List<Model> models = Model.GetModels();
 
L

Ludwig

First, you have the interface:

interface IModel
{
int id { get; set;}
string description { get; set;}
bool deleted { get; set;}
}

Then you have an implementation of you interface:

public class SomeModel : IModel
{
public string Description
{
get {return description;}
set {description = value;}
}

// same for id and deleted
}

Then you can:

SqlConnection sConn = clsDatabase.GetConnection();
List<IModel> models = new List<IModel>();
IModel mod = new Model(); <<-- the class, not interface
mod.id = 1;
mod.description = "Raptor";
models.Add( mod );

Note: properties start with capital letter (Pascal notation), private
variables with small letter (Camel notation)

there was an error:

SqlConnection sConn = clsDatabase.GetConnection();
List<IModel> models = new List<IModel>();
IModel mod = new SomeModel(); <<-- SomeModel !!
mod.id = 1;
mod.description = "Raptor";
models.Add( mod );
 
C

Cralis

Thanks very much to all of you. I'll work with the examples and advice
you have given, and see how far I get. THanks very much for your help
and time.
 
C

Cralis

Thanks Goran. As it's just am learning exercise, I like the idea of
learning about Interfaces while I go along.

I now have this:

namespace ModelParts.classes
{
class clsDOModels
{
public static List<IModel> getListOfModels()
{
SqlConnection sConn = clsDatabase.GetConnection();
List<IModel> models = new List<IModel>();
IModel mod = new Model();
mod.id = 1;
mod.description = "Raptor";
models.Add( mod );

return models;
}

class Model : IModel
{
public int id
{
get { return id; }
set { id = value; }
}

public string description
{
get { return description; }
set { description = value; }
}

public bool deleted
{
get { return deleted; }
set { deleted = value; }
}
}


}

Compiles OK, but I seem to be getting an overflow error when I get to
the line:

set { id = value; }

Doesn't like that too much. It's when I try set id to 1.
 
C

Cralis

Fixed...

class Model : IModel
{
private int _id;
private string _description;
private bool _deleted;

public int id
{
get { return _id; }
set { _id = value; }
}

public string description
{
get { return description; }
set { description = value; }
}

public bool deleted
{
get { return deleted; }
set { deleted = value; }
}
}
 
R

Roman Wagner

Thanks Goran. As it's just am learning exercise, I like the idea of
learning about Interfaces while I go along.

I now have this:

namespace ModelParts.classes
{
class clsDOModels
{
public static List<IModel> getListOfModels()
{
SqlConnection sConn = clsDatabase.GetConnection();
List<IModel> models = new List<IModel>();
IModel mod = new Model();
mod.id = 1;
mod.description = "Raptor";
models.Add( mod );

return models;
}

class Model : IModel
{
public int id
{
get { return id; }
set { id = value; }
}

public string description
{
get { return description; }
set { description = value; }
}

public bool deleted
{
get { return deleted; }
set { deleted = value; }
}
}

}

Compiles OK, but I seem to be getting an overflow error when I get to
the line:

set { id = value; }

Doesn't like that too much. It's when I try set id to 1.

Hmm,

thats why you recursively call the set-Method of your id property.

You should use properties in the following way.

1. declare a private member for your property
2. declare the property with a similar name
3. inside the get and set methods of your property allways use your
private member

Example:

#region member Id
private int _Id; // the private member for your property

public int Id //the property with a similar name
{
get { return _Id; } //use the private member here
set { _Id = value; }
}
#endregion
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Cralis said:
Fixed...

class Model : IModel
{
private int _id;
private string _description;
private bool _deleted;

public int id
{
get { return _id; }
set { _id = value; }
}

public string description
{
get { return description; }
set { description = value; }

get { return _description; }
set { _description = value; }
:)
}

public bool deleted
{
get { return deleted; }
set { deleted = value; }

get { return _deleted; }
set { _deleted = value; }

Public properties are usually written in Pascal case, i.e. Description
instead of description.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top