Questions tagged [class]

33128 questions
-2

votes
2

answer
30

Views

Question: Howto change a funtion to a class in python

I want to create a class with methods, so that I do not have to create multiple functions. Below is my code, I want to get a class with two methods. Method 1: SQL Query, method 2: sql insert. Any tipp is greatly appreciated. Stefan def dbconnect(): dbconn = pymysql.connect(host='192.168.1.2', port=3...
Stefan
-1

votes
0

answer
16

Views

How to print items of class including os('clear')?

Can somebody explain to me why the menu() function in the code below does not print the attributes of the class person while menu2() does? Thank you import os class person(): def __init__(self, name, age): self.name = name self.age = age def printAttr(self): print([self.name, self.age]) def menu(tit...
user10161844
1

votes
3

answer
137

Views

JSON to Python objects

I am deliberating about how to transfer the complex information from a JSON API response to (several) Python objects. I have included the (lengthy) model response below. Note that some values are not always included in the response, and some values are a list of dictionaries. Is there an 'easy' way...
Dave
1

votes
1

answer
1.3k

Views

What is the proper way to change the name of a ViewController class in Swift?

When I change my ViewController class name after it's been created and assigned to a view controller in storyboards I run into problems with the IBOutlets of that view controller, and Xcode does not automatically show the corresponding class in the assistant editor when I click the scene. I've chang...
Austin Berenyi
-3

votes
2

answer
47

Views

How to Override abstract Property?

public abstract class SampleA { public abstract string PropertyA {get;set;} } How to Override abstract PropertyA ?
Nagaraj
1

votes
1

answer
3.7k

Views

Function implementation is missing or not immediately following the declaration, TypeScript class

I got a handwritten array to fill a table in my class, now I'm getting this array's content from a JSON on ngOnInit but it's not structured as I need. So I'm trying to write a function to fill the table array with this new one I'm getting on ngOnInit. The issue is that when I write code outside of...
prevox
1

votes
2

answer
1.9k

Views

How to read connection string inside .NET Standard Class library project from ASP.NET Core

In my solution, I have a ASP.NET Core web project and a .NET Standard class library project. Class library project is the data access layer and I want to read the connection string from my appsettings.json (ASP.NET Core project) in my data access layer. I have found few answers such as by Andrii Li...
Learning Curve
1

votes
1

answer
270

Views

Passing variable to function within class method

I have a class I am attempting to write that looks like the below. When I run the class, I get an error: Audio.js:53 Uncaught ReferenceError: bufferLength is not defined I believe this is because bufferLength is not available in the drawCanvas function. I have tried adding the this keyword, however...
PetePete
0

votes
1

answer
19

Views

Error changing optional parameter values ​of methods

I have a class in Javascript ... class MyClass{ static createElement(selector, settings = { name : 'element', enabled : true, color : '#EAEAEA' }){ name = settings.name; enabled = settings.enabled; color = settings.color; // Do something... } static destroyElement(selector){ // Do something... } } W...
-1

votes
3

answer
23

Views

Spark - Map flat dataframe to a configurable nested json schema

I have a flat dataframe with 5-6 columns. I want to nest them and convert it into a nested dataframe so that I can then write it to parquet format. However, I don't want to use case classes as I am trying to keep the code as configurable as possible. I'm stuck with this part and need some help. My...
devnong
1

votes
2

answer
55

Views

Javascript: instantiate an object with chain calling new

is it possible to instantiate an object with chain calling new on it? I was reading a source code that did that but it was fairly confusing. var SomeObject = function() {}; SomeObject.new.Sometask; any direction would be appreciated.
A.R.H
1

votes
2

answer
93

Views

How do you call different class using a class?

i am learning about object oriented programming in a python and i am trying to write a program which calls class B as a variable to class A. May i know if my approach of handling this correct? Example of class A Class A(): _name = str _cost = int def __init__(self, _name, _cost): self.name = _name s...
Tash
1

votes
2

answer
43

Views

Execute parent class method without calling super()

We have the following class structure: class NamePrinter(): def phone(): print(self.name) def email(): print(self.name) class PhoneCaller(NamePrinter): def __init__(self, name, number, mail): self.name = name self.number = number self.mail = mail def phone(self): # here, NamePrinter.phone() should b...
Jivan
1

votes
1

answer
16

Views

When modifying a property on one instance of a class, that property on all instances of the class are changed

My background is mostly with Python and C#, but I'm helping a friend who is taking a beginning JavaScript class learn to code. The class ended this week, but it never covered how to do classes in JavaScript so I thought I'd throw together an example really quick to illustrate how they work. The only...
ChickenOverlord
1

votes
3

answer
42

Views

Trying to save a class (in a list) to a file

I am trying to create a program where I can store NPCs in a game with certain attributes. Such as: Faction, personality, interests/hobbies. To achieve this, I have created an NPC class. class NPC: #name, faction, position/job, character, interests, other def __init__ (self, name, faction, pos, char,...
DustyHalo
1

votes
1

answer
30

Views

How can I draw a UML class that must be a valid IP address

I want to be able to draw a UML diagram of my code that utilizes IPv4 addresses. They are normally just a string but they must be created in a very specific way to be valid. Here is the regex for one: \b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\. (25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\. (25[0-5]|2[0-4]...
Interlooper
1

votes
1

answer
25

Views

How to realise generic abstract class?

I have abstract class: abstract class AScore { constructor( protected data: T) {} } I implement this class like: class GetActivitiesPupil implements AScore {} Compiler says it is wrong implementation of class
OPV
1

votes
1

answer
22

Views

Ensuring the absence of an __init__ function in Python

I'm writing a plugin-based application. Part of the specification for a plugin is that a plugin must not implement the __init__ method, as any initialization needs to be handled by the plugin manager, not the plugin itself. (A plugin writer would define their startup code in another method, called b...
Mikey T.K.
1

votes
3

answer
32

Views

How can i find max element index for a specific value in list composed of class objects?

I want to find a maximum elements index in a nested list for each row. I got this error: maxe = array[i].index(max(e_object.x for e_object in array[i])) ValueError: 5 is not in list class e_object(): def __init__(self,x): self.x = x array = [] array.append( []) array[0].append(e_object(0)) array[0]....
Şevval Kahraman
1

votes
2

answer
41

Views

Difference between class method vs className.prototype method in JS

I am wondering what's the difference between setting methods via Class body vs prototype binding in JS. (if any) I am currently working on Eloquent JavaScript example and I was surprised when author firstly created a class with bunch of methods inside its body and then created another method with cl...
Martyq
1

votes
1

answer
22

Views

Change object's class

In a chess game, I define p1 = Pawn(). I'd like to be able to promote it: def promote(self, piece): pos = f'{Config.tile_convert(self.x)}{Config.tile_convert(self.y, True)}' try: self = piece(pos, color=self.color, num='s') except NameError: print(f'Cannot promote {self.pieceid} to {piece}') However...
alec_a
0

votes
0

answer
4

Views

Mocking classes with Jest

Given I have a class that calls a function doStuff like so: const myService = require(`./myService`), service = new myService(); exports.doStuff = async (callback) => { try { const data = await service.doOtherStuff(); //I want to mock this return callback(null, data); } catch (err) { callback(new Er...
Tikkes
16

votes
4

answer
887

Views

Why can I not instantiate a class whose constructor is private in a friend class?

I have two classes; Salary that is intended to hold information and calculations regarding the salary of an employee and Employee that has an object of type class Salary and some members like name and address of the employee... What I want to do is to prevent class Salary from being instantiated exc...
Syfu_H
0

votes
0

answer
29

Views

How I make an overloaded operator illegal?

I'm planning to implement an unique smart pointer in c++, so I want to make illegal to use the '=' operator. How can I do it? This is the class that I have: template class uniquePointer{ private: dataType *pointer; public: uniquePointer(){ pointer = nullptr; } uniquePointer(dataType *value){ poin...
BelisaDi
-1

votes
0

answer
24

Views

Create a polymorphic function that sorts shapes by area (parameter is vector of pointers)

BIG EDIT: unfortunately was not showing up in my function sortTheShapes parameter. I had to create multiple classes in order to calculate the area of a shape. I have my base class (Shape) and several derived classes such as circle, rectangle, and triangle. My task now is to create the polymorphic f...
Evan
0

votes
0

answer
14

Views

How to call a function from within a class? error is “X object has no attribute Y”

I'm a hobbyist programmer so I apologise if this seems a simple question. When I click on a button a function is called. The function though doesn't appear to have the info it needs to complete properly. I've tried various things including sending the CodeBox object but they have their own errors...
robster
0

votes
0

answer
9

Views

App/Homepage displaying as blank window while using class structures with tkinter in Python 3.7

I'm brand new to python/programming (2-3 weeks experience) and I'm trying to make a GUI app with tkinter for a Battleship game that I wrote originally as a cmd line game for my first project. I would like this GUI to have a few different screens/frames that walk the user through setting up and playi...
BurtMacklinFBI
0

votes
1

answer
17

Views

How can i withdraw function open_restaurant()?

I need to withdraw function in class. I tried to type name of function, but get mistake: NameError: name 'open_restaurant' is not defined class Restaurant(): '''описание ресторана''' def __init__(self, restaurant_name, cuisine_type): '''Инициализирует атрибуты...
mash_
1

votes
2

answer
4.3k

Views

PHP: Call an internal (Function) method within the same class

Rather than having multiple lines of the same code on each method, I want to set up a (Boot/initialization) method to define common values and such. Then, call that method when needed. Initially, I had something like this: But I would get an error saying something like: Parse error: syntax error, un...
Omar
1

votes
2

answer
5k

Views

How to launch activity from Navigation Drawer?

So I've searched allot about navigation drawers here, and when I was pointed to a tutorial from an answer in another persons question. I did so. I successfully managed to create and style the nav drawer to my liking. But now I've been searching tirelessly on how I can launcher activities from the n...
Robin
1

votes
2

answer
3.1k

Views

VSTO : how to set a ribbon's button from ThisAddIn class

I have a ribbon, that has button in it. When a user selects new document, I would like to change the ribbon's button's text. I think I've done everything but one line: private void ThisAddIn_Startup(object sender, System.EventArgs e) { var wb = this.Application; ((Word.ApplicationEvents4_Event)wb).N...
XristosK
1

votes
1

answer
542

Views

Getting Screen Size in Java as a Statric Variable [duplicate]

This question already has an answer here: Get effective screen size from Java 4 answers I am fairly new to Java so this probably has a simple solution but I just can't find the error. I want to get the screen size with a separate class (called Screen) and want it to be accessed as a static variable...
Ood
0

votes
0

answer
21

Views

TicTacToe Code With For Loops Winner… Cannot Find A Winner Why?

//I have been working on this code for the past week on and off... the main issue I have been having is finding a winner without listing down all the possibilities... //THIS IS MY METHODS CLASS import java.util.Scanner; import java.util.Arrays; public class TicTacToe1 { private String Player1Name; p...
ManU4lifeAK
7

votes
4

answer
193

Views

Why I cannot instantiate a class whose constructor is private in a friend class?

I have two classes; Salary that is intended to hold information and calculations regarding the salary of an employee and Employee that has an object of type class Salary and some members like name and address of the employee... What I want to do is to prevent class Salary from being instantiated and...
Syfu_H
1

votes
1

answer
312

Views

Best practice regarding the inclusion of id of domain model on equals implementation

What is the best practice regarding implementation of equals of domain model in grails? Do we include the id field or just the business rule relevant fields?
froi
1

votes
2

answer
3.3k

Views

c++ create a class exception

I'm having trouble creating a simple class exception. Basically, I have to throw an exception if my number of score on a golf game is 0, because the method decreases the score by 1. How would I create a simple exception class in this case? My current code looks like this, but I'm stuck, I erased eve...
blayrex
1

votes
2

answer
197

Views

Implement an internal interface in an internal class in C#

Why can't I implement an internal interface in an internal class? internal interface IDefinition { string GetValueAsString(string property); } internal sealed class DefinitionArray : IDefinition { internal string GetValueAsString(string property) { return m_definitionRows .Select(o => o.GetValueAsSt...
Vahid
1

votes
2

answer
570

Views

ES6 Javascript: Calling static methods from classes with arrow functions

While this works as intended class ClassWithStaticMethod { static staticMethod() { return ('staticMethod'); }; static staticMethod2() { const yee = this.staticMethod(); return 'staticMethod2 '+yee; }; } console.log(ClassWithStaticMethod.staticMethod2()); //staticMethod2 staticMethod This is, i) have...
GWorking
1

votes
3

answer
59

Views

Creating global variable or creating instance inside of function

It is not a coding question but related to coding concept. I have a service class with some methods and also this class have following 2 private method for url parse and validation process. private boolean isUrlFormatValid(String url) { Pattern pattern = Pattern.compile('^(https?:\\/\\/)?(www\\.)?([...
abidinberkay
1

votes
2

answer
69

Views

Should or Must in Java file name [closed]

I'm learning about Java and have just encountered 2 instructions: A class must have a matching filename Remember that the name of the java file should match the class name Ask: Should in (2) LIKE or DIFFERENT Must in (1) ?? -> (1), (2). What is correct ??
Puskin

View additional questions