要从一个类访问属性到另一个类 [英] Ow to access property from one class to another class

查看:92
本文介绍了要从一个类访问属性到另一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

命名空间Samba.Modules.MenuModule

{

[导出,PartCreationPolicy(CreationPolicy.NonShared)]

公共类MenuItemViewModel:EntityViewModelBase, IEntityCreator

{

私人只读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;

}

公共字符串条形码

{

get {return Model.Barcode ?? ; }

set {Model.Barcode = value; }

}





====>我必须把这个条形码发给我的班级。

我的班级是



名称空间Samba.Presentation.Common.ModelBase

{

公共抽象类EntityViewModelBase:VisibleViewModelBase,其中TModel:class,IEntityClass

{

private bool _modelSaved;

private IValidator _validator;



protected EntityViewModelBase()

{

SaveCommand = new CaptionCommand(Resources .Save,OnSave,CanSave);

}



[可浏览(假)]

公共TModel模型{get;组; }



[可浏览(假)]

public ICaptionCommand SaveCommand {get;私人集; }

protected virtual void OnSave(字符串值)

{

if(CanSave())

{

string barcode =192.0.0.1;

//检查IP是否重复

string constring = LocalSettings.ConnectionString +;初始目录= 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已经exsit.Please再试一次,Resources.CantSave);

ErrorMessage = ;

}

其他

{

_modelSaved = true;

if( Model.Id == 0)

{

this.PublishEvent(EventTopicNames.AddedModelSaved);

}

this.PublishEvent(EventTopicNames.ModelAddedOrDeleted);

((VisibleViewModelBase)this).PublishEvent(EventTopicNames.ViewClosed);

}

con。关闭();



}



我尝试过: < br $>


i已经尝试了很多解决方案n =但是因为这是非共享类卡住了

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

推荐答案

因为你的条形码属性是非静态的,您需要一个MenuModule类的实例才能获取值 - 就像每个实例一样不同的价值。您使用 new 关键字创建实例。



实例就像汽车 - 你可以拥有一辆汽车所有四轮车都是Car实例。在现实世界中你已经知道这一点:这辆车与那辆车不同; 我的车与你的车颜色不同。每辆车都是汽车类别的独立实例,独立于所有其他汽车创建,购买,销售和驾驶。



计算机中的类别相同:您可以拥有MenuModule类的多个实例,每个实例都有一个单独的Barcode属性值。因此,您需要准确指定您要引用的类的哪个实例,全部是:

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:
MenuModule mm = new MenuModule();
...
Console.WriteLine(mm.Barcode);


这篇关于要从一个类访问属性到另一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆