本文介绍了C#交叉方法问题,后期绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我在使用这种方法时遇到了一些问题。不知何故,我没有创造或不接受创造的对象(动物)。它似乎只返回null。I''m having some problem with this method of mine. Somehow, I doesn''t create or doesn''t accept the created object (animal). It seems it only return null.public Animal GetSetAnimalInfo(string name, double age, CategoryType cat, object animalType, GenderType gender, string extra) { Animal animalObj = null; switch (cat) { case CategoryType.Bird: Bird.BirdSpecies birdSpecie = (Bird.BirdSpecies)Enum.Parse(typeof(Bird.BirdSpecies), animalType.ToString()); animalObj = Bird.BirdFactory.CreateBird(birdSpecie); //late binding break; case CategoryType.Insect: Insect.InsectSpecies insectSpecie = (Insect.InsectSpecies)Enum.Parse(typeof(Insect.InsectSpecies), animalType.ToString()); animalObj = Insect.InsectFactory.CreateInsect(insectSpecie); //late binding break; case CategoryType.Mammal: Mammal.MammalSpecies mammalSpecie = (Mammal.MammalSpecies)Enum.Parse(typeof(Mammal.MammalSpecies), animalType.ToString()); animalObj = Mammal.MammalFactory.CreateMammal(mammalSpecie); break; case CategoryType.Marine: Marine.MarineSpecies marineSpecie = (Marine.MarineSpecies)Enum.Parse(typeof(Marine.MarineSpecies), animalType.ToString()); animalObj = Marine.MarineFactory.CreateMarine(marineSpecie); break; case CategoryType.Reptile: Reptile.ReptileSpecies reptileSpecie = (Reptile.ReptileSpecies)Enum.Parse(typeof(Reptile.ReptileSpecies), animalType.ToString()); animalObj = Reptile.ReptileFactory.CreateReptile(reptileSpecie); break; } if (animalObj != null) { System.Windows.Forms.MessageBox.Show(animalType.ToString()); } if (!InputUtility.IsLettersOnly(name)) { string message = string.Format("Animal could not be registered because the name input was not correct.") + Environment.NewLine; throw new InvalidInputException(message); } else { animalObj.Name = name; } animalObj.ID = GenerateID(animalObj); if (!InputUtility.IsNumeric(age)) { string message = string.Format("Animal could not be registered because the age input was not correct." + Environment.NewLine); throw new InvalidInputException(message); } else { animalObj.Age = age; } animalObj.Gender = gender; animalObj.ExtraAnimalInfo = extra; AddToList(animalObj); return animalObj; } 这是第二种方法,用于GUI,而第一种方法处理数据,创建对象。但是,如上所述,某种方式第一种方法返回nullThis is the second method, which is being used for the GUI whereas the first one handles the data, and creation of objects. However, as mentioned above, somehow the first method returns nullprivate Animal AddAnimal() { Animal animalObj = null; try { animalManager.GetSetAnimalInfo( txtName.Text, Convert.ToDouble(txtAge.Text), (CategoryType)lstCategory.SelectedIndex, lstAnimalType.SelectedValue, (GenderType)lstGender.SelectedIndex, txtSpecialInput.Text); } catch (Exception ex) { MessageBox.Show(ex.Message); } if (animalObj != null) { try { ListViewItem lvi = new ListViewItem(animalObj.Name); lvi.SubItems.Add(animalObj.ID); lvi.SubItems.Add(animalObj.Age.ToString()); lvi.SubItems.Add(animalObj.Gender.ToString()); lvi.SubItems.Add(animalObj.Category.ToString()); lstVRegisteredAnimals.Items.Add(lvi); } catch (FormatException ex) { MessageBox.Show(ex.Message); } } return animalObj; }推荐答案 这篇关于C#交叉方法问题,后期绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-23 23:15