Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
namespace Samba.Modules.MenuModule
{
[Export, PartCreationPolicy(CreationPolicy.NonShared)]
public class MenuItemViewModel : EntityViewModelBase, IEntityCreator
{
private readonly IMenuService _menuService;

[ImportingConstructor]
public MenuItemViewModel(IMenuService menuService)
{
AddPortionCommand = new CaptionCommand(string.Format(Resources.Add_f, Resources.Portion), OnAddPortion);
DeletePortionCommand = new CaptionCommand(string.Format(Resources.Delete_f, Resources.Portion), OnDeletePortion, CanDeletePortion);
_menuService = menuService;
}
public string Barcode
{
get { return Model.Barcode ?? ""; }
set { Model.Barcode = value; }
}


====> i have to access this barcode to my class.
my class is

namespace Samba.Presentation.Common.ModelBase
{
public abstract class EntityViewModelBase : VisibleViewModelBase where TModel : class, IEntityClass
{
private bool _modelSaved;
private IValidator _validator;

protected EntityViewModelBase()
{
SaveCommand = new CaptionCommand(Resources.Save, OnSave, CanSave);
}

[Browsable(false)]
public TModel Model { get; set; }

[Browsable(false)]
public ICaptionCommand SaveCommand { get; private set; }
protected virtual void OnSave(string value)
{
if (CanSave())
{
string barcode = "192.0.0.1";
//check if IP is duplicate or not
string constring = LocalSettings.ConnectionString + ";Initial Catalog=sTMS 3;";
SqlConnection con = new SqlConnection();
con.ConnectionString = constring;
SqlCommand check_Item = new SqlCommand("SELECT COUNT(*) FROM MenuItems WHERE (Barcode ='" + barcode + "')", con);
con.Open();
int UserExist = (int)check_Item.ExecuteScalar();

if (UserExist > 0)
{
MessageBox.Show("IP already exsit.Please try again", Resources.CantSave);
ErrorMessage = "";
}
else
{
_modelSaved = true;
if (Model.Id == 0)
{
this.PublishEvent(EventTopicNames.AddedModelSaved);
}
this.PublishEvent(EventTopicNames.ModelAddedOrDeleted);
((VisibleViewModelBase)this).PublishEvent(EventTopicNames.ViewClosed);
}
con.Close();

}

What I have tried:

i have tried many solution n=but because this is non shared class m stuck
Posted
Updated 6-Mar-18 2:12am

1 solution

Because your Barcode property is non-static, you need an instance of the MenuModule class in order to get the value - as each instance will have a different value. You create an instance using the new keyword.

An instance is like a Car - you can have a Car class, and all four-wheeled vehicles are Car instances. In the real world you already know this: "this car" is a different vehicle to "that car"; "my car" is a different colour to "your car". Each car is a separate instance of the class of Cars, and is created, bought, sold, and driven independently of all the others.

Classes in computers are the same: you can have multiple instances of your MenuModule class, and each will have a separate Barcode property value. So you need to specify exactly which instance of the class you are trying to refer to, is all:
C#
MenuModule mm = new MenuModule();
...
Console.WriteLine(mm.Barcode);
 
Share this answer
 
Comments
Member 10235493 7-Mar-18 1:43am    
brother i know this concept but instance of Menumodule is not being created because this module has [Export, PartCreationPolicy(CreationPolicy.NonShared)].see top of manuModule class.
Menumodule is not accessible.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900