473,809 Members | 2,724 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Returning <List>

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,
'getListOfModel s', which I want to retuyrn a <Listof 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<ModelgetLi stOfModels()
{
SqlConnection sConn = clsDatabase.Get Connection();
List<Modelmodel s = 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.clas ses.clsDOModels .getListOfModel s();

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?

May 18 '07 #1
17 2105
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 ()
May 18 '07 #2
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?

May 18 '07 #3
On 18 May 2007 00:28:00 -0700, Cralis <ad***@myschool mates.comwrote:
>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...

--
Ludwig
http://www.coders-lab.be
May 18 '07 #4
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<IModelmod els = new
List<IModel>(); "

namespace ModelParts.clas ses
{
class clsDOModels
{
public static List<IModelgetL istOfModels()
{
SqlConnection sConn = clsDatabase.Get Connection();
List<IModelmode ls = 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;}
}
}

May 18 '07 #5
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<Modelmodel s =
ModelParts.clas ses.clsDOModels .getListOfModel s();
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

May 18 '07 #6
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)

May 18 '07 #7
>
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.Get Connection();
List<IModelmode ls = 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.Get Connection();
List<IModelmode ls = new List<IModel>();
IModel mod = new SomeModel(); <<-- SomeModel !!
mod.id = 1;
mod.description = "Raptor";
models.Add( mod );

--
Ludwig
http://www.coders-lab.be
May 18 '07 #8
Cralis wrote:
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,
'getListOfModel s', which I want to retuyrn a <Listof 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<ModelgetLi stOfModels()
{
SqlConnection sConn = clsDatabase.Get Connection();
List<Modelmodel s = 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.clas ses.clsDOModels .getListOfModel s();

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 ClassOfDataObje ctCreationClass ForClassModels 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<ModelGetMo dels() {
List<Modelmodel s;
using (SqlConnection sConn = clsDatabase.Get Connection()) {
models = new List<Model>();
Model mod = new Model();
mod._id = 1;
mod._descriptio n = "Raptor";
models.Add( mod );
}
return models;
}

}

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

List<Modelmodel s = Model.GetModels ();

--
Göran Andersson
_____
http://www.guffa.com
May 18 '07 #9
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.

May 18 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
8712
by: G?nter Omer | last post by:
Hi there! I'm just trying to compile a header file (unsing Borland C++ Builder 10)implementing a class, containing the declaration of the STL <list> but it refuses to work. The following errors occure: "test.h" line 29 type name expected "test.h" line 29 declaration missing
4
1300
by: lallous | last post by:
Hello Given this: list<mystruct_t> lst; lst.push_back(item1); lst.push_back(item2); lst.push_back(item3);
5
6257
by: Kenneth | last post by:
<list> seems to be a powerful structure to store the related nodes in memory for fast operations, but the examples I found are all related to primitive type storage. I'm doing a project on C++ with my defined classes to be added to linked list structure so as to facilitate the operation of all instances of defined classes. Is that possible to apply such classes to <list> or <Vector> structure? Thanks!
2
2070
by: Tom Vogel | last post by:
I'd like to use the XML Documentation Tags to comment my C# code. But many of the tags do not have any effect when I execute the "Build Comment Web Pages" menu. For example, the <list> tag gets reendered as is. The resulting HTML page contains the exact same tags, which are not valid HTML. The same with <see> or <see also>. Isn't this function supposed to transform these tags into valid HTML? Did anybody see this work properly?
1
1434
by: Steffo | last post by:
Why can't I use stl list in my dll. I'ts no problem with vector, but when trying list I get the followning error msg: error C2061: syntax error : identifier 'list' Hu!! S.
3
2239
by: kuiyuli | last post by:
I'm using VC++ .Net to do a simlple program. I tried to use <vector> <list> in the program, and I simply put the folowing lines " #include <list> #include <vector> #include <string> using namespace std; ...... vector <Vector> vectorlist;
12
2653
by: arnuld | last post by:
It works fine. any advice on making it better or if I can improve my C++ coding skills: /* C++ Primer - 4/e * * Chapter 9 - Sequential Containers * exercise 9.18 - STATEMENT * Write a program to copy elements from a list of "ints" * to 2 "deques". The list elements that are even should go into one deque * and even elements should go into 2nd deque.
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9600
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10633
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10114
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7651
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6880
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4331
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3011
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.